From 66682b83c5a1ee57a8ddda6e1f760133adcc833a Mon Sep 17 00:00:00 2001 From: Henry H Li Date: Wed, 20 Mar 2024 14:19:02 -0400 Subject: [PATCH 1/3] scaffolded OperatorConfig API Signed-off-by: Henry H Li --- PROJECT | 22 +++- api/v1alpha1/operatorconfig_types.go | 64 +++++++++ api/v1alpha1/zz_generated.deepcopy.go | 121 ++++++++++++++++++ bundle.Dockerfile | 2 +- ...fecycle-manager.clusterserviceversion.yaml | 39 +++--- .../operator.ibm.com_operatorconfigs.yaml | 59 +++++++++ bundle/metadata/annotations.yaml | 2 +- .../operator.ibm.com_operatorconfigs.yaml | 57 +++++++++ config/crd/kustomization.yaml | 5 + .../crd/patches/label_in_operatorconfigs.yaml | 9 ++ config/rbac/operatorconfig_editor_role.yaml | 31 +++++ config/rbac/operatorconfig_viewer_role.yaml | 27 ++++ .../operator_v1alpha1_operatorconfig.yaml | 12 ++ controllers/operatorconfig_controller.go | 62 +++++++++ controllers/suite_test.go | 80 ++++++++++++ go.mod | 1 + go.sum | 1 + main.go | 8 ++ 18 files changed, 578 insertions(+), 24 deletions(-) create mode 100644 api/v1alpha1/operatorconfig_types.go create mode 100644 bundle/manifests/operator.ibm.com_operatorconfigs.yaml create mode 100644 config/crd/bases/operator.ibm.com_operatorconfigs.yaml create mode 100644 config/crd/patches/label_in_operatorconfigs.yaml create mode 100644 config/rbac/operatorconfig_editor_role.yaml create mode 100644 config/rbac/operatorconfig_viewer_role.yaml create mode 100644 config/samples/operator_v1alpha1_operatorconfig.yaml create mode 100644 controllers/operatorconfig_controller.go create mode 100644 controllers/suite_test.go diff --git a/PROJECT b/PROJECT index 83657404..faf71aed 100644 --- a/PROJECT +++ b/PROJECT @@ -1,5 +1,13 @@ +# Code generated by tool. DO NOT EDIT. +# This file is used to track the info used to scaffold your project +# and allow the plugins properly work. +# More info: https://book.kubebuilder.io/reference/project-config.html domain: ibm.com -layout: go.kubebuilder.io/v3 +layout: +- go.kubebuilder.io/v3 +plugins: + manifests.sdk.operatorframework.io/v2: {} + scorecard.sdk.operatorframework.io/v2: {} projectName: operand-deployment-lifecycle-manager repo: github.com/IBM/operand-deployment-lifecycle-manager resources: @@ -27,7 +35,13 @@ resources: kind: OperandBindInfo path: github.com/IBM/operand-deployment-lifecycle-manager/api/v1alpha1 version: v1alpha1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: ibm.com + group: operator + kind: OperatorConfig + path: github.com/IBM/operand-deployment-lifecycle-manager/api/v1alpha1 + version: v1alpha1 version: "3" -plugins: - manifests.sdk.operatorframework.io/v2: {} - scorecard.sdk.operatorframework.io/v2: {} diff --git a/api/v1alpha1/operatorconfig_types.go b/api/v1alpha1/operatorconfig_types.go new file mode 100644 index 00000000..c2c5a924 --- /dev/null +++ b/api/v1alpha1/operatorconfig_types.go @@ -0,0 +1,64 @@ +// +// Copyright 2022 IBM Corporation +// +// 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. + +// OperatorConfigSpec defines the desired state of OperatorConfig +type OperatorConfigSpec 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 OperatorConfig. Edit operatorconfig_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// OperatorConfigStatus defines the observed state of OperatorConfig +type OperatorConfigStatus struct { + // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster + // Important: Run "make" to regenerate code after modifying this file +} + +//+kubebuilder:object:root=true +//+kubebuilder:subresource:status + +// OperatorConfig is the Schema for the operatorconfigs API +type OperatorConfig struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec OperatorConfigSpec `json:"spec,omitempty"` + Status OperatorConfigStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// OperatorConfigList contains a list of OperatorConfig +type OperatorConfigList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []OperatorConfig `json:"items"` +} + +func init() { + SchemeBuilder.Register(&OperatorConfig{}, &OperatorConfigList{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 531bb62f..c3013da1 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -88,6 +88,13 @@ func (in *ConfigResource) DeepCopyInto(out *ConfigResource) { *out = new(runtime.RawExtension) (*in).DeepCopyInto(*out) } + if in.OwnerReferences != nil { + in, out := &in.OwnerReferences, &out.OwnerReferences + *out = make([]OwnerReference, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ConfigResource. @@ -717,6 +724,95 @@ func (in *Operator) DeepCopy() *Operator { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *OperatorConfig) DeepCopyInto(out *OperatorConfig) { + *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 OperatorConfig. +func (in *OperatorConfig) DeepCopy() *OperatorConfig { + if in == nil { + return nil + } + out := new(OperatorConfig) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *OperatorConfig) 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 *OperatorConfigList) DeepCopyInto(out *OperatorConfigList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]OperatorConfig, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OperatorConfigList. +func (in *OperatorConfigList) DeepCopy() *OperatorConfigList { + if in == nil { + return nil + } + out := new(OperatorConfigList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *OperatorConfigList) 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 *OperatorConfigSpec) DeepCopyInto(out *OperatorConfigSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OperatorConfigSpec. +func (in *OperatorConfigSpec) DeepCopy() *OperatorConfigSpec { + if in == nil { + return nil + } + out := new(OperatorConfigSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *OperatorConfigStatus) DeepCopyInto(out *OperatorConfigStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OperatorConfigStatus. +func (in *OperatorConfigStatus) DeepCopy() *OperatorConfigStatus { + if in == nil { + return nil + } + out := new(OperatorConfigStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *OperatorStatus) DeepCopyInto(out *OperatorStatus) { *out = *in @@ -737,6 +833,31 @@ func (in *OperatorStatus) DeepCopy() *OperatorStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *OwnerReference) DeepCopyInto(out *OwnerReference) { + *out = *in + if in.Controller != nil { + in, out := &in.Controller, &out.Controller + *out = new(bool) + **out = **in + } + if in.BlockOwnerDeletion != nil { + in, out := &in.BlockOwnerDeletion, &out.BlockOwnerDeletion + *out = new(bool) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OwnerReference. +func (in *OwnerReference) DeepCopy() *OwnerReference { + if in == nil { + return nil + } + out := new(OwnerReference) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ReconcileRequest) DeepCopyInto(out *ReconcileRequest) { *out = *in diff --git a/bundle.Dockerfile b/bundle.Dockerfile index 162e5db1..6f953fb9 100644 --- a/bundle.Dockerfile +++ b/bundle.Dockerfile @@ -7,7 +7,7 @@ LABEL operators.operatorframework.io.bundle.metadata.v1=metadata/ LABEL operators.operatorframework.io.bundle.package.v1=ibm-odlm LABEL operators.operatorframework.io.bundle.channels.v1=v4.3 LABEL operators.operatorframework.io.bundle.channel.default.v1=v4.3 -LABEL operators.operatorframework.io.metrics.builder=operator-sdk-v1.32.0 +LABEL operators.operatorframework.io.metrics.builder=operator-sdk-v1.29.0 LABEL operators.operatorframework.io.metrics.mediatype.v1=metrics+v1 LABEL operators.operatorframework.io.metrics.project_layout=go.kubebuilder.io/v3 diff --git a/bundle/manifests/operand-deployment-lifecycle-manager.clusterserviceversion.yaml b/bundle/manifests/operand-deployment-lifecycle-manager.clusterserviceversion.yaml index b85d3e1d..f815c155 100644 --- a/bundle/manifests/operand-deployment-lifecycle-manager.clusterserviceversion.yaml +++ b/bundle/manifests/operand-deployment-lifecycle-manager.clusterserviceversion.yaml @@ -129,12 +129,12 @@ metadata: categories: Developer Tools, Monitoring, Logging & Tracing, Security certified: "false" containerImage: icr.io/cpopen/odlm:latest - createdAt: "2024-03-13T21:38:26Z" + createdAt: "2024-03-20T18:18:08Z" description: The Operand Deployment Lifecycle Manager provides a Kubernetes CRD-based API to manage the lifecycle of operands. nss.operator.ibm.com/managed-operators: ibm-odlm olm.skipRange: '>=1.2.0 <4.3.0' operators.openshift.io/infrastructure-features: '["disconnected"]' - operators.operatorframework.io/builder: operator-sdk-v1.32.0 + operators.operatorframework.io/builder: operator-sdk-v1.29.0 operators.operatorframework.io/project_layout: go.kubebuilder.io/v3 repository: https://github.com/IBM/operand-deployment-lifecycle-manager support: IBM @@ -215,6 +215,9 @@ spec: x-descriptors: - urn:alm:descriptor:io.kubernetes.phase version: v1alpha1 + - kind: OperatorConfig + name: operatorconfigs.operator.ibm.com + version: v1alpha1 description: |- # Introduction @@ -562,22 +565,22 @@ spec: install: spec: clusterPermissions: - - rules: - - apiGroups: - - operators.coreos.com - resources: - - catalogsources - verbs: - - get - - apiGroups: - - operator.ibm.com - resources: - - certmanagers - - auditloggings - verbs: - - get - - delete - serviceAccountName: operand-deployment-lifecycle-manager + - rules: + - apiGroups: + - operators.coreos.com + resources: + - catalogsources + verbs: + - get + - apiGroups: + - operator.ibm.com + resources: + - certmanagers + - auditloggings + verbs: + - get + - delete + serviceAccountName: operand-deployment-lifecycle-manager deployments: - label: app.kubernetes.io/instance: operand-deployment-lifecycle-manager diff --git a/bundle/manifests/operator.ibm.com_operatorconfigs.yaml b/bundle/manifests/operator.ibm.com_operatorconfigs.yaml new file mode 100644 index 00000000..20d9f31d --- /dev/null +++ b/bundle/manifests/operator.ibm.com_operatorconfigs.yaml @@ -0,0 +1,59 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.6.1 + creationTimestamp: null + labels: + app.kubernetes.io/instance: operand-deployment-lifecycle-manager + app.kubernetes.io/managed-by: operand-deployment-lifecycle-manager + app.kubernetes.io/name: operand-deployment-lifecycle-manager + name: operatorconfigs.operator.ibm.com +spec: + group: operator.ibm.com + names: + kind: OperatorConfig + listKind: OperatorConfigList + plural: operatorconfigs + singular: operatorconfig + scope: Namespaced + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: OperatorConfig is the Schema for the operatorconfigs API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: OperatorConfigSpec defines the desired state of OperatorConfig + properties: + foo: + description: Foo is an example field of OperatorConfig. Edit operatorconfig_types.go + to remove/update + type: string + type: object + status: + description: OperatorConfigStatus defines the observed state of OperatorConfig + type: object + type: object + served: true + storage: true + subresources: + status: {} +status: + acceptedNames: + kind: "" + plural: "" + conditions: [] + storedVersions: [] diff --git a/bundle/metadata/annotations.yaml b/bundle/metadata/annotations.yaml index e4758c86..f3bfce43 100644 --- a/bundle/metadata/annotations.yaml +++ b/bundle/metadata/annotations.yaml @@ -6,7 +6,7 @@ annotations: operators.operatorframework.io.bundle.package.v1: ibm-odlm operators.operatorframework.io.bundle.channels.v1: v4.3 operators.operatorframework.io.bundle.channel.default.v1: v4.3 - operators.operatorframework.io.metrics.builder: operator-sdk-v1.32.0 + operators.operatorframework.io.metrics.builder: operator-sdk-v1.29.0 operators.operatorframework.io.metrics.mediatype.v1: metrics+v1 operators.operatorframework.io.metrics.project_layout: go.kubebuilder.io/v3 # Annotations for testing. diff --git a/config/crd/bases/operator.ibm.com_operatorconfigs.yaml b/config/crd/bases/operator.ibm.com_operatorconfigs.yaml new file mode 100644 index 00000000..b9062664 --- /dev/null +++ b/config/crd/bases/operator.ibm.com_operatorconfigs.yaml @@ -0,0 +1,57 @@ + +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.6.1 + creationTimestamp: null + name: operatorconfigs.operator.ibm.com +spec: + group: operator.ibm.com + names: + kind: OperatorConfig + listKind: OperatorConfigList + plural: operatorconfigs + singular: operatorconfig + scope: Namespaced + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: OperatorConfig is the Schema for the operatorconfigs API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: OperatorConfigSpec defines the desired state of OperatorConfig + properties: + foo: + description: Foo is an example field of OperatorConfig. Edit operatorconfig_types.go + to remove/update + type: string + type: object + status: + description: OperatorConfigStatus defines the observed state of OperatorConfig + type: object + type: object + served: true + storage: true + subresources: + status: {} +status: + acceptedNames: + kind: "" + plural: "" + conditions: [] + storedVersions: [] diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index be422ac7..b50b14f2 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -6,6 +6,7 @@ resources: - bases/operator.ibm.com_operandconfigs.yaml - bases/operator.ibm.com_operandbindinfos.yaml - bases/operator.ibm.com_operandregistries.yaml +- bases/operator.ibm.com_operatorconfigs.yaml # +kubebuilder:scaffold:crdkustomizeresource patchesStrategicMerge: @@ -15,6 +16,7 @@ patchesStrategicMerge: #- patches/webhook_in_operandconfigs.yaml #- patches/webhook_in_operandbindinfoes.yaml #- patches/webhook_in_operandregistries.yaml +#- patches/webhook_in_operatorconfigs.yaml # +kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix. @@ -23,6 +25,7 @@ patchesStrategicMerge: #- patches/cainjection_in_operandconfigs.yaml #- patches/cainjection_in_operandbindinfoes.yaml #- patches/cainjection_in_operandregistries.yaml +#- patches/cainjection_in_operatorconfigs.yaml # +kubebuilder:scaffold:crdkustomizecainjectionpatch # patches here are for adding labels for each CRD @@ -30,6 +33,8 @@ patchesStrategicMerge: - patches/label_in_operandconfigs.yaml - patches/label_in_operandbindinfos.yaml - patches/label_in_operandregistries.yaml +- patches/label_in_operatorconfigs.yaml +#- patches/cainjection_in_operatorconfigs.yaml # +kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/config/crd/patches/label_in_operatorconfigs.yaml b/config/crd/patches/label_in_operatorconfigs.yaml new file mode 100644 index 00000000..49773cb2 --- /dev/null +++ b/config/crd/patches/label_in_operatorconfigs.yaml @@ -0,0 +1,9 @@ +# The following patch adds a directive for certmanager to inject CA into the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + labels: + app.kubernetes.io/instance: "operand-deployment-lifecycle-manager" + app.kubernetes.io/managed-by: "operand-deployment-lifecycle-manager" + app.kubernetes.io/name: "operand-deployment-lifecycle-manager" + name: operatorconfigs.operator.ibm.com diff --git a/config/rbac/operatorconfig_editor_role.yaml b/config/rbac/operatorconfig_editor_role.yaml new file mode 100644 index 00000000..17a6e034 --- /dev/null +++ b/config/rbac/operatorconfig_editor_role.yaml @@ -0,0 +1,31 @@ +# permissions for end users to edit operatorconfigs. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: operatorconfig-editor-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: operand-deployment-lifecycle-manager + app.kubernetes.io/part-of: operand-deployment-lifecycle-manager + app.kubernetes.io/managed-by: kustomize + name: operatorconfig-editor-role +rules: +- apiGroups: + - operator.ibm.com + resources: + - operatorconfigs + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - operator.ibm.com + resources: + - operatorconfigs/status + verbs: + - get diff --git a/config/rbac/operatorconfig_viewer_role.yaml b/config/rbac/operatorconfig_viewer_role.yaml new file mode 100644 index 00000000..a785595c --- /dev/null +++ b/config/rbac/operatorconfig_viewer_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to view operatorconfigs. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: operatorconfig-viewer-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: operand-deployment-lifecycle-manager + app.kubernetes.io/part-of: operand-deployment-lifecycle-manager + app.kubernetes.io/managed-by: kustomize + name: operatorconfig-viewer-role +rules: +- apiGroups: + - operator.ibm.com + resources: + - operatorconfigs + verbs: + - get + - list + - watch +- apiGroups: + - operator.ibm.com + resources: + - operatorconfigs/status + verbs: + - get diff --git a/config/samples/operator_v1alpha1_operatorconfig.yaml b/config/samples/operator_v1alpha1_operatorconfig.yaml new file mode 100644 index 00000000..f57b5d62 --- /dev/null +++ b/config/samples/operator_v1alpha1_operatorconfig.yaml @@ -0,0 +1,12 @@ +apiVersion: operator.ibm.com/v1alpha1 +kind: OperatorConfig +metadata: + labels: + app.kubernetes.io/name: operatorconfig + app.kubernetes.io/instance: operatorconfig-sample + app.kubernetes.io/part-of: operand-deployment-lifecycle-manager + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: operand-deployment-lifecycle-manager + name: operatorconfig-sample +spec: + # TODO(user): Add fields here diff --git a/controllers/operatorconfig_controller.go b/controllers/operatorconfig_controller.go new file mode 100644 index 00000000..8779089c --- /dev/null +++ b/controllers/operatorconfig_controller.go @@ -0,0 +1,62 @@ +// +// Copyright 2022 IBM Corporation +// +// 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" + + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + + operatorv1alpha1 "github.com/IBM/operand-deployment-lifecycle-manager/api/v1alpha1" +) + +// OperatorConfigReconciler reconciles a OperatorConfig object +type OperatorConfigReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=operator.ibm.com,resources=operatorconfigs,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=operator.ibm.com,resources=operatorconfigs/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=operator.ibm.com,resources=operatorconfigs/finalizers,verbs=update + +// Reconcile is part of the main kubernetes reconciliation loop which aims to +// move the current state of the cluster closer to the desired state. +// TODO(user): Modify the Reconcile function to compare the state specified by +// the OperatorConfig object against the actual cluster state, and then +// perform operations to make the cluster state reflect the state specified by +// the user. +// +// For more details, check Reconcile and its Result here: +// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.1/pkg/reconcile +func (r *OperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + _ = log.FromContext(ctx) + + // TODO(user): your logic here + + return ctrl.Result{}, nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *OperatorConfigReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&operatorv1alpha1.OperatorConfig{}). + Complete(r) +} diff --git a/controllers/suite_test.go b/controllers/suite_test.go new file mode 100644 index 00000000..844c11a4 --- /dev/null +++ b/controllers/suite_test.go @@ -0,0 +1,80 @@ +// +// Copyright 2022 IBM Corporation +// +// 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/v2" + . "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" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + + operatorv1alpha1 "github.com/IBM/operand-deployment-lifecycle-manager/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) + + RunSpecs(t, "Controller Suite") +} + +var _ = BeforeSuite(func() { + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + + By("bootstrapping test environment") + testEnv = &envtest.Environment{ + CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, + ErrorIfCRDPathMissing: true, + } + + var err error + // cfg is defined in this file globally. + cfg, err = testEnv.Start() + Expect(err).NotTo(HaveOccurred()) + Expect(cfg).NotTo(BeNil()) + + err = operatorv1alpha1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + + //+kubebuilder:scaffold:scheme + + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + Expect(err).NotTo(HaveOccurred()) + Expect(k8sClient).NotTo(BeNil()) + +}) + +var _ = AfterSuite(func() { + By("tearing down the test environment") + err := testEnv.Stop() + Expect(err).NotTo(HaveOccurred()) +}) diff --git a/go.mod b/go.mod index 14ca6fa0..af70c077 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/jaegertracing/jaeger-operator v1.36.0 github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 github.com/onsi/ginkgo v1.16.5 + github.com/onsi/ginkgo/v2 v2.1.4 github.com/onsi/gomega v1.19.0 github.com/openshift/api v0.0.0-20220124143425-d74727069f6f github.com/operator-framework/api v0.6.2 diff --git a/go.sum b/go.sum index 948430fb..9f91737e 100644 --- a/go.sum +++ b/go.sum @@ -732,6 +732,7 @@ github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9k github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/ginkgo/v2 v2.1.4 h1:GNapqRSid3zijZ9H77KrgVG4/8KqiyRsxcSxe+7ApXY= +github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= diff --git a/main.go b/main.go index 0b53313b..d20d0666 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,7 @@ import ( nssv1 "github.com/IBM/ibm-namespace-scope-operator/api/v1" operatorv1alpha1 "github.com/IBM/operand-deployment-lifecycle-manager/api/v1alpha1" + "github.com/IBM/operand-deployment-lifecycle-manager/controllers" "github.com/IBM/operand-deployment-lifecycle-manager/controllers/constant" "github.com/IBM/operand-deployment-lifecycle-manager/controllers/k8sutil" "github.com/IBM/operand-deployment-lifecycle-manager/controllers/namespacescope" @@ -166,6 +167,13 @@ func main() { } } } + if err = (&controllers.OperatorConfigReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + klog.Error(err, "unable to create controller", "controller", "OperatorConfig") + os.Exit(1) + } // +kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("health", healthz.Ping); err != nil { From 7b31fecd5fc82b3d00dc75da48d5239dd67d6eb2 Mon Sep 17 00:00:00 2001 From: Henry H Li Date: Wed, 27 Mar 2024 14:34:02 -0400 Subject: [PATCH 2/3] changed operatorconfig controller to use NewODLMOperator type Signed-off-by: Henry H Li --- .../{ => operatorconfig}/operatorconfig_controller.go | 8 +++----- .../operatorconfig_suite_test.go} | 2 +- main.go | 7 +++---- 3 files changed, 7 insertions(+), 10 deletions(-) rename controllers/{ => operatorconfig}/operatorconfig_controller.go (93%) rename controllers/{suite_test.go => operatorconfig/operatorconfig_suite_test.go} (98%) diff --git a/controllers/operatorconfig_controller.go b/controllers/operatorconfig/operatorconfig_controller.go similarity index 93% rename from controllers/operatorconfig_controller.go rename to controllers/operatorconfig/operatorconfig_controller.go index 8779089c..5d656df9 100644 --- a/controllers/operatorconfig_controller.go +++ b/controllers/operatorconfig/operatorconfig_controller.go @@ -14,23 +14,21 @@ // limitations under the License. // -package controllers +package operatorconfig import ( "context" - "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" operatorv1alpha1 "github.com/IBM/operand-deployment-lifecycle-manager/api/v1alpha1" + deploy "github.com/IBM/operand-deployment-lifecycle-manager/controllers/operator" ) // OperatorConfigReconciler reconciles a OperatorConfig object type OperatorConfigReconciler struct { - client.Client - Scheme *runtime.Scheme + *deploy.ODLMOperator } //+kubebuilder:rbac:groups=operator.ibm.com,resources=operatorconfigs,verbs=get;list;watch;create;update;patch;delete diff --git a/controllers/suite_test.go b/controllers/operatorconfig/operatorconfig_suite_test.go similarity index 98% rename from controllers/suite_test.go rename to controllers/operatorconfig/operatorconfig_suite_test.go index 844c11a4..aef3a37a 100644 --- a/controllers/suite_test.go +++ b/controllers/operatorconfig/operatorconfig_suite_test.go @@ -14,7 +14,7 @@ // limitations under the License. // -package controllers +package operatorconfig import ( "path/filepath" diff --git a/main.go b/main.go index d20d0666..007da1ae 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,6 @@ import ( nssv1 "github.com/IBM/ibm-namespace-scope-operator/api/v1" operatorv1alpha1 "github.com/IBM/operand-deployment-lifecycle-manager/api/v1alpha1" - "github.com/IBM/operand-deployment-lifecycle-manager/controllers" "github.com/IBM/operand-deployment-lifecycle-manager/controllers/constant" "github.com/IBM/operand-deployment-lifecycle-manager/controllers/k8sutil" "github.com/IBM/operand-deployment-lifecycle-manager/controllers/namespacescope" @@ -50,6 +49,7 @@ import ( "github.com/IBM/operand-deployment-lifecycle-manager/controllers/operandrequest" deploy "github.com/IBM/operand-deployment-lifecycle-manager/controllers/operator" "github.com/IBM/operand-deployment-lifecycle-manager/controllers/operatorchecker" + "github.com/IBM/operand-deployment-lifecycle-manager/controllers/operatorconfig" "github.com/IBM/operand-deployment-lifecycle-manager/controllers/util" // +kubebuilder:scaffold:imports ) @@ -167,9 +167,8 @@ func main() { } } } - if err = (&controllers.OperatorConfigReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), + if err = (&operatorconfig.OperatorConfigReconciler{ + ODLMOperator: deploy.NewODLMOperator(mgr, "OperatorConfig"), }).SetupWithManager(mgr); err != nil { klog.Error(err, "unable to create controller", "controller", "OperatorConfig") os.Exit(1) From e44d839d2d9fb5947badf74dcd539af35dea40ac Mon Sep 17 00:00:00 2001 From: Henry H Li Date: Wed, 27 Mar 2024 14:39:07 -0400 Subject: [PATCH 3/3] renamed operatorconfig reconciler to just Reconciler Signed-off-by: Henry H Li --- controllers/operatorconfig/operatorconfig_controller.go | 6 +++--- main.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/controllers/operatorconfig/operatorconfig_controller.go b/controllers/operatorconfig/operatorconfig_controller.go index 5d656df9..9abeab89 100644 --- a/controllers/operatorconfig/operatorconfig_controller.go +++ b/controllers/operatorconfig/operatorconfig_controller.go @@ -27,7 +27,7 @@ import ( ) // OperatorConfigReconciler reconciles a OperatorConfig object -type OperatorConfigReconciler struct { +type Reconciler struct { *deploy.ODLMOperator } @@ -44,7 +44,7 @@ type OperatorConfigReconciler struct { // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.1/pkg/reconcile -func (r *OperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = log.FromContext(ctx) // TODO(user): your logic here @@ -53,7 +53,7 @@ func (r *OperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reque } // SetupWithManager sets up the controller with the Manager. -func (r *OperatorConfigReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&operatorv1alpha1.OperatorConfig{}). Complete(r) diff --git a/main.go b/main.go index 007da1ae..7281861f 100644 --- a/main.go +++ b/main.go @@ -167,7 +167,7 @@ func main() { } } } - if err = (&operatorconfig.OperatorConfigReconciler{ + if err = (&operatorconfig.Reconciler{ ODLMOperator: deploy.NewODLMOperator(mgr, "OperatorConfig"), }).SetupWithManager(mgr); err != nil { klog.Error(err, "unable to create controller", "controller", "OperatorConfig")