@@ -100,6 +100,19 @@ func AddServiceAccountProviderControllerToManager(ctx *context.ControllerManager
100
100
& corev1.Secret {},
101
101
handler .EnqueueRequestsFromMapFunc (r .secretToVSphereCluster ),
102
102
).
103
+ Watches (
104
+ & vmwarev1.VSphereCluster {},
105
+ handler .EnqueueRequestsFromMapFunc (func (ctx goctx.Context , o client.Object ) []reconcile.Request {
106
+ return []reconcile.Request {
107
+ {
108
+ NamespacedName : types.NamespacedName {
109
+ Namespace : o .GetNamespace (),
110
+ Name : o .GetName (),
111
+ },
112
+ },
113
+ }
114
+ }),
115
+ ).
103
116
// Watches clusters and reconciles the vSphereCluster
104
117
Watches (
105
118
& clusterv1.Cluster {},
@@ -198,6 +211,13 @@ func (r ServiceAccountReconciler) Reconcile(_ goctx.Context, req reconcile.Reque
198
211
return reconcile.Result {}, nil
199
212
}
200
213
214
+ // Add finalizer first if not set to avoid the race condition between init and delete.
215
+ // Note: Finalizers in general can only be added when the deletionTimestamp is not set.
216
+ if ! controllerutil .ContainsFinalizer (clusterContext .VSphereCluster , vmwarev1 .ProviderServiceAccountFinalizer ) {
217
+ controllerutil .AddFinalizer (clusterContext .VSphereCluster , vmwarev1 .ProviderServiceAccountFinalizer )
218
+ return ctrl.Result {}, nil
219
+ }
220
+
201
221
// We cannot proceed until we are able to access the target cluster. Until
202
222
// then just return a no-op and wait for the next sync. This will occur when
203
223
// the Cluster's status is updated with a reference to the secret that has
@@ -235,6 +255,7 @@ func (r ServiceAccountReconciler) ReconcileDelete(ctx *vmwarecontext.ClusterCont
235
255
}
236
256
}
237
257
258
+ controllerutil .RemoveFinalizer (ctx .VSphereCluster , vmwarev1 .ProviderServiceAccountFinalizer )
238
259
return reconcile.Result {}, nil
239
260
}
240
261
@@ -513,6 +534,9 @@ func (r ServiceAccountReconciler) ensureServiceAccountConfigMap(ctx *vmwareconte
513
534
if err != nil {
514
535
return err
515
536
}
537
+ if configMap .Data == nil {
538
+ configMap .Data = map [string ]string {}
539
+ }
516
540
if valid , exist := configMap .Data [svcAccountName ]; exist && valid == strconv .FormatBool (true ) {
517
541
// Service account name is already in the config map
518
542
return nil
0 commit comments