Skip to content

Commit f5c3177

Browse files
authored
Merge pull request #2850 from chrischdi/pr-release-1-8-cp-2846
[release-1.8] 🐛 Ensure entries for ProviderServiceAccount created in the ConfigMap are cleaned up
2 parents b4f517c + cae6e7e commit f5c3177

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

apis/vmware/v1beta1/vspherecluster_types.go

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const (
2727
// resources associated with VSphereCluster before removing it from the
2828
// API server.
2929
ClusterFinalizer = "vspherecluster.vmware.infrastructure.cluster.x-k8s.io"
30+
31+
// ProviderServiceAccountFinalizer allows ServiceAccountReconciler to clean up service accounts
32+
// resources associated with VSphereCluster from the SERVICE_ACCOUNTS_CM (service accounts ConfigMap).
33+
ProviderServiceAccountFinalizer = "providerserviceaccount.vmware.infrastructure.cluster.x-k8s.io"
3034
)
3135

3236
// VSphereClusterSpec defines the desired state of VSphereCluster

controllers/serviceaccount_controller.go

+24
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ func AddServiceAccountProviderControllerToManager(ctx *context.ControllerManager
100100
&corev1.Secret{},
101101
handler.EnqueueRequestsFromMapFunc(r.secretToVSphereCluster),
102102
).
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+
).
103116
// Watches clusters and reconciles the vSphereCluster
104117
Watches(
105118
&clusterv1.Cluster{},
@@ -198,6 +211,13 @@ func (r ServiceAccountReconciler) Reconcile(_ goctx.Context, req reconcile.Reque
198211
return reconcile.Result{}, nil
199212
}
200213

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+
201221
// We cannot proceed until we are able to access the target cluster. Until
202222
// then just return a no-op and wait for the next sync. This will occur when
203223
// 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
235255
}
236256
}
237257

258+
controllerutil.RemoveFinalizer(ctx.VSphereCluster, vmwarev1.ProviderServiceAccountFinalizer)
238259
return reconcile.Result{}, nil
239260
}
240261

@@ -513,6 +534,9 @@ func (r ServiceAccountReconciler) ensureServiceAccountConfigMap(ctx *vmwareconte
513534
if err != nil {
514535
return err
515536
}
537+
if configMap.Data == nil {
538+
configMap.Data = map[string]string{}
539+
}
516540
if valid, exist := configMap.Data[svcAccountName]; exist && valid == strconv.FormatBool(true) {
517541
// Service account name is already in the config map
518542
return nil

0 commit comments

Comments
 (0)