Skip to content

Commit a8b5252

Browse files
authored
Merge pull request kubernetes#2897 from bskiba/fix_less
Correct Less function for priority.
2 parents 6bad36f + fd2c05f commit a8b5252

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

vertical-pod-autoscaler/pkg/updater/priority/update_priority_calculator.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (calc *UpdatePriorityCalculator) AddPod(pod *apiv1.Pod, now time.Time) {
153153

154154
// GetSortedPods returns a list of pods ordered by update priority (highest update priority first)
155155
func (calc *UpdatePriorityCalculator) GetSortedPods(admission PodEvictionAdmission) []*apiv1.Pod {
156-
sort.Sort(byPriority(calc.pods))
156+
sort.Sort(byPriorityDesc(calc.pods))
157157

158158
result := []*apiv1.Pod{}
159159
for _, podPrio := range calc.pods {
@@ -197,30 +197,31 @@ type PodPriority struct {
197197
ResourceDiff float64
198198
}
199199

200-
type byPriority []prioritizedPod
200+
type byPriorityDesc []prioritizedPod
201201

202-
func (list byPriority) Len() int {
202+
func (list byPriorityDesc) Len() int {
203203
return len(list)
204204
}
205-
func (list byPriority) Swap(i, j int) {
205+
func (list byPriorityDesc) Swap(i, j int) {
206206
list[i], list[j] = list[j], list[i]
207207
}
208208

209209
// Less implements reverse ordering by priority (highest priority first).
210-
func (list byPriority) Less(i, j int) bool {
211-
return list[i].priority.Less(list[j].priority)
210+
// This means we return true if priority at index j is lower than at index i.
211+
func (list byPriorityDesc) Less(i, j int) bool {
212+
return list[j].priority.Less(list[i].priority)
212213
}
213214

214-
// Less compares pod update priorities.
215+
// Less returns true if p is lower than other.
215216
func (p PodPriority) Less(other PodPriority) bool {
216217
// 1. If any container wants to grow, the pod takes precedence.
217218
// TODO: A better policy would be to prioritize scaling down when
218219
// (a) the pod is pending
219220
// (b) there is general resource shortage
220221
// and prioritize scaling up otherwise.
221222
if p.ScaleUp != other.ScaleUp {
222-
return p.ScaleUp
223+
return other.ScaleUp
223224
}
224225
// 2. A pod with larger value of resourceDiff takes precedence.
225-
return p.ResourceDiff > other.ResourceDiff
226+
return p.ResourceDiff < other.ResourceDiff
226227
}

vertical-pod-autoscaler/pkg/updater/priority/update_priority_calculator_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,58 @@ func TestAdmission(t *testing.T) {
499499
result := calculator.GetSortedPods(&pod1Admission{})
500500
assert.Exactly(t, []*apiv1.Pod{pod1}, result, "Wrong priority order")
501501
}
502+
503+
func TestLessPodPriority(t *testing.T) {
504+
testCases := []struct {
505+
name string
506+
prio, other PodPriority
507+
isLess bool
508+
}{
509+
{
510+
name: "scale down more than empty",
511+
prio: PodPriority{
512+
ScaleUp: false,
513+
ResourceDiff: 0.1,
514+
},
515+
other: PodPriority{},
516+
isLess: false,
517+
}, {
518+
name: "scale up more than empty",
519+
prio: PodPriority{
520+
ScaleUp: true,
521+
ResourceDiff: 0.1,
522+
},
523+
other: PodPriority{},
524+
isLess: false,
525+
}, {
526+
name: "two scale ups",
527+
prio: PodPriority{
528+
ScaleUp: true,
529+
ResourceDiff: 0.1,
530+
},
531+
other: PodPriority{
532+
ScaleUp: true,
533+
ResourceDiff: 1.0,
534+
},
535+
isLess: true,
536+
}, {
537+
name: "two scale downs",
538+
prio: PodPriority{
539+
ScaleUp: false,
540+
ResourceDiff: 0.9,
541+
},
542+
other: PodPriority{
543+
ScaleUp: false,
544+
ResourceDiff: 0.1,
545+
},
546+
isLess: false,
547+
},
548+
}
549+
for _, tc := range testCases {
550+
t.Run(tc.name, func(t *testing.T) {
551+
assert.Equal(t, tc.isLess, tc.prio.Less(tc.other))
552+
assert.Equal(t, !tc.isLess, tc.other.Less(tc.prio))
553+
})
554+
}
555+
556+
}

0 commit comments

Comments
 (0)