Skip to content

Commit 8ce4da5

Browse files
authored
Merge pull request #2855 from chrischdi/pr-release-1-7-cp-2846
[release-1.7] 🐛 Ensure entries for ProviderServiceAccount created in the ConfigMap are cleaned up
2 parents b7a58b7 + 1698855 commit 8ce4da5

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
@@ -98,6 +98,19 @@ func AddServiceAccountProviderControllerToManager(ctx *context.ControllerManager
9898
&source.Kind{Type: &corev1.Secret{}},
9999
handler.EnqueueRequestsFromMapFunc(r.secretToVSphereCluster),
100100
).
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+
).
101114
// Watches clusters and reconciles the vSphereCluster
102115
Watches(
103116
&source.Kind{Type: &clusterv1.Cluster{}},
@@ -195,6 +208,13 @@ func (r ServiceAccountReconciler) Reconcile(_ goctx.Context, req reconcile.Reque
195208
return reconcile.Result{}, nil
196209
}
197210

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+
198218
// We cannot proceed until we are able to access the target cluster. Until
199219
// then just return a no-op and wait for the next sync. This will occur when
200220
// 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
228248
}
229249
}
230250

251+
controllerutil.RemoveFinalizer(ctx.VSphereCluster, vmwarev1.ProviderServiceAccountFinalizer)
231252
return reconcile.Result{}, nil
232253
}
233254

@@ -506,6 +527,9 @@ func (r ServiceAccountReconciler) ensureServiceAccountConfigMap(ctx *vmwareconte
506527
if err != nil {
507528
return err
508529
}
530+
if configMap.Data == nil {
531+
configMap.Data = map[string]string{}
532+
}
509533
if valid, exist := configMap.Data[svcAccountName]; exist && valid == strconv.FormatBool(true) {
510534
// Service account name is already in the config map
511535
return nil

0 commit comments

Comments
 (0)