Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Add finalizer for VSphereClusterIdentity #2322

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
killianmuldoon marked this conversation as resolved.
Show resolved Hide resolved
return err
}
// Remove the finalizer from the identity as all cleanup is complete.
ctrlutil.RemoveFinalizer(identity, infrav1.VSphereClusterIdentityFinalizer)
return nil
}