Skip to content

Commit 2188e96

Browse files
authored
Merge pull request #2322 from killianmuldoon/pr-add-vsphereclusteridentity-finalizer
🌱 Add finalizer for VSphereClusterIdentity
2 parents df1c3dd + af4c671 commit 2188e96

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

apis/v1beta1/vsphereclusteridentity_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
)
2424

2525
const (
26-
SecretIdentitySetFinalizer = "vspherecluster/infrastructure.cluster.x-k8s.io"
26+
SecretIdentitySetFinalizer = "vspherecluster/infrastructure.cluster.x-k8s.io"
27+
VSphereClusterIdentityFinalizer = "vsphereclusteridentity/infrastructure.cluster.x-k8s.io"
2728
)
2829

2930
type VSphereClusterIdentitySpec struct {

controllers/vsphereclusteridentity_controller.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.
119119
return ctrl.Result{}, r.reconcileDelete(ctx, identity)
120120
}
121121

122+
// Add a finalizer and requeue to ensure that the secret is deleted when the identity is deleted.
123+
if !ctrlutil.ContainsFinalizer(identity, infrav1.VSphereClusterIdentityFinalizer) {
124+
ctrlutil.AddFinalizer(identity, infrav1.VSphereClusterIdentityFinalizer)
125+
return reconcile.Result{}, nil
126+
}
127+
122128
// fetch secret
123129
secret := &corev1.Secret{}
124130
secretKey := client.ObjectKey{
@@ -171,6 +177,8 @@ func (r clusterIdentityReconciler) reconcileDelete(ctx context.Context, identity
171177
err := r.Client.Get(ctx, secretKey, secret)
172178
if err != nil {
173179
if apierrors.IsNotFound(err) {
180+
// The secret no longer exists. Remove the finalizer from the VSphereClusterIdentity.
181+
ctrlutil.RemoveFinalizer(identity, infrav1.VSphereClusterIdentityFinalizer)
174182
return nil
175183
}
176184
return err
@@ -185,5 +193,10 @@ func (r clusterIdentityReconciler) reconcileDelete(ctx context.Context, identity
185193
if err := r.Client.Update(ctx, secret); err != nil {
186194
return err
187195
}
188-
return r.Client.Delete(ctx, secret)
196+
if err := r.Client.Delete(ctx, secret); err != nil {
197+
return err
198+
}
199+
// Remove the finalizer from the identity as all cleanup is complete.
200+
ctrlutil.RemoveFinalizer(identity, infrav1.VSphereClusterIdentityFinalizer)
201+
return nil
189202
}

0 commit comments

Comments
 (0)