Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛Add DeleteMachineAnnotation for MachineSet Delete Policy #2929

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions controllers/machineset_delete_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ type (
const (
// DeleteNodeAnnotation marks nodes that will be given priority for deletion
// when a machineset scales down. This annotation is given top priority on all delete policies.
wfernandes marked this conversation as resolved.
Show resolved Hide resolved
// Deprecated: Please use DeleteMachineAnnotation instead.
DeleteNodeAnnotation = "cluster.k8s.io/delete-machine"
// DeleteMachineAnnotation marks nodes that will be given priority for deletion
// when a machineset scales down. This annotation is given top priority on all delete policies.
DeleteMachineAnnotation = "cluster.x-k8s.io/delete-machine"

mustDelete deletePriority = 100.0
betterDelete deletePriority = 50.0
Expand All @@ -51,6 +55,9 @@ func oldestDeletePriority(machine *clusterv1.Machine) deletePriority {
if machine.ObjectMeta.Annotations != nil && machine.ObjectMeta.Annotations[DeleteNodeAnnotation] != "" {
return mustDelete
}
if _, ok := machine.ObjectMeta.Annotations[DeleteMachineAnnotation]; ok {
return mustDelete
}
if machine.Status.FailureReason != nil || machine.Status.FailureMessage != nil {
return mustDelete
}
Expand All @@ -71,6 +78,9 @@ func newestDeletePriority(machine *clusterv1.Machine) deletePriority {
if machine.ObjectMeta.Annotations != nil && machine.ObjectMeta.Annotations[DeleteNodeAnnotation] != "" {
return mustDelete
}
if _, ok := machine.ObjectMeta.Annotations[DeleteMachineAnnotation]; ok {
return mustDelete
}
if machine.Status.FailureReason != nil || machine.Status.FailureMessage != nil {
return mustDelete
}
Expand All @@ -84,6 +94,9 @@ func randomDeletePolicy(machine *clusterv1.Machine) deletePriority {
if machine.ObjectMeta.Annotations != nil && machine.ObjectMeta.Annotations[DeleteNodeAnnotation] != "" {
return betterDelete
}
if _, ok := machine.ObjectMeta.Annotations[DeleteMachineAnnotation]; ok {
return betterDelete
}
if machine.Status.FailureReason != nil || machine.Status.FailureMessage != nil {
return betterDelete
}
Expand Down
84 changes: 60 additions & 24 deletions controllers/machineset_delete_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func TestMachineToDelete(t *testing.T) {
now := metav1.Now()
mustDeleteMachine := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{DeletionTimestamp: &now}}
betterDeleteMachine := &clusterv1.Machine{Status: clusterv1.MachineStatus{FailureMessage: &msg}}
deleteMeMachine := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{DeleteNodeAnnotation: "yes"}}}
deleteMachineWithNodeAnnotation := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{DeleteNodeAnnotation: "yes"}}}
deleteMachineWithMachineAnnotation := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{DeleteMachineAnnotation: ""}}}

tests := []struct {
desc string
Expand Down Expand Up @@ -139,36 +140,51 @@ func TestMachineToDelete(t *testing.T) {
},
},
{
desc: "func=randomDeletePolicy, annotated, diff=1",
desc: "func=randomDeletePolicy, DeleteNodeAnnotation, diff=1",
diff: 1,
machines: []*clusterv1.Machine{
{},
deleteMeMachine,
deleteMachineWithNodeAnnotation,
{},
},
expect: []*clusterv1.Machine{
deleteMeMachine,
deleteMachineWithNodeAnnotation,
},
}}
},
{
desc: "func=randomDeletePolicy, DeleteMachineAnnotation, diff=1",
diff: 1,
machines: []*clusterv1.Machine{
{},
deleteMachineWithMachineAnnotation,
{},
},
expect: []*clusterv1.Machine{
deleteMachineWithMachineAnnotation,
},
},
}

for _, test := range tests {
g := NewWithT(t)
t.Run(test.desc, func(t *testing.T) {
g := NewWithT(t)

result := getMachinesToDeletePrioritized(test.machines, test.diff, randomDeletePolicy)
g.Expect(result).To(Equal(test.expect))
result := getMachinesToDeletePrioritized(test.machines, test.diff, randomDeletePolicy)
g.Expect(result).To(Equal(test.expect))
})
}
}

