Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.6] 🐛 Refine finalizer handling #2116

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion controllers/vmware/test/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ var _ = Describe("Reconciliation tests", func() {
By("Expect a ResourcePolicy to exist")
rpKey := client.ObjectKey{Namespace: infraCluster.GetNamespace(), Name: infraCluster.GetName()}
resourcePolicy := &vmoprv1.VirtualMachineSetResourcePolicy{}
Expect(k8sClient.Get(ctx, rpKey, resourcePolicy)).To(Succeed())
Eventually(func() error {
return k8sClient.Get(ctx, rpKey, resourcePolicy)
}, time.Second*30).Should(Succeed())
Expect(len(resourcePolicy.Spec.ClusterModules)).To(BeEquivalentTo(2))

By("Create the CAPI Machine and wait for it to exist")
Expand Down
10 changes: 7 additions & 3 deletions controllers/vmware/vspherecluster_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func (r ClusterReconciler) Reconcile(_ goctx.Context, req ctrl.Request) (_ ctrl.
return reconcile.Result{}, nil
}

// If the VSphereCluster doesn't have our finalizer, add it.
// Requeue immediately after adding finalizer to avoid the race condition between init and delete
if !controllerutil.ContainsFinalizer(vsphereCluster, vmwarev1.ClusterFinalizer) {
controllerutil.AddFinalizer(vsphereCluster, vmwarev1.ClusterFinalizer)
return ctrl.Result{}, nil
}

// Handle non-deleted clusters
return ctrl.Result{}, r.reconcileNormal(clusterContext)
}
Expand All @@ -147,9 +154,6 @@ func (r *ClusterReconciler) reconcileDelete(ctx *vmware.ClusterContext) {
func (r *ClusterReconciler) reconcileNormal(ctx *vmware.ClusterContext) error {
ctx.Logger.Info("Reconciling vsphereCluster")

// If the vsphereCluster doesn't have our finalizer, add it.
controllerutil.AddFinalizer(ctx.VSphereCluster, vmwarev1.ClusterFinalizer)

// Get any failure domains to report back to the CAPI core controller.
failureDomains, err := r.getFailureDomains(ctx)
if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions controllers/vspherecluster_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ func (r clusterReconciler) Reconcile(_ goctx.Context, req ctrl.Request) (_ ctrl.
return r.reconcileDelete(clusterContext)
}

// If the VSphereCluster doesn't have our finalizer, add it.
// Requeue immediately after adding finalizer to avoid the race condition between init and delete
if !ctrlutil.ContainsFinalizer(vsphereCluster, infrav1.ClusterFinalizer) {
ctrlutil.AddFinalizer(vsphereCluster, infrav1.ClusterFinalizer)
return reconcile.Result{}, nil
}

// Handle non-deleted clusters
return r.reconcileNormal(clusterContext)
}
Expand Down Expand Up @@ -216,9 +223,6 @@ func (r clusterReconciler) reconcileDelete(ctx *context.ClusterContext) (reconci
func (r clusterReconciler) reconcileNormal(ctx *context.ClusterContext) (reconcile.Result, error) {
ctx.Logger.Info("Reconciling VSphereCluster")

// If the VSphereCluster doesn't have our finalizer, add it.
ctrlutil.AddFinalizer(ctx.VSphereCluster, infrav1.ClusterFinalizer)

ok, err := r.reconcileDeploymentZones(ctx)
if err != nil {
return reconcile.Result{}, err
Expand Down
9 changes: 7 additions & 2 deletions controllers/vspheredeploymentzone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ func (r vsphereDeploymentZoneReconciler) Reconcile(ctx goctx.Context, request re
return ctrl.Result{}, r.reconcileDelete(vsphereDeploymentZoneContext)
}

// If the VSphereDeploymentZone doesn't have our finalizer, add it.
// Requeue immediately after adding finalizer to avoid the race condition between init and delete
if !ctrlutil.ContainsFinalizer(vsphereDeploymentZone, infrav1.DeploymentZoneFinalizer) {
ctrlutil.AddFinalizer(vsphereDeploymentZone, infrav1.DeploymentZoneFinalizer)
return ctrl.Result{}, nil
}

return ctrl.Result{}, r.reconcileNormal(vsphereDeploymentZoneContext)
}

func (r vsphereDeploymentZoneReconciler) reconcileNormal(ctx *context.VSphereDeploymentZoneContext) error {
ctrlutil.AddFinalizer(ctx.VSphereDeploymentZone, infrav1.DeploymentZoneFinalizer)

authSession, err := r.getVCenterSession(ctx)
if err != nil {
ctx.Logger.V(4).Error(err, "unable to create session")
Expand Down
9 changes: 6 additions & 3 deletions controllers/vspheremachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ func (r machineReconciler) Reconcile(_ goctx.Context, req ctrl.Request) (_ ctrl.
return reconcile.Result{}, nil
}

// If the VSphereMachine doesn't have our finalizer, add it.
// Requeue immediately after adding finalizer to avoid the race condition between init and delete
if !ctrlutil.ContainsFinalizer(machineContext.GetVSphereMachine(), infrav1.MachineFinalizer) {
ctrlutil.AddFinalizer(machineContext.GetVSphereMachine(), infrav1.MachineFinalizer)
return reconcile.Result{}, nil
}
// Handle non-deleted machines
return r.reconcileNormal(machineContext)
}
Expand Down Expand Up @@ -289,9 +295,6 @@ func (r machineReconciler) reconcileNormal(ctx context.MachineContext) (reconcil
return reconcile.Result{}, nil
}

// If the VSphereMachine doesn't have our finalizer, add it.
ctrlutil.AddFinalizer(ctx.GetVSphereMachine(), infrav1.MachineFinalizer)

//nolint:gocritic
if r.supervisorBased {
err := r.setVMModifiers(ctx)
Expand Down
11 changes: 9 additions & 2 deletions controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ func (r vmReconciler) Reconcile(ctx goctx.Context, req ctrl.Request) (_ ctrl.Res
}
}

if vsphereVM.ObjectMeta.DeletionTimestamp.IsZero() {
// If the VSphereVM doesn't have our finalizer, add it.
// Requeue immediately to avoid the race condition between init and delete
if !ctrlutil.ContainsFinalizer(vsphereVM, infrav1.VMFinalizer) {
ctrlutil.AddFinalizer(vsphereVM, infrav1.VMFinalizer)
return reconcile.Result{}, nil
}
}

return r.reconcile(vmContext, fetchClusterModuleInput{
VSphereCluster: vsphereCluster,
Machine: machine,
Expand Down Expand Up @@ -388,8 +397,6 @@ func (r vmReconciler) reconcileNormal(ctx *context.VMContext) (reconcile.Result,
r.Logger.Info("VM is failed, won't reconcile", "namespace", ctx.VSphereVM.Namespace, "name", ctx.VSphereVM.Name)
return reconcile.Result{}, nil
}
// If the VSphereVM doesn't have our finalizer, add it.
ctrlutil.AddFinalizer(ctx.VSphereVM, infrav1.VMFinalizer)

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