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 Bohdan Siryk committed Oct 23, 2023
1 parent 26eee3c commit 970b40e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
30 changes: 30 additions & 0 deletions controllers/clusters/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package clusters
import (
"context"
"errors"
"fmt"

"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -392,6 +395,15 @@ func (r *KafkaConnectReconciler) handleDeleteCluster(ctx context.Context, kc *v1
}
}

err = r.deleteDefaultSecret(ctx, 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 Expand Up @@ -463,6 +475,24 @@ func (r *KafkaConnectReconciler) createDefaultSecret(ctx context.Context, kc *v1
return nil
}

func (r *KafkaConnectReconciler) deleteDefaultSecret(ctx context.Context, kc *v1beta1.KafkaConnect) error {
secret := &v1.Secret{}
err := r.Get(ctx, types.NamespacedName{
Name: fmt.Sprintf(models.DefaultUserSecretNameTemplate, models.DefaultUserSecretPrefix, kc.Name),
Namespace: kc.Namespace,
}, secret)

if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}

return err
}

return r.Delete(ctx, secret)
}

func (r *KafkaConnectReconciler) startClusterStatusJob(kc *v1beta1.KafkaConnect) error {
job := r.newWatchStatusJob(kc)

Expand Down
31 changes: 31 additions & 0 deletions controllers/clusters/zookeeper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package clusters
import (
"context"
"errors"
"fmt"

"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -225,6 +228,24 @@ func (r *ZookeeperReconciler) createDefaultSecret(ctx context.Context, zk *v1bet
return nil
}

func (r *ZookeeperReconciler) deleteDefaultSecret(ctx context.Context, zk *v1beta1.Zookeeper) error {
secret := &v1.Secret{}
err := r.Get(ctx, types.NamespacedName{
Name: fmt.Sprintf(models.DefaultUserSecretNameTemplate, models.DefaultUserSecretPrefix, zk.Name),
Namespace: zk.Namespace,
}, secret)

if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}

return err
}

return r.Delete(ctx, secret)
}

func (r *ZookeeperReconciler) handleUpdateCluster(
zook *v1beta1.Zookeeper,
l logr.Logger,
Expand Down Expand Up @@ -384,6 +405,16 @@ func (r *ZookeeperReconciler) handleDeleteCluster(
}
}

err = r.deleteDefaultSecret(ctx, 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 970b40e

Please sign in to comment.