Skip to content

Commit c0cf039

Browse files
authored
NO-ISSUE: Prevent further reconciling after removing AgentMachine finalizer (#214)
When the AgentMachine's finalizer is removed, the deletionTimestamp is also removed. Previously, anytime the finalizer is successfully removed, we allow the reconciliation to continue, which is problematic since the AgentMachine will have no indication that it is actually being deleted. This leads to problems like when scaling down a NodePool, the AgentMachine controller will unbind the Agents, but then re-add the AgentMachineRef label to those Agents later on in the reconcile logic. The Agents will then not be able to be bound again unless the AgentMachineRef label is removed manually. This change ensures that whenever the AgentMachine has successfully had its finalizer removed that we end the reconciliation cycle.
1 parent 3708b6f commit c0cf039

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

controllers/agentmachine_controller.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (r *AgentMachineReconciler) handleDeletionHook(ctx context.Context, log log
218218
log.Error(err)
219219
return &ctrl.Result{}, err
220220
}
221-
return nil, nil
221+
return &ctrl.Result{}, nil
222222
}
223223

224224
// return if the machine is not waiting on this hook
@@ -236,7 +236,7 @@ func (r *AgentMachineReconciler) handleDeletionHook(ctx context.Context, log log
236236
log.Error(err)
237237
return &ctrl.Result{}, err
238238
}
239-
return nil, nil
239+
return &ctrl.Result{}, nil
240240
}
241241

242242
log.Infof("Machine is waiting on delete hook %s", clusterv1.PreTerminateDeleteHookSucceededCondition)
@@ -246,7 +246,7 @@ func (r *AgentMachineReconciler) handleDeletionHook(ctx context.Context, log log
246246
log.Error(err)
247247
return &ctrl.Result{}, err
248248
}
249-
return nil, nil
249+
return &ctrl.Result{}, nil
250250
}
251251

252252
agent, err := r.getAgent(ctx, log, agentMachine)
@@ -257,7 +257,7 @@ func (r *AgentMachineReconciler) handleDeletionHook(ctx context.Context, log log
257257
log.Error(hookErr)
258258
return &ctrl.Result{}, hookErr
259259
}
260-
return nil, nil
260+
return &ctrl.Result{}, nil
261261
} else {
262262
log.WithError(err).Errorf("Failed to get agent %s", agentMachine.Status.AgentRef.Name)
263263
return &ctrl.Result{}, err
@@ -294,12 +294,11 @@ func (r *AgentMachineReconciler) handleDeletionHook(ctx context.Context, log log
294294
log.Error(err)
295295
return &ctrl.Result{}, err
296296
}
297+
return &ctrl.Result{}, nil
297298
} else {
298299
log.Infof("Waiting for agent %s to reboot into discovery", agent.Name)
299300
return &ctrl.Result{RequeueAfter: 5 * time.Second}, nil
300301
}
301-
302-
return nil, nil
303302
}
304303

305304
func (r *AgentMachineReconciler) getAgentCluster(ctx context.Context, log logrus.FieldLogger, machine *clusterv1.Machine) (*capiproviderv1.AgentCluster, error) {

controllers/agentmachine_controller_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ var _ = Describe("agentmachine reconcile", func() {
585585
Expect(machine.GetAnnotations()).ToNot(HaveKey(machineDeleteHookName))
586586
Expect(c.Get(ctx, types.NamespacedName{Namespace: testNamespace, Name: "agentMachine-1"}, agentMachine)).To(Succeed())
587587
Expect(controllerutil.ContainsFinalizer(agentMachine, AgentMachineFinalizerName)).To(BeFalse())
588+
Expect(c.Get(ctx, types.NamespacedName{Namespace: agent.Namespace, Name: agent.Name}, agent)).To(Succeed())
589+
Expect(agent.Labels).NotTo(HaveKey(AgentMachineRefLabelKey))
588590
})
589591

590592
It("removes the delete hook and finalizer when the agent reaches unbinding pending user action", func() {
@@ -599,6 +601,8 @@ var _ = Describe("agentmachine reconcile", func() {
599601
Expect(machine.GetAnnotations()).ToNot(HaveKey(machineDeleteHookName))
600602
Expect(c.Get(ctx, types.NamespacedName{Namespace: testNamespace, Name: "agentMachine-1"}, agentMachine)).To(Succeed())
601603
Expect(controllerutil.ContainsFinalizer(agentMachine, AgentMachineFinalizerName)).To(BeFalse())
604+
Expect(c.Get(ctx, types.NamespacedName{Namespace: agent.Namespace, Name: agent.Name}, agent)).To(Succeed())
605+
Expect(agent.Labels).NotTo(HaveKey(AgentMachineRefLabelKey))
602606
})
603607
})
604608
Context("pausing agentmachine", func() {

0 commit comments

Comments
 (0)