From df6fa53d1a53225c2c36192f1d066a00e0c4e18e Mon Sep 17 00:00:00 2001 From: "mykyta.oleksiienko" Date: Mon, 4 Mar 2024 15:33:33 +0200 Subject: [PATCH] issue-728, default user secret already exists error --- controllers/clusters/helpers.go | 36 ------------------- .../clusters/kafkaconnect_controller.go | 21 ++++++----- controllers/clusters/zookeeper_controller.go | 22 +++++++----- 3 files changed, 25 insertions(+), 54 deletions(-) diff --git a/controllers/clusters/helpers.go b/controllers/clusters/helpers.go index d9df9eab..8b3abefe 100644 --- a/controllers/clusters/helpers.go +++ b/controllers/clusters/helpers.go @@ -28,13 +28,9 @@ import ( "github.com/go-logr/logr" "github.com/hashicorp/go-version" k8scorev1 "k8s.io/api/core/v1" - v1 "k8s.io/api/core/v1" - k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/instaclustr/operator/pkg/models" @@ -137,38 +133,6 @@ var msgExternalChanges = "The k8s specification is different from Instaclustr Co "Update operations are blocked. Please check operator logs and edit the cluster spec manually, " + "so that it would corresponds to the data from Instaclustr." -// deleteDefaultUserSecret deletes the secret with default user credentials. -// It ignores NotFound error. -func deleteDefaultUserSecret( - ctx context.Context, - client client.Client, - clusterNamespacedName types.NamespacedName, -) error { - l := log.FromContext(ctx) - - l.Info("Deleting default user secret...", - "resource namespaced name", clusterNamespacedName, - ) - - secret := &v1.Secret{} - err := client.Get(ctx, types.NamespacedName{ - Name: fmt.Sprintf(models.DefaultUserSecretNameTemplate, models.DefaultUserSecretPrefix, clusterNamespacedName.Name), - Namespace: clusterNamespacedName.Namespace, - }, secret) - if err != nil { - if k8serrors.IsNotFound(err) { - l.Info("The secret for the given resource is not found, skipping...", - "resource namespaced name", clusterNamespacedName, - ) - return nil - } - - return err - } - - return client.Delete(ctx, secret) -} - // Object is a general representation of any object the operator works with type Object interface { client.Object diff --git a/controllers/clusters/kafkaconnect_controller.go b/controllers/clusters/kafkaconnect_controller.go index 029b8aea..0deea342 100644 --- a/controllers/clusters/kafkaconnect_controller.go +++ b/controllers/clusters/kafkaconnect_controller.go @@ -353,15 +353,6 @@ func (r *KafkaConnectReconciler) handleDeleteCluster(ctx context.Context, kc *v1 } } - err = deleteDefaultUserSecret(ctx, r.Client, client.ObjectKeyFromObject(kc)) - if err != nil { - l.Error(err, "Cannot delete default user secret") - r.EventRecorder.Eventf(kc, models.Warning, models.DeletionFailed, - "Deletion of the secret with default user credentials is failed. Reason: %w", err) - - return reconcile.Result{}, err - } - r.Scheduler.RemoveJob(kc.GetJobID(scheduler.SyncJob)) controllerutil.RemoveFinalizer(kc, models.DeletionFinalizer) kc.Annotations[models.ResourceStateAnnotation] = models.DeletedEvent @@ -414,6 +405,18 @@ func (r *KafkaConnectReconciler) createDefaultSecret(ctx context.Context, kc *v1 patch := kc.NewPatch() secret := newDefaultUserSecret(username, password, kc.Name, kc.Namespace) + err = controllerutil.SetOwnerReference(kc, secret, r.Scheme) + if err != nil { + l.Error(err, "Cannot set secret owner reference with default user credentials", + "cluster ID", kc.Status.ID, + ) + r.EventRecorder.Eventf(kc, models.Warning, models.SetOwnerRef, + "Setting secret owner ref with default user credentials is failed. Reason: %v", err, + ) + + return err + } + err = r.Create(ctx, secret) if err != nil { l.Error(err, "Cannot create secret with default user credentials", diff --git a/controllers/clusters/zookeeper_controller.go b/controllers/clusters/zookeeper_controller.go index c64d0bb1..07367be6 100644 --- a/controllers/clusters/zookeeper_controller.go +++ b/controllers/clusters/zookeeper_controller.go @@ -218,6 +218,19 @@ func (r *ZookeeperReconciler) createDefaultSecret(ctx context.Context, zk *v1bet patch := zk.NewPatch() secret := newDefaultUserSecret(username, password, zk.Name, zk.Namespace) + + err = controllerutil.SetOwnerReference(zk, secret, r.Scheme) + if err != nil { + l.Error(err, "Cannot set secret owner reference with default user credentials", + "cluster ID", zk.Status.ID, + ) + r.EventRecorder.Eventf(zk, models.Warning, models.SetOwnerRef, + "Setting secret owner ref with default user credentials is failed. Reason: %v", err, + ) + + return err + } + err = r.Create(ctx, secret) if err != nil { l.Error(err, "Cannot create secret with default user credentials", @@ -359,15 +372,6 @@ func (r *ZookeeperReconciler) handleDeleteCluster( } } - err = deleteDefaultUserSecret(ctx, r.Client, client.ObjectKeyFromObject(zook)) - if err != nil { - l.Error(err, "Cannot delete default user secret") - r.EventRecorder.Eventf(zook, models.Warning, models.DeletionFailed, - "Deletion of the secret with default user credentials is failed. Reason: %w", err) - - return reconcile.Result{}, err - } - r.Scheduler.RemoveJob(zook.GetJobID(scheduler.SyncJob)) controllerutil.RemoveFinalizer(zook, models.DeletionFinalizer) zook.Annotations[models.ResourceStateAnnotation] = models.DeletedEvent