Skip to content

Commit

Permalink
Merge pull request #2322 from killianmuldoon/pr-add-vsphereclusteride…
Browse files Browse the repository at this point in the history
…ntity-finalizer

🌱 Add finalizer for VSphereClusterIdentity
  • Loading branch information
k8s-ci-robot authored Sep 7, 2023
2 parents df1c3dd + af4c671 commit 2188e96
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, identity
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, identity
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 2188e96

Please sign in to comment.