Skip to content

Commit

Permalink
issue-728, default user secret already exists error
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksiienkoMykyta committed Mar 5, 2024
1 parent 2b72599 commit 08588ce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 54 deletions.
36 changes: 0 additions & 36 deletions controllers/clusters/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions controllers/clusters/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
22 changes: 13 additions & 9 deletions controllers/clusters/zookeeper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 08588ce

Please sign in to comment.