Skip to content

Commit bdd5b7f

Browse files
committed
mhc: Don't set OwnerRemediated on deleting machines
1 parent d7e314f commit bdd5b7f

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

internal/controllers/machinehealthcheck/machinehealthcheck_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ func (r *Reconciler) patchUnhealthyTargets(ctx context.Context, logger logr.Logg
496496
Status: metav1.ConditionFalse,
497497
Reason: clusterv1.MachineExternallyRemediatedWaitingForRemediationV1Beta2Reason,
498498
})
499-
} else {
499+
} else if t.Machine.DeletionTimestamp.IsZero() { // Only setting the OwnerRemediated conditions when machine is not already in deletion.
500500
logger.Info("Target has failed health check, marking for remediation", "target", t.string(), "reason", condition.Reason, "message", condition.Message)
501501
// NOTE: MHC is responsible for creating MachineOwnerRemediatedCondition if missing or to trigger another remediation if the previous one is completed;
502502
// instead, if a remediation is in already progress, the remediation owner is responsible for completing the process and MHC should not overwrite the condition.

internal/controllers/machinehealthcheck/machinehealthcheck_controller_test.go

+41-5
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) {
376376
assertMachinesNotHealthy(g, mhc, 0)
377377
})
378378

379-
t.Run("it marks unhealthy machines for remediation when there is one unhealthy Machine", func(t *testing.T) {
379+
t.Run("it marks unhealthy machines for remediation when there is one unhealthy Machine and skips deleting machines", func(t *testing.T) {
380380
g := NewWithT(t)
381381
cluster := createCluster(g, ns.Name)
382382

@@ -404,7 +404,16 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) {
404404
machineLabels(mhc.Spec.Selector.MatchLabels),
405405
)
406406
defer cleanup2()
407-
machines = append(machines, unhealthyMachines...)
407+
// Unhealthy nodes and machines but already in deletion.
408+
_, unhealthyMachinesDeleting, cleanup3 := createMachinesWithNodes(g, cluster,
409+
count(1),
410+
createNodeRefForMachine(true),
411+
nodeStatus(corev1.ConditionUnknown),
412+
machineLabels(mhc.Spec.Selector.MatchLabels),
413+
machineDeleting(),
414+
)
415+
defer cleanup3()
416+
machines = append(append(machines, unhealthyMachines...), unhealthyMachinesDeleting...)
408417
targetMachines := make([]string, len(machines))
409418
for i, m := range machines {
410419
targetMachines[i] = m.Name
@@ -419,7 +428,7 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) {
419428
}
420429
return &mhc.Status
421430
}).Should(MatchMachineHealthCheckStatus(&clusterv1.MachineHealthCheckStatus{
422-
ExpectedMachines: 3,
431+
ExpectedMachines: 4,
423432
CurrentHealthy: 2,
424433
RemediationsAllowed: 2,
425434
ObservedGeneration: 1,
@@ -441,7 +450,7 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) {
441450
},
442451
}))
443452

444-
assertMachinesNotHealthy(g, mhc, 1)
453+
assertMachinesNotHealthy(g, mhc, 2)
445454
assertMachinesOwnerRemediated(g, mhc, 1)
446455
})
447456

@@ -2450,6 +2459,8 @@ type machinesWithNodes struct {
24502459
labels map[string]string
24512460
failureReason string
24522461
failureMessage string
2462+
finalizers []string
2463+
deleted bool
24532464
}
24542465

24552466
type machineWithNodesOption func(m *machinesWithNodes)
@@ -2496,6 +2507,13 @@ func machineFailureMessage(s string) machineWithNodesOption {
24962507
}
24972508
}
24982509

2510+
func machineDeleting() machineWithNodesOption {
2511+
return func(m *machinesWithNodes) {
2512+
m.finalizers = append(m.finalizers, "test.cluster.io/deleting")
2513+
m.deleted = true
2514+
}
2515+
}
2516+
24992517
func createMachinesWithNodes(
25002518
g *WithT,
25012519
c *clusterv1.Cluster,
@@ -2535,9 +2553,18 @@ func createMachinesWithNodes(
25352553
Name: infraMachine.GetName(),
25362554
Namespace: infraMachine.GetNamespace(),
25372555
}
2556+
if len(o.finalizers) > 0 {
2557+
machine.Finalizers = o.finalizers
2558+
}
25382559
g.Expect(env.Create(ctx, machine)).To(Succeed())
25392560
fmt.Printf("machine created: %s\n", machine.GetName())
25402561

2562+
// Set deletiontimestamp before updating status to ensure its not reconciled
2563+
// without having the deletionTimestamp set.
2564+
if o.deleted {
2565+
g.Expect(env.Delete(ctx, machine)).To(Succeed())
2566+
}
2567+
25412568
// Before moving on we want to ensure that the machine has a valid
25422569
// status. That is, LastUpdated should not be nil.
25432570
g.Eventually(func() *metav1.Time {
@@ -2618,7 +2645,16 @@ func createMachinesWithNodes(
26182645
}
26192646
}
26202647
for _, m := range machines {
2621-
g.Expect(env.Delete(ctx, m)).To(Succeed())
2648+
if m.DeletionTimestamp.IsZero() {
2649+
g.Expect(env.Delete(ctx, m)).To(Succeed())
2650+
}
2651+
if len(m.Finalizers) > 1 {
2652+
g.Expect(env.Get(ctx, util.ObjectKey(m), m)).To(Succeed())
2653+
machinePatchHelper, err := patch.NewHelper(m, env.Client)
2654+
g.Expect(err).ToNot(HaveOccurred())
2655+
m.Finalizers = nil
2656+
g.Expect(machinePatchHelper.Patch(ctx, m)).To(Succeed())
2657+
}
26222658
}
26232659
for _, im := range infraMachines {
26242660
if err := env.Delete(ctx, im); !apierrors.IsNotFound(err) {

0 commit comments

Comments
 (0)