Skip to content

Commit bfa39e3

Browse files
committed
Refine finalizer handling
- Add finalizer if not existed - Requeue immediately after adding finalizer to avoid the race condition between init and delete Signed-off-by: Gong Zhang <[email protected]>
1 parent 52e1547 commit bfa39e3

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

controllers/vmware/test/controllers_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,9 @@ var _ = Describe("Reconciliation tests", func() {
417417
By("Expect a ResourcePolicy to exist")
418418
rpKey := client.ObjectKey{Namespace: infraCluster.GetNamespace(), Name: infraCluster.GetName()}
419419
resourcePolicy := &vmoprv1.VirtualMachineSetResourcePolicy{}
420-
Expect(k8sClient.Get(ctx, rpKey, resourcePolicy)).To(Succeed())
420+
Eventually(func() error {
421+
return k8sClient.Get(ctx, rpKey, resourcePolicy)
422+
}, time.Second*30).Should(Succeed())
421423
Expect(len(resourcePolicy.Spec.ClusterModules)).To(BeEquivalentTo(2))
422424

423425
By("Create the CAPI Machine and wait for it to exist")

controllers/vmware/vspherecluster_reconciler.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ func (r ClusterReconciler) Reconcile(_ goctx.Context, req ctrl.Request) (_ ctrl.
121121
return reconcile.Result{}, nil
122122
}
123123

124+
// If the VSphereCluster doesn't have our finalizer, add it.
125+
// Requeue immediately after adding finalizer to avoid the race condition between init and delete
126+
if !controllerutil.ContainsFinalizer(vsphereCluster, vmwarev1.ClusterFinalizer) {
127+
controllerutil.AddFinalizer(vsphereCluster, vmwarev1.ClusterFinalizer)
128+
return ctrl.Result{}, nil
129+
}
130+
124131
// Handle non-deleted clusters
125132
return ctrl.Result{}, r.reconcileNormal(clusterContext)
126133
}
@@ -147,9 +154,6 @@ func (r *ClusterReconciler) reconcileDelete(ctx *vmware.ClusterContext) {
147154
func (r *ClusterReconciler) reconcileNormal(ctx *vmware.ClusterContext) error {
148155
ctx.Logger.Info("Reconciling vsphereCluster")
149156

150-
// If the vsphereCluster doesn't have our finalizer, add it.
151-
controllerutil.AddFinalizer(ctx.VSphereCluster, vmwarev1.ClusterFinalizer)
152-
153157
// Get any failure domains to report back to the CAPI core controller.
154158
failureDomains, err := r.getFailureDomains(ctx)
155159
if err != nil {

controllers/vspherecluster_reconciler.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ func (r clusterReconciler) Reconcile(_ goctx.Context, req ctrl.Request) (_ ctrl.
127127
return r.reconcileDelete(clusterContext)
128128
}
129129

130+
// If the VSphereCluster doesn't have our finalizer, add it.
131+
// Requeue immediately after adding finalizer to avoid the race condition between init and delete
132+
if !ctrlutil.ContainsFinalizer(vsphereCluster, infrav1.ClusterFinalizer) {
133+
ctrlutil.AddFinalizer(vsphereCluster, infrav1.ClusterFinalizer)
134+
return reconcile.Result{}, nil
135+
}
136+
130137
// Handle non-deleted clusters
131138
return r.reconcileNormal(clusterContext)
132139
}
@@ -217,9 +224,6 @@ func (r clusterReconciler) reconcileDelete(ctx *context.ClusterContext) (reconci
217224
func (r clusterReconciler) reconcileNormal(ctx *context.ClusterContext) (reconcile.Result, error) {
218225
ctx.Logger.Info("Reconciling VSphereCluster")
219226

220-
// If the VSphereCluster doesn't have our finalizer, add it.
221-
ctrlutil.AddFinalizer(ctx.VSphereCluster, infrav1.ClusterFinalizer)
222-
223227
ok, err := r.reconcileDeploymentZones(ctx)
224228
if err != nil {
225229
return reconcile.Result{}, err

controllers/vspheredeploymentzone_controller.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,17 @@ func (r vsphereDeploymentZoneReconciler) Reconcile(ctx goctx.Context, request re
147147
return ctrl.Result{}, r.reconcileDelete(vsphereDeploymentZoneContext)
148148
}
149149

150+
// If the VSphereDeploymentZone doesn't have our finalizer, add it.
151+
// Requeue immediately after adding finalizer to avoid the race condition between init and delete
152+
if !ctrlutil.ContainsFinalizer(vsphereDeploymentZone, infrav1.DeploymentZoneFinalizer) {
153+
ctrlutil.AddFinalizer(vsphereDeploymentZone, infrav1.DeploymentZoneFinalizer)
154+
return ctrl.Result{}, nil
155+
}
156+
150157
return ctrl.Result{}, r.reconcileNormal(vsphereDeploymentZoneContext)
151158
}
152159

153160
func (r vsphereDeploymentZoneReconciler) reconcileNormal(ctx *context.VSphereDeploymentZoneContext) error {
154-
ctrlutil.AddFinalizer(ctx.VSphereDeploymentZone, infrav1.DeploymentZoneFinalizer)
155-
156161
authSession, err := r.getVCenterSession(ctx)
157162
if err != nil {
158163
ctx.Logger.V(4).Error(err, "unable to create session")

controllers/vspheremachine_controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ func (r *machineReconciler) Reconcile(_ goctx.Context, req ctrl.Request) (_ ctrl
255255
return reconcile.Result{}, nil
256256
}
257257

258+
// If the VSphereMachine doesn't have our finalizer, add it.
259+
// Requeue immediately after adding finalizer to avoid the race condition between init and delete
260+
if !ctrlutil.ContainsFinalizer(machineContext.GetVSphereMachine(), infrav1.MachineFinalizer) {
261+
ctrlutil.AddFinalizer(machineContext.GetVSphereMachine(), infrav1.MachineFinalizer)
262+
return reconcile.Result{}, nil
263+
}
258264
// Handle non-deleted machines
259265
return r.reconcileNormal(machineContext)
260266
}
@@ -289,9 +295,6 @@ func (r *machineReconciler) reconcileNormal(ctx context.MachineContext) (reconci
289295
return reconcile.Result{}, nil
290296
}
291297

292-
// If the VSphereMachine doesn't have our finalizer, add it.
293-
ctrlutil.AddFinalizer(ctx.GetVSphereMachine(), infrav1.MachineFinalizer)
294-
295298
//nolint:gocritic
296299
if r.supervisorBased {
297300
err := r.setVMModifiers(ctx)

controllers/vspherevm_controller.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,15 @@ func (r vmReconciler) Reconcile(ctx goctx.Context, req ctrl.Request) (_ ctrl.Res
279279
}
280280
}
281281

282+
if vsphereVM.ObjectMeta.DeletionTimestamp.IsZero() {
283+
// If the VSphereVM doesn't have our finalizer, add it.
284+
// Requeue immediately to avoid the race condition between init and delete
285+
if !ctrlutil.ContainsFinalizer(vsphereVM, infrav1.VMFinalizer) {
286+
ctrlutil.AddFinalizer(vsphereVM, infrav1.VMFinalizer)
287+
return reconcile.Result{}, nil
288+
}
289+
}
290+
282291
return r.reconcile(vmContext, fetchClusterModuleInput{
283292
VSphereCluster: vsphereCluster,
284293
Machine: machine,
@@ -371,8 +380,6 @@ func (r vmReconciler) reconcileNormal(ctx *context.VMContext) (reconcile.Result,
371380
r.Logger.Info("VM is failed, won't reconcile", "namespace", ctx.VSphereVM.Namespace, "name", ctx.VSphereVM.Name)
372381
return reconcile.Result{}, nil
373382
}
374-
// If the VSphereVM doesn't have our finalizer, add it.
375-
ctrlutil.AddFinalizer(ctx.VSphereVM, infrav1.VMFinalizer)
376383

377384
if r.isWaitingForStaticIPAllocation(ctx) {
378385
conditions.MarkFalse(ctx.VSphereVM, infrav1.VMProvisionedCondition, infrav1.WaitingForStaticIPAllocationReason, clusterv1.ConditionSeverityInfo, "")

0 commit comments

Comments
 (0)