Skip to content

Commit 43d487b

Browse files
committed
move finalizer earlier in the execution flow to handle update scenario
1 parent 9b0826e commit 43d487b

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

api/v1alpha1/intelmachine_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
const (
1212
// FreeInstanceFinalizer allows ReconcileIntelMachine to remove the instance from the Workload in the Inventory
1313
FreeInstanceFinalizer = "intelmachine.infrastructure.cluster.x-k8s.io/free-instance"
14+
// HostCleanupFinalizer - no longer used, but kept for backward compatibility
15+
HostCleanupFinalizer = "intelmachine.infrastructure.cluster.x-k8s.io/host-cleanup"
1416
// DeauthHostFinalizer allows ReconcileIntelMachine to deauthorize the host in the Inventory
1517
DeauthHostFinalizer = "intelmachine.infrastructure.cluster.x-k8s.io/deauth-host"
1618

internal/controller/intelmachine_controller.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ func (r *IntelMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request
153153
}
154154
}()
155155

156+
// Add finalizers to the IntelMachine if they are not already present.
157+
if !controllerutil.ContainsFinalizer(rc.intelMachine, infrastructurev1alpha1.FreeInstanceFinalizer) ||
158+
!controllerutil.ContainsFinalizer(rc.intelMachine, infrastructurev1alpha1.DeauthHostFinalizer) {
159+
rc.log.Info("Adding finalizers to IntelMachine", "finalizers", []string{
160+
infrastructurev1alpha1.FreeInstanceFinalizer,
161+
infrastructurev1alpha1.DeauthHostFinalizer,
162+
})
163+
// FreeInstanceFinalizer is used to remove the instance from the Workload in Inventory.
164+
controllerutil.AddFinalizer(rc.intelMachine, infrastructurev1alpha1.FreeInstanceFinalizer)
165+
// DeauthHostFinalizer is used to deauthorize the host in Inventory.
166+
controllerutil.AddFinalizer(rc.intelMachine, infrastructurev1alpha1.DeauthHostFinalizer)
167+
}
168+
156169
// Handle deleted machines
157170
if !rc.intelMachine.ObjectMeta.DeletionTimestamp.IsZero() {
158171
return ctrl.Result{}, r.reconcileDelete(rc)
@@ -162,6 +175,7 @@ func (r *IntelMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request
162175
if requeue := r.reconcileNormal(rc); requeue {
163176
return ctrl.Result{RequeueAfter: defaultRequeueAfter}, nil
164177
}
178+
165179
return ctrl.Result{}, nil
166180
}
167181

@@ -203,6 +217,11 @@ func (r *IntelMachineReconciler) reconcileDelete(rc IntelMachineReconcilerContex
203217
return errors.Wrap(err, "failed to patch IntelMachine")
204218
}
205219

220+
if controllerutil.ContainsFinalizer(rc.intelMachine, infrastructurev1alpha1.HostCleanupFinalizer) {
221+
// upgrade scenario: remove the obsolete HostCleanupFinalizer
222+
controllerutil.RemoveFinalizer(rc.intelMachine, infrastructurev1alpha1.HostCleanupFinalizer)
223+
}
224+
206225
if controllerutil.ContainsFinalizer(rc.intelMachine, infrastructurev1alpha1.FreeInstanceFinalizer) {
207226
// Remove the instance from the workload in Inventory
208227
req := inventory.DeleteInstanceFromWorkloadInput{
@@ -331,12 +350,6 @@ func (r *IntelMachineReconciler) reconcileNormal(rc IntelMachineReconcilerContex
331350
rc.intelMachine.Spec.ProviderID = &gmRes.Instance.Id
332351
rc.intelMachine.Annotations[infrastructurev1alpha1.HostIdAnnotation] = gmRes.Host.Id
333352
conditions.MarkTrue(rc.intelMachine, infrastructurev1alpha1.HostProvisionedCondition)
334-
335-
// Add finalizers.
336-
// The FreeInstanceFinalizer is removed by the IntelMachine Reconciler after the host is freed in Inventory.
337-
controllerutil.AddFinalizer(rc.intelMachine, infrastructurev1alpha1.FreeInstanceFinalizer)
338-
// The DeauthHostFinalizer is removed by the IntelMachine Reconciler after the host is deauthorized in Inventory.
339-
controllerutil.AddFinalizer(rc.intelMachine, infrastructurev1alpha1.DeauthHostFinalizer)
340353
return false
341354
}
342355

0 commit comments

Comments
 (0)