Skip to content

Commit

Permalink
issue-599, deleting of the default secret of zookeeper and kafka conn…
Browse files Browse the repository at this point in the history
…ect resources was implemented
  • Loading branch information
Bohdan Siryk authored and testisnullus committed Oct 26, 2023
1 parent b5b1bb0 commit efeef06
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
37 changes: 37 additions & 0 deletions controllers/clusters/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ limitations under the License.
package clusters

import (
"context"
"encoding/json"
"fmt"
"sort"

"github.com/hashicorp/go-version"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/strings/slices"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/instaclustr/operator/apis/clusters/v1beta1"
"github.com/instaclustr/operator/pkg/models"
Expand Down Expand Up @@ -179,3 +184,35 @@ var msgExternalChanges = "The k8s specification is different from Instaclustr Co
"so that it would corresponds to the data from Instaclustr."

var msgSpecStillNoMatch = "k8s resource specification still doesn't match with data on the Instaclustr Console. Double check the difference."

// 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)
}
9 changes: 9 additions & 0 deletions controllers/clusters/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,15 @@ 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.StatusChecker))
controllerutil.RemoveFinalizer(kc, models.DeletionFinalizer)
kc.Annotations[models.ResourceStateAnnotation] = models.DeletedEvent
Expand Down
9 changes: 9 additions & 0 deletions controllers/clusters/zookeeper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,15 @@ 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.StatusChecker))
controllerutil.RemoveFinalizer(zook, models.DeletionFinalizer)
zook.Annotations[models.ResourceStateAnnotation] = models.DeletedEvent
Expand Down

0 comments on commit efeef06

Please sign in to comment.