forked from mittwald/kubernetes-loadwatcher
/
evict_selection.go
140 lines (116 loc) · 2.63 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package pressurecooker
import (
"math"
"sort"
"time"
"github.com/golang/glog"
v1 "k8s.io/api/core/v1"
)
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 += 100
case v1.PodQOSBurstable:
s[i].Score += 100
}
}
}
func (s PodCandidateSet) scoreByAge(minPodAge time.Duration) {
now := time.Now()
for i, pod := range s {
if pod.Pod.Status.StartTime == nil {
s[i].Score -= 10000
continue
}
delta := now.Sub(pod.Pod.Status.StartTime.Time)
if delta < minPodAge {
s[i].Score -= 10000
continue
}
age := int64(delta / time.Second)
if age < 1 {
age = 1
}
s[i].Score += int(math.Floor(math.Log1p(float64(age))))
}
}
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 -= 10000
case "DaemonSet":
s[i].Score -= 10000
}
}
}
}
func (s PodCandidateSet) scoreByCriticality() {
for i := range s {
if s[i].Pod.Namespace == "kube-system" {
s[i].Score -= 10000
}
switch s[i].Pod.Spec.PriorityClassName {
case "system-cluster-critical":
s[i].Score -= 10000
case "system-node-critical":
s[i].Score -= 10000
}
if _, ok := s[i].Pod.Annotations["scheduler.alpha.kubernetes.io/critical-pod"]; ok {
s[i].Score -= 10000
}
}
}
func (s PodCandidateSet) SelectPodForEviction(minPodAge time.Duration) *v1.Pod {
s.scoreByAge(minPodAge)
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
}