func TestMachineNewestDelete(t *testing.T) {

currentTime := metav1.Now()
statusError := capierrors.MachineStatusError("I'm unhealthy!")
mustDeleteMachine := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{DeletionTimestamp: &currentTime}}
newest := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -1))}}
new := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -5))}}
old := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
oldest := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
annotatedMachine := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{DeleteNodeAnnotation: "yes"}, CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
deleteMachineWithNodeAnnotation := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{DeleteNodeAnnotation: "yes"}, CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
deleteMachineWithMachineAnnotation := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{DeleteMachineAnnotation: ""}, CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
unhealthyMachine := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}, Status: clusterv1.MachineStatus{FailureReason: &statusError}}

tests := []struct {
Expand Down Expand Up @@ -202,12 +218,20 @@ func TestMachineNewestDelete(t *testing.T) {
expect: []*clusterv1.Machine{mustDeleteMachine, newest, new},
},
{
desc: "func=newestDeletePriority, diff=1 (annotated)",
desc: "func=newestDeletePriority, diff=1 (DeleteNodeAnnotation)",
diff: 1,
machines: []*clusterv1.Machine{
new, oldest, old, newest, annotatedMachine,
new, oldest, old, newest, deleteMachineWithNodeAnnotation,
},
expect: []*clusterv1.Machine{annotatedMachine},
expect: []*clusterv1.Machine{deleteMachineWithNodeAnnotation},
},
{
desc: "func=newestDeletePriority, diff=1 (DeleteMachineAnnotation)",
diff: 1,
machines: []*clusterv1.Machine{
new, oldest, old, newest, deleteMachineWithMachineAnnotation,
},
expect: []*clusterv1.Machine{deleteMachineWithMachineAnnotation},
},
{
desc: "func=newestDeletePriority, diff=1 (unhealthy)",
Expand All @@ -220,23 +244,25 @@ func TestMachineNewestDelete(t *testing.T) {
}

for _, test := range tests {
g := NewWithT(t)
t.Run(test.desc, func(t *testing.T) {
g := NewWithT(t)

result := getMachinesToDeletePrioritized(test.machines, test.diff, newestDeletePriority)
g.Expect(result).To(Equal(test.expect))
result := getMachinesToDeletePrioritized(test.machines, test.diff, newestDeletePriority)
g.Expect(result).To(Equal(test.expect))
})
}
}

func TestMachineOldestDelete(t *testing.T) {

currentTime := metav1.Now()
statusError := capierrors.MachineStatusError("I'm unhealthy!")
empty := &clusterv1.Machine{}
newest := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -1))}}
new := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -5))}}
old := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
oldest := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
annotatedMachine := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{DeleteNodeAnnotation: "yes"}, CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
deleteMachineWithNodeAnnotation := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{DeleteNodeAnnotation: "yes"}, CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
deleteMachineWithMachineAnnotation := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{DeleteMachineAnnotation: ""}, CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}}
unhealthyMachine := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.NewTime(currentTime.Time.AddDate(0, 0, -10))}, Status: clusterv1.MachineStatus{FailureReason: &statusError}}

tests := []struct {
Expand Down Expand Up @@ -278,12 +304,20 @@ func TestMachineOldestDelete(t *testing.T) {
expect: []*clusterv1.Machine{oldest, old, new, newest},
},
{
desc: "func=oldestDeletePriority, diff=1 (annotated)",
desc: "func=oldestDeletePriority, diff=1 (DeleteNodeAnnotation)",
diff: 1,
machines: []*clusterv1.Machine{
empty, new, oldest, old, newest, deleteMachineWithNodeAnnotation,
},
expect: []*clusterv1.Machine{deleteMachineWithNodeAnnotation},
},
{
desc: "func=oldestDeletePriority, diff=1 (DeleteMachineAnnotation)",
diff: 1,
machines: []*clusterv1.Machine{
empty, new, oldest, old, newest, annotatedMachine,
empty, new, oldest, old, newest, deleteMachineWithMachineAnnotation,
},
expect: []*clusterv1.Machine{annotatedMachine},
expect: []*clusterv1.Machine{deleteMachineWithMachineAnnotation},
},
{
desc: "func=oldestDeletePriority, diff=1 (unhealthy)",
Expand All @@ -296,9 +330,11 @@ func TestMachineOldestDelete(t *testing.T) {
}

for _, test := range tests {
g := NewWithT(t)
t.Run(test.desc, func(t *testing.T) {
g := NewWithT(t)

result := getMachinesToDeletePrioritized(test.machines, test.diff, oldestDeletePriority)
g.Expect(result).To(Equal(test.expect))
result := getMachinesToDeletePrioritized(test.machines, test.diff, oldestDeletePriority)
g.Expect(result).To(Equal(test.expect))
})
}
}