@@ -119,6 +119,12 @@ func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.
119
119
return ctrl.Result {}, r .reconcileDelete (ctx , identity )
120
120
}
121
121
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
+
122
128
// fetch secret
123
129
secret := & corev1.Secret {}
124
130
secretKey := client.ObjectKey {
@@ -171,6 +177,8 @@ func (r clusterIdentityReconciler) reconcileDelete(ctx context.Context, identity
171
177
err := r .Client .Get (ctx , secretKey , secret )
172
178
if err != nil {
173
179
if apierrors .IsNotFound (err ) {
180
+ // The secret no longer exists. Remove the finalizer from the VSphereClusterIdentity.
181
+ ctrlutil .RemoveFinalizer (identity , infrav1 .VSphereClusterIdentityFinalizer )
174
182
return nil
175
183
}
176
184
return err
@@ -185,5 +193,10 @@ func (r clusterIdentityReconciler) reconcileDelete(ctx context.Context, identity
185
193
if err := r .Client .Update (ctx , secret ); err != nil {
186
194
return err
187
195
}
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
189
202
}
0 commit comments