From a048169819312a58e56dd0602a1a6ab512ff14dd Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Sat, 18 Jul 2020 15:29:59 +0100 Subject: [PATCH] Add ImageUpdateAutomation type and controller kubebuilder create api --group image --version v1alpha1 \ --kind ImageUpdateAutomation --- PROJECT | 6 +- api/v1alpha1/groupversion_info.go | 36 ++++++ api/v1alpha1/imageupdateautomation_types.go | 63 ++++++++++ api/v1alpha1/zz_generated.deepcopy.go | 114 ++++++++++++++++++ config/crd/kustomization.yaml | 21 ++++ config/crd/kustomizeconfig.yaml | 17 +++ ...cainjection_in_imageupdateautomations.yaml | 8 ++ .../webhook_in_imageupdateautomations.yaml | 17 +++ .../imageupdateautomation_editor_role.yaml | 24 ++++ .../imageupdateautomation_viewer_role.yaml | 20 +++ .../image_v1alpha1_imageupdateautomation.yaml | 7 ++ .../imageupdateautomation_controller.go | 53 ++++++++ controllers/suite_test.go | 81 +++++++++++++ go.mod | 3 + main.go | 12 ++ 15 files changed, 481 insertions(+), 1 deletion(-) create mode 100644 api/v1alpha1/groupversion_info.go create mode 100644 api/v1alpha1/imageupdateautomation_types.go create mode 100644 api/v1alpha1/zz_generated.deepcopy.go create mode 100644 config/crd/kustomization.yaml create mode 100644 config/crd/kustomizeconfig.yaml create mode 100644 config/crd/patches/cainjection_in_imageupdateautomations.yaml create mode 100644 config/crd/patches/webhook_in_imageupdateautomations.yaml create mode 100644 config/rbac/imageupdateautomation_editor_role.yaml create mode 100644 config/rbac/imageupdateautomation_viewer_role.yaml create mode 100644 config/samples/image_v1alpha1_imageupdateautomation.yaml create mode 100644 controllers/imageupdateautomation_controller.go create mode 100644 controllers/suite_test.go diff --git a/PROJECT b/PROJECT index 55fd582..314d710 100644 --- a/PROJECT +++ b/PROJECT @@ -1,3 +1,7 @@ domain: fluxcd.io -repo: github.com/squaremo/image-automation +repo: github.com/squaremo/image-automation-controller +resources: +- group: image + kind: ImageUpdateAutomation + version: v1alpha1 version: "2" diff --git a/api/v1alpha1/groupversion_info.go b/api/v1alpha1/groupversion_info.go new file mode 100644 index 0000000..ba9e2ff --- /dev/null +++ b/api/v1alpha1/groupversion_info.go @@ -0,0 +1,36 @@ +/* +Copyright 2020 Michael Bridgen + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package v1alpha1 contains API Schema definitions for the image v1alpha1 API group +// +kubebuilder:object:generate=true +// +groupName=image.fluxcd.io +package v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/controller-runtime/pkg/scheme" +) + +var ( + // GroupVersion is group version used to register these objects + GroupVersion = schema.GroupVersion{Group: "image.fluxcd.io", Version: "v1alpha1"} + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} + + // AddToScheme adds the types in this group-version to the given scheme. + AddToScheme = SchemeBuilder.AddToScheme +) diff --git a/api/v1alpha1/imageupdateautomation_types.go b/api/v1alpha1/imageupdateautomation_types.go new file mode 100644 index 0000000..3567e03 --- /dev/null +++ b/api/v1alpha1/imageupdateautomation_types.go @@ -0,0 +1,63 @@ +/* +Copyright 2020 Michael Bridgen + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! +// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. + +// ImageUpdateAutomationSpec defines the desired state of ImageUpdateAutomation +type ImageUpdateAutomationSpec struct { + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster + // Important: Run "make" to regenerate code after modifying this file + + // Foo is an example field of ImageUpdateAutomation. Edit ImageUpdateAutomation_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// ImageUpdateAutomationStatus defines the observed state of ImageUpdateAutomation +type ImageUpdateAutomationStatus struct { + // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster + // Important: Run "make" to regenerate code after modifying this file +} + +// +kubebuilder:object:root=true + +// ImageUpdateAutomation is the Schema for the imageupdateautomations API +type ImageUpdateAutomation struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec ImageUpdateAutomationSpec `json:"spec,omitempty"` + Status ImageUpdateAutomationStatus `json:"status,omitempty"` +} + +// +kubebuilder:object:root=true + +// ImageUpdateAutomationList contains a list of ImageUpdateAutomation +type ImageUpdateAutomationList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []ImageUpdateAutomation `json:"items"` +} + +func init() { + SchemeBuilder.Register(&ImageUpdateAutomation{}, &ImageUpdateAutomationList{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 0000000..962527a --- /dev/null +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,114 @@ +// +build !ignore_autogenerated + +/* +Copyright 2020 Michael Bridgen + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by controller-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImageUpdateAutomation) DeepCopyInto(out *ImageUpdateAutomation) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.Spec = in.Spec + out.Status = in.Status +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageUpdateAutomation. +func (in *ImageUpdateAutomation) DeepCopy() *ImageUpdateAutomation { + if in == nil { + return nil + } + out := new(ImageUpdateAutomation) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ImageUpdateAutomation) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImageUpdateAutomationList) DeepCopyInto(out *ImageUpdateAutomationList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]ImageUpdateAutomation, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageUpdateAutomationList. +func (in *ImageUpdateAutomationList) DeepCopy() *ImageUpdateAutomationList { + if in == nil { + return nil + } + out := new(ImageUpdateAutomationList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ImageUpdateAutomationList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImageUpdateAutomationSpec) DeepCopyInto(out *ImageUpdateAutomationSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageUpdateAutomationSpec. +func (in *ImageUpdateAutomationSpec) DeepCopy() *ImageUpdateAutomationSpec { + if in == nil { + return nil + } + out := new(ImageUpdateAutomationSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ImageUpdateAutomationStatus) DeepCopyInto(out *ImageUpdateAutomationStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageUpdateAutomationStatus. +func (in *ImageUpdateAutomationStatus) DeepCopy() *ImageUpdateAutomationStatus { + if in == nil { + return nil + } + out := new(ImageUpdateAutomationStatus) + in.DeepCopyInto(out) + return out +} diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml new file mode 100644 index 0000000..ca61653 --- /dev/null +++ b/config/crd/kustomization.yaml @@ -0,0 +1,21 @@ +# This kustomization.yaml is not intended to be run by itself, +# since it depends on service name and namespace that are out of this kustomize package. +# It should be run by config/default +resources: +- bases/image.fluxcd.io_imageupdateautomations.yaml +# +kubebuilder:scaffold:crdkustomizeresource + +patchesStrategicMerge: +# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. +# patches here are for enabling the conversion webhook for each CRD +#- patches/webhook_in_imageupdateautomations.yaml +# +kubebuilder:scaffold:crdkustomizewebhookpatch + +# [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix. +# patches here are for enabling the CA injection for each CRD +#- patches/cainjection_in_imageupdateautomations.yaml +# +kubebuilder:scaffold:crdkustomizecainjectionpatch + +# the following config is for teaching kustomize how to do kustomization for CRDs. +configurations: +- kustomizeconfig.yaml diff --git a/config/crd/kustomizeconfig.yaml b/config/crd/kustomizeconfig.yaml new file mode 100644 index 0000000..6f83d9a --- /dev/null +++ b/config/crd/kustomizeconfig.yaml @@ -0,0 +1,17 @@ +# This file is for teaching kustomize how to substitute name and namespace reference in CRD +nameReference: +- kind: Service + version: v1 + fieldSpecs: + - kind: CustomResourceDefinition + group: apiextensions.k8s.io + path: spec/conversion/webhookClientConfig/service/name + +namespace: +- kind: CustomResourceDefinition + group: apiextensions.k8s.io + path: spec/conversion/webhookClientConfig/service/namespace + create: false + +varReference: +- path: metadata/annotations diff --git a/config/crd/patches/cainjection_in_imageupdateautomations.yaml b/config/crd/patches/cainjection_in_imageupdateautomations.yaml new file mode 100644 index 0000000..0b01627 --- /dev/null +++ b/config/crd/patches/cainjection_in_imageupdateautomations.yaml @@ -0,0 +1,8 @@ +# The following patch adds a directive for certmanager to inject CA into the CRD +# CRD conversion requires k8s 1.13 or later. +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + annotations: + cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) + name: imageupdateautomations.image.fluxcd.io diff --git a/config/crd/patches/webhook_in_imageupdateautomations.yaml b/config/crd/patches/webhook_in_imageupdateautomations.yaml new file mode 100644 index 0000000..4eb5596 --- /dev/null +++ b/config/crd/patches/webhook_in_imageupdateautomations.yaml @@ -0,0 +1,17 @@ +# The following patch enables conversion webhook for CRD +# CRD conversion requires k8s 1.13 or later. +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: imageupdateautomations.image.fluxcd.io +spec: + conversion: + strategy: Webhook + webhookClientConfig: + # this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank, + # but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager) + caBundle: Cg== + service: + namespace: system + name: webhook-service + path: /convert diff --git a/config/rbac/imageupdateautomation_editor_role.yaml b/config/rbac/imageupdateautomation_editor_role.yaml new file mode 100644 index 0000000..9c4966a --- /dev/null +++ b/config/rbac/imageupdateautomation_editor_role.yaml @@ -0,0 +1,24 @@ +# permissions for end users to edit imageupdateautomations. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: imageupdateautomation-editor-role +rules: +- apiGroups: + - image.fluxcd.io + resources: + - imageupdateautomations + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - image.fluxcd.io + resources: + - imageupdateautomations/status + verbs: + - get diff --git a/config/rbac/imageupdateautomation_viewer_role.yaml b/config/rbac/imageupdateautomation_viewer_role.yaml new file mode 100644 index 0000000..7c5babf --- /dev/null +++ b/config/rbac/imageupdateautomation_viewer_role.yaml @@ -0,0 +1,20 @@ +# permissions for end users to view imageupdateautomations. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: imageupdateautomation-viewer-role +rules: +- apiGroups: + - image.fluxcd.io + resources: + - imageupdateautomations + verbs: + - get + - list + - watch +- apiGroups: + - image.fluxcd.io + resources: + - imageupdateautomations/status + verbs: + - get diff --git a/config/samples/image_v1alpha1_imageupdateautomation.yaml b/config/samples/image_v1alpha1_imageupdateautomation.yaml new file mode 100644 index 0000000..60c2e0a --- /dev/null +++ b/config/samples/image_v1alpha1_imageupdateautomation.yaml @@ -0,0 +1,7 @@ +apiVersion: image.fluxcd.io/v1alpha1 +kind: ImageUpdateAutomation +metadata: + name: imageupdateautomation-sample +spec: + # Add fields here + foo: bar diff --git a/controllers/imageupdateautomation_controller.go b/controllers/imageupdateautomation_controller.go new file mode 100644 index 0000000..f824811 --- /dev/null +++ b/controllers/imageupdateautomation_controller.go @@ -0,0 +1,53 @@ +/* +Copyright 2020 Michael Bridgen + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package controllers + +import ( + "context" + + "github.com/go-logr/logr" + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + + imagev1alpha1 "github.com/squaremo/image-automation-controller/api/v1alpha1" +) + +// ImageUpdateAutomationReconciler reconciles a ImageUpdateAutomation object +type ImageUpdateAutomationReconciler struct { + client.Client + Log logr.Logger + Scheme *runtime.Scheme +} + +// +kubebuilder:rbac:groups=image.fluxcd.io,resources=imageupdateautomations,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=image.fluxcd.io,resources=imageupdateautomations/status,verbs=get;update;patch + +func (r *ImageUpdateAutomationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { + _ = context.Background() + _ = r.Log.WithValues("imageupdateautomation", req.NamespacedName) + + // your logic here + + return ctrl.Result{}, nil +} + +func (r *ImageUpdateAutomationReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&imagev1alpha1.ImageUpdateAutomation{}). + Complete(r) +} diff --git a/controllers/suite_test.go b/controllers/suite_test.go new file mode 100644 index 0000000..6e8bdce --- /dev/null +++ b/controllers/suite_test.go @@ -0,0 +1,81 @@ +/* +Copyright 2020 Michael Bridgen + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package controllers + +import ( + "path/filepath" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/rest" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" + "sigs.k8s.io/controller-runtime/pkg/envtest/printer" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + + imagev1alpha1 "github.com/squaremo/image-automation-controller/api/v1alpha1" + // +kubebuilder:scaffold:imports +) + +// These tests use Ginkgo (BDD-style Go testing framework). Refer to +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. + +var cfg *rest.Config +var k8sClient client.Client +var testEnv *envtest.Environment + +func TestAPIs(t *testing.T) { + RegisterFailHandler(Fail) + + RunSpecsWithDefaultAndCustomReporters(t, + "Controller Suite", + []Reporter{printer.NewlineReporter{}}) +} + +var _ = BeforeSuite(func(done Done) { + logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) + + By("bootstrapping test environment") + testEnv = &envtest.Environment{ + CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, + } + + var err error + cfg, err = testEnv.Start() + Expect(err).ToNot(HaveOccurred()) + Expect(cfg).ToNot(BeNil()) + + err = imagev1alpha1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + + // +kubebuilder:scaffold:scheme + + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + Expect(err).ToNot(HaveOccurred()) + Expect(k8sClient).ToNot(BeNil()) + + close(done) +}, 60) + +var _ = AfterSuite(func() { + By("tearing down the test environment") + err := testEnv.Stop() + Expect(err).ToNot(HaveOccurred()) +}) diff --git a/go.mod b/go.mod index ef0a366..7af67dc 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,9 @@ module github.com/squaremo/image-automation-controller go 1.13 require ( + github.com/go-logr/logr v0.1.0 + github.com/onsi/ginkgo v1.11.0 + github.com/onsi/gomega v1.8.1 k8s.io/apimachinery v0.17.2 k8s.io/client-go v0.17.2 sigs.k8s.io/controller-runtime v0.5.0 diff --git a/main.go b/main.go index 229e200..d54d1ce 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,9 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/log/zap" + + imagev1alpha1 "github.com/squaremo/image-automation-controller/api/v1alpha1" + "github.com/squaremo/image-automation-controller/controllers" // +kubebuilder:scaffold:imports ) @@ -36,6 +39,7 @@ var ( func init() { _ = clientgoscheme.AddToScheme(scheme) + _ = imagev1alpha1.AddToScheme(scheme) // +kubebuilder:scaffold:scheme } @@ -62,6 +66,14 @@ func main() { os.Exit(1) } + if err = (&controllers.ImageUpdateAutomationReconciler{ + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("controllers").WithName("ImageUpdateAutomation"), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "ImageUpdateAutomation") + os.Exit(1) + } // +kubebuilder:scaffold:builder setupLog.Info("starting manager")