Skip to content

Commit

Permalink
Add finalizer for VSphereClusterIdentity
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <[email protected]>
  • Loading branch information
killianmuldoon committed Sep 7, 2023
1 parent e9b09f6 commit af4c671
Show file tree
Hide file tree
Showing 2 changed files with 16 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
15 changes: 14 additions & 1 deletion controllers/vsphereclusteridentity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ func (r clusterIdentityReconciler) Reconcile(ctx _context.Context, req reconcile
return ctrl.Result{}, r.reconcileDelete(ctx, identity)
}

// Add a finalizer and requeue 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{}, nil
}

// fetch secret
secret := &corev1.Secret{}
secretKey := client.ObjectKey{
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 @@ -185,5 +193,10 @@ func (r clusterIdentityReconciler) reconcileDelete(ctx _context.Context, identit
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 af4c671

Please sign in to comment.