@@ -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