From 774cbf7c9fee96256041a7ec54083460f1fc1ebc Mon Sep 17 00:00:00 2001 From: "mykyta.oleksiienko" Date: Thu, 15 Feb 2024 14:01:40 +0200 Subject: [PATCH] issue-698, Save default user ref for zookeeper --- Makefile | 2 +- apis/clusters/v1beta1/zookeeper_types.go | 3 ++- .../clusters/v1beta1/zz_generated.deepcopy.go | 5 +++++ .../clusters.instaclustr.com_zookeepers.yaml | 11 +++++++++++ .../samples/clusters_v1beta1_zookeeper.yaml | 2 +- controllers/clusters/zookeeper_controller.go | 19 +++++++++++++++++++ 6 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 0b20f0367..7e5875f4a 100644 --- a/Makefile +++ b/Makefile @@ -142,8 +142,8 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. cd scripts && ./make_creds_secret.sh cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} - $(KUSTOMIZE) build config/default | kubectl apply -f - kubectl apply -f ~/creds_secret.yaml + $(KUSTOMIZE) build config/default | kubectl apply -f - .PHONY: helm-deploy helm-deploy: diff --git a/apis/clusters/v1beta1/zookeeper_types.go b/apis/clusters/v1beta1/zookeeper_types.go index c4bda24f2..4d9b4d125 100644 --- a/apis/clusters/v1beta1/zookeeper_types.go +++ b/apis/clusters/v1beta1/zookeeper_types.go @@ -45,7 +45,8 @@ type ZookeeperStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file - ClusterStatus `json:",inline"` + ClusterStatus `json:",inline"` + DefaultUserSecretRef *Reference `json:"DefaultRef,omitempty"` } //+kubebuilder:object:root=true diff --git a/apis/clusters/v1beta1/zz_generated.deepcopy.go b/apis/clusters/v1beta1/zz_generated.deepcopy.go index 9dfb532d6..a19ccb2af 100644 --- a/apis/clusters/v1beta1/zz_generated.deepcopy.go +++ b/apis/clusters/v1beta1/zz_generated.deepcopy.go @@ -3095,6 +3095,11 @@ func (in *ZookeeperSpec) DeepCopy() *ZookeeperSpec { func (in *ZookeeperStatus) DeepCopyInto(out *ZookeeperStatus) { *out = *in in.ClusterStatus.DeepCopyInto(&out.ClusterStatus) + if in.DefaultUserSecretRef != nil { + in, out := &in.DefaultUserSecretRef, &out.DefaultUserSecretRef + *out = new(apiextensions.ObjectReference) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ZookeeperStatus. diff --git a/config/crd/bases/clusters.instaclustr.com_zookeepers.yaml b/config/crd/bases/clusters.instaclustr.com_zookeepers.yaml index badcbe5d3..07c9e6285 100644 --- a/config/crd/bases/clusters.instaclustr.com_zookeepers.yaml +++ b/config/crd/bases/clusters.instaclustr.com_zookeepers.yaml @@ -141,6 +141,17 @@ spec: status: description: ZookeeperStatus defines the observed state of Zookeeper properties: + DefaultRef: + description: ObjectReference is namespaced reference to an object + properties: + name: + type: string + namespace: + type: string + required: + - name + - namespace + type: object cdcid: type: string currentClusterOperationStatus: diff --git a/config/samples/clusters_v1beta1_zookeeper.yaml b/config/samples/clusters_v1beta1_zookeeper.yaml index 570d4582e..19b122e2b 100644 --- a/config/samples/clusters_v1beta1_zookeeper.yaml +++ b/config/samples/clusters_v1beta1_zookeeper.yaml @@ -3,7 +3,7 @@ kind: Zookeeper metadata: name: zookeeper-sample spec: - name: "username-zookeeper" + name: "mykyta-zookeeper-test" # description: "some description" dataCentres: - clientToServerEncryption: false diff --git a/controllers/clusters/zookeeper_controller.go b/controllers/clusters/zookeeper_controller.go index 113f842a4..9dde6f293 100644 --- a/controllers/clusters/zookeeper_controller.go +++ b/controllers/clusters/zookeeper_controller.go @@ -199,6 +199,7 @@ func (r *ZookeeperReconciler) handleCreateCluster( } func (r *ZookeeperReconciler) createDefaultSecret(ctx context.Context, zk *v1beta1.Zookeeper, l logr.Logger) error { + patch := zk.NewPatch() username, password, err := r.API.GetDefaultCredentialsV1(zk.Status.ID) if err != nil { l.Error(err, "Cannot get default user creds for Zookeeper cluster from the Instaclustr API", @@ -224,6 +225,24 @@ func (r *ZookeeperReconciler) createDefaultSecret(ctx context.Context, zk *v1bet return err } + zk.Status.DefaultUserSecretRef = &v1beta1.Reference{ + Name: secret.Name, + Namespace: secret.Namespace, + } + + err = r.Status().Patch(ctx, zk, patch) + if err != nil { + l.Error(err, "Cannot patch Zookeeper resource", + "cluster name", zk.Spec.Name, + "status", zk.Status) + + r.EventRecorder.Eventf( + zk, models.Warning, models.PatchFailed, + "Cluster resource patch is failed. Reason: %v", err) + + return err + } + return nil }