@@ -98,6 +98,19 @@ func AddServiceAccountProviderControllerToManager(ctx *context.ControllerManager
98
98
& source.Kind {Type : & corev1.Secret {}},
99
99
handler .EnqueueRequestsFromMapFunc (r .secretToVSphereCluster ),
100
100
).
101
+ Watches (
102
+ & source.Kind {Type : & vmwarev1.VSphereCluster {}},
103
+ handler .EnqueueRequestsFromMapFunc (func (o client.Object ) []reconcile.Request {
104
+ return []reconcile.Request {
105
+ {
106
+ NamespacedName : types.NamespacedName {
107
+ Namespace : o .GetNamespace (),
108
+ Name : o .GetName (),
109
+ },
110
+ },
111
+ }
112
+ }),
113
+ ).
101
114
// Watches clusters and reconciles the vSphereCluster
102
115
Watches (
103
116
& source.Kind {Type : & clusterv1.Cluster {}},
@@ -195,6 +208,13 @@ func (r ServiceAccountReconciler) Reconcile(_ goctx.Context, req reconcile.Reque
195
208
return reconcile.Result {}, nil
196
209
}
197
210
211
+ // Add finalizer first if not set to avoid the race condition between init and delete.
212
+ // Note: Finalizers in general can only be added when the deletionTimestamp is not set.
213
+ if ! controllerutil .ContainsFinalizer (clusterContext .VSphereCluster , vmwarev1 .ProviderServiceAccountFinalizer ) {
214
+ controllerutil .AddFinalizer (clusterContext .VSphereCluster , vmwarev1 .ProviderServiceAccountFinalizer )
215
+ return ctrl.Result {}, nil
216
+ }
217
+
198
218
// We cannot proceed until we are able to access the target cluster. Until
199
219
// then just return a no-op and wait for the next sync. This will occur when
200
220
// the Cluster's status is updated with a reference to the secret that has
@@ -228,6 +248,7 @@ func (r ServiceAccountReconciler) ReconcileDelete(ctx *vmwarecontext.ClusterCont
228
248
}
229
249
}
230
250
251
+ controllerutil .RemoveFinalizer (ctx .VSphereCluster , vmwarev1 .ProviderServiceAccountFinalizer )
231
252
return reconcile.Result {}, nil
232
253
}
233
254
@@ -506,6 +527,9 @@ func (r ServiceAccountReconciler) ensureServiceAccountConfigMap(ctx *vmwareconte
506
527
if err != nil {
507
528
return err
508
529
}
530
+ if configMap .Data == nil {
531
+ configMap .Data = map [string ]string {}
532
+ }
509
533
if valid , exist := configMap .Data [svcAccountName ]; exist && valid == strconv .FormatBool (true ) {
510
534
// Service account name is already in the config map
511
535
return nil
0 commit comments