Skip to content

Commit

Permalink
fix: deletion priority to avoid deleting too many machines
Browse files Browse the repository at this point in the history
This introduces an additional deletePriority between betterDelete and
mustDelete to avoid a race where multiple machines might be deleted at
the same time.
  • Loading branch information
ctrox committed Mar 19, 2024
1 parent 6daf4ac commit e0ee1dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 7 additions & 6 deletions internal/controllers/machineset/machineset_delete_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type (

const (
mustDelete deletePriority = 100.0
shouldDelete deletePriority = 75.0
betterDelete deletePriority = 50.0
couldDelete deletePriority = 20.0
mustNotDelete deletePriority = 0.0
Expand All @@ -48,10 +49,10 @@ func oldestDeletePriority(machine *clusterv1.Machine) deletePriority {
return mustDelete
}
if _, ok := machine.ObjectMeta.Annotations[clusterv1.DeleteMachineAnnotation]; ok {
return mustDelete
return shouldDelete
}
if !isMachineHealthy(machine) {
return mustDelete
return betterDelete
}
if machine.ObjectMeta.CreationTimestamp.Time.IsZero() {
return mustNotDelete
Expand All @@ -60,20 +61,20 @@ func oldestDeletePriority(machine *clusterv1.Machine) deletePriority {
if d.Seconds() < 0 {
return mustNotDelete
}
return deletePriority(float64(mustDelete) * (1.0 - math.Exp(-d.Seconds()/secondsPerTenDays)))
return deletePriority(float64(betterDelete) * (1.0 - math.Exp(-d.Seconds()/secondsPerTenDays)))
}

func newestDeletePriority(machine *clusterv1.Machine) deletePriority {
if !machine.DeletionTimestamp.IsZero() {
return mustDelete
}
if _, ok := machine.ObjectMeta.Annotations[clusterv1.DeleteMachineAnnotation]; ok {
return mustDelete
return shouldDelete
}
if !isMachineHealthy(machine) {
return mustDelete
return betterDelete
}
return mustDelete - oldestDeletePriority(machine)
return betterDelete - oldestDeletePriority(machine)
}

func randomDeletePolicy(machine *clusterv1.Machine) deletePriority {
Expand Down
12 changes: 12 additions & 0 deletions internal/controllers/machineset/machineset_delete_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ func TestMachineOldestDelete(t *testing.T) {
empty := &clusterv1.Machine{
Status: clusterv1.MachineStatus{NodeRef: nodeRef},
}
mustDeleteMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{DeletionTimestamp: &currentTime},
Status: clusterv1.MachineStatus{NodeRef: nodeRef},
}
newest := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -1))},
Status: clusterv1.MachineStatus{NodeRef: nodeRef},
Expand Down Expand Up @@ -513,6 +517,14 @@ func TestMachineOldestDelete(t *testing.T) {
},
expect: []*clusterv1.Machine{nodeHealthyConditionUnknownMachine},
},
{
desc: "func=oldestDeletePriority, diff=1 (deletionTimestamp)",
diff: 1,
machines: []*clusterv1.Machine{
empty, secondNewest, oldest, secondOldest, newest, nodeHealthyConditionUnknownMachine, mustDeleteMachine,
},
expect: []*clusterv1.Machine{mustDeleteMachine},
},
}

for _, test := range tests {
Expand Down

0 comments on commit e0ee1dd

Please sign in to comment.