-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_sort.go
109 lines (94 loc) · 2.81 KB
/
priority_sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright 2019 The Kruise Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package updatesort
import (
"sort"
"strconv"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
type prioritySort struct {
strategy *appsv1alpha1.UpdatePriorityStrategy
}
func NewPrioritySorter(s *appsv1alpha1.UpdatePriorityStrategy) Sorter {
return &prioritySort{strategy: s}
}
// Sort helps sort the indexes of pods by UpdatePriorityStrategy.
func (ps *prioritySort) Sort(pods []*v1.Pod, indexes []int) []int {
if ps.strategy == nil || (len(ps.strategy.WeightPriority) == 0 && len(ps.strategy.OrderPriority) == 0) {
return indexes
}
f := func(i, j int) bool {
podI := pods[indexes[i]]
podJ := pods[indexes[j]]
return ps.compare(podI.Labels, podJ.Labels, indexes[i] > indexes[j])
}
sort.Slice(indexes, f)
return indexes
}
func (ps *prioritySort) compare(podI, podJ map[string]string, defaultVal bool) bool {
if len(ps.strategy.WeightPriority) > 0 {
if wI, wJ := ps.getPodWeightPriority(podI), ps.getPodWeightPriority(podJ); wI != wJ {
return wI > wJ
}
} else if len(ps.strategy.OrderPriority) > 0 {
levelI, orderI := ps.getPodOrderPriority(podI)
levelJ, orderJ := ps.getPodOrderPriority(podJ)
if levelI != levelJ {
return levelI < levelJ
} else if orderI != orderJ {
return orderI > orderJ
}
}
return defaultVal
}
func (ps *prioritySort) getPodWeightPriority(podLabels map[string]string) int64 {
var weight int64
for _, p := range ps.strategy.WeightPriority {
selector, err := metav1.LabelSelectorAsSelector(&p.MatchSelector)
if err != nil {
continue
}
if selector.Matches(labels.Set(podLabels)) {
weight += int64(p.Weight)
}
}
return weight
}
func (ps *prioritySort) getPodOrderPriority(podLabels map[string]string) (int64, int64) {
for i, p := range ps.strategy.OrderPriority {
if value, ok := podLabels[p.OrderedKey]; ok {
return int64(i), getIntFromStringSuffix(value)
}
}
return -1, 0
}
func getIntFromStringSuffix(v string) int64 {
startIdx := -1
for i := len(v) - 1; i >= 0; i-- {
if v[i] >= '0' && v[i] <= '9' {
startIdx = i
} else {
break
}
}
if startIdx < 0 {
return 0
}
if order, err := strconv.ParseInt(v[startIdx:], 10, 64); err == nil {
return order
}
return 0
}