Skip to content

Commit

Permalink
Add finalizer for VSphereClusterIdentity
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <kmuldoon@vmware.com>
  • Loading branch information
killianmuldoon committed Sep 5, 2023
1 parent e9b09f6 commit 5f25d90
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion apis/v1beta1/vsphereclusteridentity_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
)

const (
SecretIdentitySetFinalizer = "vspherecluster/infrastructure.cluster.x-k8s.io"
SecretIdentitySetFinalizer = "vspherecluster/infrastructure.cluster.x-k8s.io"
VSphereClusterIdentityFinalizer = "vsphereclusteridentity/infrastructure.cluster.x-k8s.io"
)

type VSphereClusterIdentitySpec struct {
Expand Down
17 changes: 16 additions & 1 deletion controllers/vsphereclusteridentity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func (r clusterIdentityReconciler) Reconcile(ctx _context.Context, req reconcile
}
}()

// Add a finalizer and requeque to ensure that the secret is deleted when the identity is deleted.
if !ctrlutil.ContainsFinalizer(identity, infrav1.VSphereClusterIdentityFinalizer) {
ctrlutil.AddFinalizer(identity, infrav1.VSphereClusterIdentityFinalizer)
return reconcile.Result{Requeue: true}, nil
}

if !identity.DeletionTimestamp.IsZero() {
return ctrl.Result{}, r.reconcileDelete(ctx, identity)
}
Expand Down Expand Up @@ -171,6 +177,8 @@ func (r clusterIdentityReconciler) reconcileDelete(ctx _context.Context, identit
err := r.Client.Get(ctx, secretKey, secret)
if err != nil {
if apierrors.IsNotFound(err) {
// The secret no longer exists. Remove the finalizer from the VSphereClusterIdentity.
ctrlutil.RemoveFinalizer(identity, infrav1.VSphereClusterIdentityFinalizer)
return nil
}
return err
Expand All @@ -181,9 +189,16 @@ func (r clusterIdentityReconciler) reconcileDelete(ctx _context.Context, identit
if ctrlutil.ContainsFinalizer(secret, legacyIdentityFinalizer) {
ctrlutil.RemoveFinalizer(secret, legacyIdentityFinalizer)
}

// TODO (killianmuldoon): Consider whether we should have a finalizer on the secret and, if so, whether this finalizer should be renamed.
ctrlutil.RemoveFinalizer(secret, infrav1.SecretIdentitySetFinalizer)
if err := r.Client.Update(ctx, secret); err != nil {
return err
}
return r.Client.Delete(ctx, secret)
if err := r.Client.Delete(ctx, secret); err != nil {
return err
}
// Remove the finalizer from the identity as all cleanup is complete.
ctrlutil.RemoveFinalizer(identity, infrav1.VSphereClusterIdentityFinalizer)
return nil
}

0 comments on commit 5f25d90

Please sign in to comment.