Skip to content

Commit 9e152d7

Browse files
committed
F - pod deletion cost sorting
1 parent 8e0bd8c commit 9e152d7

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

reaper/options.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,17 @@ func maxPods() (int, error) {
168168
return v, nil
169169
}
170170

171+
func getPodDeletionCost(pod v1.Pod) int32 {
172+
// https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/#pod-deletion-cost
173+
costString, present := pod.ObjectMeta.Annotations["controller.kubernetes.io/pod-deletion-cost"]
174+
if !present {
175+
return 0
176+
}
177+
// per k8s doc: invalid values should be rejected by the API server
178+
cost, _ := strconv.ParseInt(costString, 10, 32)
179+
return int32(cost)
180+
}
181+
171182
func podSortingStrategy() (func([]v1.Pod), error) {
172183
sortingStrategy, present := os.LookupEnv(envPodSortingStrategy)
173184
if !present {
@@ -203,6 +214,12 @@ func podSortingStrategy() (func([]v1.Pod), error) {
203214
return pods[j].Status.StartTime.Unix() < pods[i].Status.StartTime.Unix()
204215
})
205216
}, nil
217+
case "pod-deletion-cost":
218+
return func(pods []v1.Pod) {
219+
sort.Slice(pods, func(i, j int) bool {
220+
return getPodDeletionCost(pods[i]) < getPodDeletionCost(pods[j])
221+
})
222+
}, nil
206223
default:
207224
return nil, errors.New("unknown pod sorting strategy")
208225
}

reaper/options_test.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,23 @@ func testPodList() []v1.Pod {
3131
},
3232
ObjectMeta: metav1.ObjectMeta{
3333
Name: "bearded-dragon",
34-
Annotations: map[string]string{"example/key": "lizard"},
34+
Annotations: map[string]string{"example/key": "lizard", "controller.kubernetes.io/pod-deletion-cost": "invalid"},
3535
},
3636
},
3737
{
3838
Status: v1.PodStatus{},
3939
ObjectMeta: metav1.ObjectMeta{
4040
Name: "nil-start-time",
41-
Annotations: map[string]string{"example/key": "lizard"},
41+
Annotations: map[string]string{"example/key": "lizard", "controller.kubernetes.io/pod-deletion-cost": "-100"},
42+
},
43+
},
44+
{
45+
Status: v1.PodStatus{
46+
StartTime: epocPlus(5 * time.Minute),
47+
},
48+
ObjectMeta: metav1.ObjectMeta{
49+
Name: "expensive",
50+
Annotations: map[string]string{"example/key": "not-lizard", "controller.kubernetes.io/pod-deletion-cost": "500"},
4251
},
4352
},
4453
{
@@ -325,7 +334,8 @@ func TestOptions(t *testing.T) {
325334
sorter(subject)
326335
assert.Equal(t, "corgi", subject[0].ObjectMeta.Name)
327336
assert.Equal(t, "bearded-dragon", subject[1].ObjectMeta.Name)
328-
assert.Equal(t, "nil-start-time", subject[2].ObjectMeta.Name)
337+
assert.Equal(t, "expensive", subject[2].ObjectMeta.Name)
338+
assert.Equal(t, "nil-start-time", subject[3].ObjectMeta.Name)
329339
assert.ElementsMatch(t, testPodList(), subject)
330340
})
331341
t.Run("youngest-first", func(t *testing.T) {
@@ -336,9 +346,22 @@ func TestOptions(t *testing.T) {
336346
assert.NoError(t, err)
337347
subject := testPodList()
338348
sorter(subject)
339-
assert.Equal(t, "bearded-dragon", subject[0].ObjectMeta.Name)
340-
assert.Equal(t, "corgi", subject[1].ObjectMeta.Name)
341-
assert.Equal(t, "nil-start-time", subject[2].ObjectMeta.Name)
349+
assert.Equal(t, "expensive", subject[0].ObjectMeta.Name)
350+
assert.Equal(t, "bearded-dragon", subject[1].ObjectMeta.Name)
351+
assert.Equal(t, "corgi", subject[2].ObjectMeta.Name)
352+
assert.Equal(t, "nil-start-time", subject[3].ObjectMeta.Name)
353+
assert.ElementsMatch(t, testPodList(), subject)
354+
})
355+
t.Run("pod-deletion-cost", func(t *testing.T) {
356+
os.Clearenv()
357+
os.Setenv(envPodSortingStrategy, "pod-deletion-cost")
358+
sorter, err := podSortingStrategy()
359+
assert.NotNil(t, sorter)
360+
assert.NoError(t, err)
361+
subject := testPodList()
362+
sorter(subject)
363+
assert.Equal(t, "nil-start-time", subject[0].ObjectMeta.Name)
364+
assert.Equal(t, "expensive", subject[3].ObjectMeta.Name)
342365
assert.ElementsMatch(t, testPodList(), subject)
343366
})
344367
})

0 commit comments

Comments
 (0)