/
evict_selection.go
116 lines (94 loc) · 2.14 KB
/
evict_selection.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
110
111
112
113
114
115
116
package loadwatcher
import (
"github.com/golang/glog"
"k8s.io/api/core/v1"
"sort"
)
type PodCandidateSet []PodCandidate
func (s PodCandidateSet) Len() int {
return len(s)
}
func (s PodCandidateSet) Less(i, j int) bool {
return s[i].Score < s[j].Score
}
func (s PodCandidateSet) Swap(i, j int) {
x := s[i]
s[i] = s[j]
s[j] = x
}
type PodCandidate struct {
Pod *v1.Pod
Score int
}
func PodCandidateSetFromPodList(l *v1.PodList) PodCandidateSet {
s := make(PodCandidateSet, len(l.Items))
for i := range l.Items {
s[i] = PodCandidate{
Pod: &l.Items[i],
Score: 0,
}
}
return s
}
func (s PodCandidateSet) scoreByQOSClass() {
for i := range s {
switch s[i].Pod.Status.QOSClass {
case v1.PodQOSBestEffort:
s[i].Score += 200
case v1.PodQOSBurstable:
s[i].Score += 100
}
}
}
func (s PodCandidateSet) scoreByOwnerType() {
for i := range s {
// do not evict Pods without owner; these will probably not be re-scheduled if evicted
if len(s[i].Pod.OwnerReferences) == 0 {
s[i].Score -= 1000
}
for j := range s[i].Pod.OwnerReferences {
o := &s[i].Pod.OwnerReferences[j]
switch o.Kind {
case "ReplicaSet":
s[i].Score += 100
case "StatefulSet":
s[i].Score -= 1000
case "DaemonSet":
s[i].Score -= 1000
}
}
}
}
func (s PodCandidateSet) scoreByCriticality() {
for i := range s {
if s[i].Pod.Namespace == "kube-system" {
s[i].Score -= 1000
}
switch s[i].Pod.Spec.PriorityClassName {
case "system-cluster-critical":
s[i].Score -= 1000
case "system-node-critical":
s[i].Score -= 1000
}
if _, ok := s[i].Pod.Annotations["scheduler.alpha.kubernetes.io/critical-pod"]; ok {
s[i].Score -= 1000
}
}
}
func (s PodCandidateSet) SelectPodForEviction() *v1.Pod {
s.scoreByQOSClass()
s.scoreByOwnerType()
s.scoreByCriticality()
sort.Stable(sort.Reverse(s))
for i := range s {
glog.Infof("eviction candidate: %s/%s (score of %d)", s[i].Pod.Namespace, s[i].Pod.Name, s[i].Score)
}
for i := range s {
if s[i].Score < 0 {
continue
}
glog.Infof("selected candidate: %s/%s (score of %d)", s[i].Pod.Namespace, s[i].Pod.Name, s[i].Score)
return s[i].Pod
}
return nil
}