Skip to content

Commit 28fbfbb

Browse files
authored
Merge pull request #10589 from davidvossel/nodedrain-during-delete
🌱 Propagate timeout fields from MachineSet to Machine during Machine deletion
2 parents 9cb5d3c + da2b7c8 commit 28fbfbb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

internal/controllers/machineset/machineset_controller.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,24 @@ func (r *Reconciler) syncMachines(ctx context.Context, machineSet *clusterv1.Mac
364364
log := ctrl.LoggerFrom(ctx)
365365
for i := range machines {
366366
m := machines[i]
367-
// If the machine is already being deleted, we don't need to update it.
367+
// If the machine is already being deleted, we only need to sync
368+
// the subset of fields that impact tearing down a machine
368369
if !m.DeletionTimestamp.IsZero() {
370+
patchHelper, err := patch.NewHelper(m, r.Client)
371+
if err != nil {
372+
return errors.Wrapf(err, "failed to generate patch for Machine %q", klog.KObj(m))
373+
}
374+
375+
// Set all other in-place mutable fields that impact the ability to tear down existing machines.
376+
m.Spec.NodeDrainTimeout = machineSet.Spec.Template.Spec.NodeDrainTimeout
377+
m.Spec.NodeDeletionTimeout = machineSet.Spec.Template.Spec.NodeDeletionTimeout
378+
m.Spec.NodeVolumeDetachTimeout = machineSet.Spec.Template.Spec.NodeVolumeDetachTimeout
379+
380+
err = patchHelper.Patch(ctx, m)
381+
if err != nil {
382+
log.Error(err, "Failed to update Machine", "Machine", klog.KObj(m))
383+
return errors.Wrapf(err, "failed to update Machine %q", klog.KObj(m))
384+
}
369385
continue
370386
}
371387

internal/controllers/machineset/machineset_controller_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ func TestMachineSetReconciler_syncMachines(t *testing.T) {
10001000
replicas := int32(2)
10011001
version := "v1.25.3"
10021002
duration10s := &metav1.Duration{Duration: 10 * time.Second}
1003+
duration11s := &metav1.Duration{Duration: 11 * time.Second}
10031004
ms := &clusterv1.MachineSet{
10041005
ObjectMeta: metav1.ObjectMeta{
10051006
UID: "abc-123-ms-uid",
@@ -1338,6 +1339,28 @@ func TestMachineSetReconciler_syncMachines(t *testing.T) {
13381339
g.Expect(updatedDeletingMachine.Spec.NodeDeletionTimeout).Should(Equal(deletingMachine.Spec.NodeDeletionTimeout))
13391340
g.Expect(updatedDeletingMachine.Spec.NodeVolumeDetachTimeout).Should(Equal(deletingMachine.Spec.NodeVolumeDetachTimeout))
13401341
}, 5*time.Second).Should(Succeed())
1342+
1343+
// Verify in-place mutable fields are updated on the deleting machine
1344+
ms.Spec.Template.Spec.NodeDrainTimeout = duration11s
1345+
ms.Spec.Template.Spec.NodeDeletionTimeout = duration11s
1346+
ms.Spec.Template.Spec.NodeVolumeDetachTimeout = duration11s
1347+
g.Expect(reconciler.syncMachines(ctx, ms, []*clusterv1.Machine{updatedInPlaceMutatingMachine, deletingMachine})).To(Succeed())
1348+
updatedDeletingMachine := deletingMachine.DeepCopy()
1349+
1350+
g.Expect(env.GetAPIReader().Get(ctx, client.ObjectKeyFromObject(updatedDeletingMachine), updatedDeletingMachine)).To(Succeed())
1351+
// Verify Node timeout values
1352+
g.Expect(updatedDeletingMachine.Spec.NodeDrainTimeout).Should(And(
1353+
Not(BeNil()),
1354+
HaveValue(Equal(*ms.Spec.Template.Spec.NodeDrainTimeout)),
1355+
))
1356+
g.Expect(updatedDeletingMachine.Spec.NodeDeletionTimeout).Should(And(
1357+
Not(BeNil()),
1358+
HaveValue(Equal(*ms.Spec.Template.Spec.NodeDeletionTimeout)),
1359+
))
1360+
g.Expect(updatedDeletingMachine.Spec.NodeVolumeDetachTimeout).Should(And(
1361+
Not(BeNil()),
1362+
HaveValue(Equal(*ms.Spec.Template.Spec.NodeVolumeDetachTimeout)),
1363+
))
13411364
}
13421365

13431366
func TestMachineSetReconciler_reconcileUnhealthyMachines(t *testing.T) {

0 commit comments

Comments
 (0)