Skip to content

Commit

Permalink
Reorder VSphereDeploymentZone reconcile to fix deletion
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <[email protected]>
  • Loading branch information
killianmuldoon committed Sep 12, 2023
1 parent c154905 commit 7eb3f77
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions controllers/vspheredeploymentzone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ func (r vsphereDeploymentZoneReconciler) Reconcile(ctx context.Context, request
return reconcile.Result{}, err
}

failureDomain := &infrav1.VSphereFailureDomain{}
failureDomainKey := client.ObjectKey{Name: vsphereDeploymentZone.Spec.FailureDomain}
if err := r.Client.Get(ctx, failureDomainKey, failureDomain); err != nil {
if apierrors.IsNotFound(err) {
logr.V(4).Info("Failure Domain not found, won't reconcile", "key", failureDomainKey)
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}

patchHelper, err := patch.NewHelper(vsphereDeploymentZone, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrapf(
Expand All @@ -132,7 +122,6 @@ func (r vsphereDeploymentZoneReconciler) Reconcile(ctx context.Context, request
vsphereDeploymentZoneContext := &capvcontext.VSphereDeploymentZoneContext{
ControllerContext: r.ControllerContext,
VSphereDeploymentZone: vsphereDeploymentZone,
VSphereFailureDomain: failureDomain,
Logger: logr,
PatchHelper: patchHelper,
}
Expand All @@ -144,11 +133,23 @@ func (r vsphereDeploymentZoneReconciler) Reconcile(ctx context.Context, request
logr.Error(err, "patch failed", "vsphereDeploymentZone", vsphereDeploymentZoneContext.String())
}
}()

if !vsphereDeploymentZone.DeletionTimestamp.IsZero() {
return ctrl.Result{}, r.reconcileDelete(vsphereDeploymentZoneContext)
}

// Fetch the VSphereFailureDomain after checking if this VSphereDeploymentZone is being deleted. Otherwise the
// vsphereDeploymentZone can get stuck in deleting state.
failureDomain := &infrav1.VSphereFailureDomain{}
failureDomainKey := client.ObjectKey{Name: vsphereDeploymentZone.Spec.FailureDomain}
if err := r.Client.Get(ctx, failureDomainKey, failureDomain); err != nil {
if apierrors.IsNotFound(err) {
logr.V(4).Info("Failure Domain not found, won't reconcile", "key", failureDomainKey)
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
vsphereDeploymentZoneContext.VSphereFailureDomain = failureDomain

// 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) {
Expand Down

0 comments on commit 7eb3f77

Please sign in to comment.