Skip to content

Commit 1721d42

Browse files
authored
Merge pull request kubernetes-sigs#10087 from ninech/multi-delete
🐛 fix: deletion priority to avoid deleting too many machines
2 parents fb30a6f + 7fc594d commit 1721d42

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

internal/controllers/machineset/machineset_delete_policy.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type (
3535

3636
const (
3737
mustDelete deletePriority = 100.0
38+
shouldDelete deletePriority = 75.0
3839
betterDelete deletePriority = 50.0
3940
couldDelete deletePriority = 20.0
4041
mustNotDelete deletePriority = 0.0
@@ -48,10 +49,10 @@ func oldestDeletePriority(machine *clusterv1.Machine) deletePriority {
4849
return mustDelete
4950
}
5051
if _, ok := machine.ObjectMeta.Annotations[clusterv1.DeleteMachineAnnotation]; ok {
51-
return mustDelete
52+
return shouldDelete
5253
}
5354
if !isMachineHealthy(machine) {
54-
return mustDelete
55+
return betterDelete
5556
}
5657
if machine.ObjectMeta.CreationTimestamp.Time.IsZero() {
5758
return mustNotDelete
@@ -60,20 +61,20 @@ func oldestDeletePriority(machine *clusterv1.Machine) deletePriority {
6061
if d.Seconds() < 0 {
6162
return mustNotDelete
6263
}
63-
return deletePriority(float64(mustDelete) * (1.0 - math.Exp(-d.Seconds()/secondsPerTenDays)))
64+
return deletePriority(float64(betterDelete) * (1.0 - math.Exp(-d.Seconds()/secondsPerTenDays)))
6465
}
6566

6667
func newestDeletePriority(machine *clusterv1.Machine) deletePriority {
6768
if !machine.DeletionTimestamp.IsZero() {
6869
return mustDelete
6970
}
7071
if _, ok := machine.ObjectMeta.Annotations[clusterv1.DeleteMachineAnnotation]; ok {
71-
return mustDelete
72+
return shouldDelete
7273
}
7374
if !isMachineHealthy(machine) {
74-
return mustDelete
75+
return betterDelete
7576
}
76-
return mustDelete - oldestDeletePriority(machine)
77+
return betterDelete - oldestDeletePriority(machine)
7778
}
7879

7980
func randomDeletePolicy(machine *clusterv1.Machine) deletePriority {

internal/controllers/machineset/machineset_delete_policy_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,18 @@ func TestMachineOldestDelete(t *testing.T) {
407407
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))},
408408
Status: clusterv1.MachineStatus{FailureReason: &statusError, NodeRef: nodeRef},
409409
}
410+
mustDeleteMachine := &clusterv1.Machine{
411+
ObjectMeta: metav1.ObjectMeta{Name: "b", DeletionTimestamp: &currentTime},
412+
Status: clusterv1.MachineStatus{NodeRef: nodeRef},
413+
}
414+
unhealthyMachineA := &clusterv1.Machine{
415+
ObjectMeta: metav1.ObjectMeta{Name: "a", CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))},
416+
Status: clusterv1.MachineStatus{FailureReason: &statusError, NodeRef: nodeRef},
417+
}
418+
unhealthyMachineZ := &clusterv1.Machine{
419+
ObjectMeta: metav1.ObjectMeta{Name: "z", CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))},
420+
Status: clusterv1.MachineStatus{FailureReason: &statusError, NodeRef: nodeRef},
421+
}
410422
deleteMachineWithoutNodeRef := &clusterv1.Machine{
411423
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))},
412424
}
@@ -513,6 +525,23 @@ func TestMachineOldestDelete(t *testing.T) {
513525
},
514526
expect: []*clusterv1.Machine{nodeHealthyConditionUnknownMachine},
515527
},
528+
// these two cases ensures the mustDeleteMachine is always picked regardless of the machine names.
529+
{
530+
desc: "func=oldestDeletePriority, diff=1 (unhealthyMachineA)",
531+
diff: 1,
532+
machines: []*clusterv1.Machine{
533+
empty, secondNewest, oldest, secondOldest, newest, mustDeleteMachine, unhealthyMachineA,
534+
},
535+
expect: []*clusterv1.Machine{mustDeleteMachine},
536+
},
537+
{
538+
desc: "func=oldestDeletePriority, diff=1 (unhealthyMachineZ)",
539+
diff: 1,
540+
machines: []*clusterv1.Machine{
541+
empty, secondNewest, oldest, secondOldest, newest, mustDeleteMachine, unhealthyMachineZ,
542+
},
543+
expect: []*clusterv1.Machine{mustDeleteMachine},
544+
},
516545
}
517546

518547
for _, test := range tests {

0 commit comments

Comments
 (0)