Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set owner ref for default user #735

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading