forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pods_eviction_restriction.go
235 lines (203 loc) · 7.94 KB
/
pods_eviction_restriction.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
Copyright 2017 The Kubernetes 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 eviction
import (
"fmt"
"github.com/golang/glog"
apiv1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kube_client "k8s.io/client-go/kubernetes"
)
// PodsEvictionRestriction controls pods evictions. It ensures that we will not evict too
// many pods from one replica set. For replica set will allow to evict one pod or more if
// evictionToleranceFraction is configured.
type PodsEvictionRestriction interface {
// Evict sends eviction instruction to the api client.
// Retrurns error if pod cannot be evicted or if client returned error.
Evict(pod *apiv1.Pod) error
// CanEvict checks if pod can be safely evicted
CanEvict(pod *apiv1.Pod) bool
}
type podsEvictionRestrictionImpl struct {
client kube_client.Interface
podsCreators map[string]podReplicaCreator
evictionBudget map[podReplicaCreator]int
}
// PodsEvictionRestrictionFactory creates PodsEvictionRestriction
type PodsEvictionRestrictionFactory interface {
// NewPodsEvictionRestriction creates PodsEvictionRestriction for given set of pods.
NewPodsEvictionRestriction(pods []*apiv1.Pod) PodsEvictionRestriction
}
type podsEvictionRestrictionFactoryImpl struct {
client kube_client.Interface
minReplicas int
evictionToleranceFraction float64
}
type podReplicaCreator struct {
Namespace string
Name string
Kind string
}
// CanEvict checks if pod can be safely evicted
func (e *podsEvictionRestrictionImpl) CanEvict(pod *apiv1.Pod) bool {
cr, present := e.podsCreators[getPodID(pod)]
if present {
return e.evictionBudget[cr] > 0
}
return false
}
// Evict sends eviction instruction to api client. Retrurns error if pod cannot be evicted or if client returned error
// Does not check if pod was actually evicted after eviction grace period.
func (e *podsEvictionRestrictionImpl) Evict(podToEvict *apiv1.Pod) error {
cr, present := e.podsCreators[getPodID(podToEvict)]
if !present {
return fmt.Errorf("pod not suitable for eviction %v : not in replicated pods map", podToEvict.Name)
}
if e.evictionBudget[cr] < 1 {
return fmt.Errorf("cannot evict pod %v : eviction budget exceeded", podToEvict.Name)
}
eviction := &policyv1.Eviction{
ObjectMeta: metav1.ObjectMeta{
Namespace: podToEvict.Namespace,
Name: podToEvict.Name,
},
}
err := e.client.CoreV1().Pods(podToEvict.Namespace).Evict(eviction)
if err != nil {
glog.Errorf("failed to evict pod %s, error: %v", podToEvict.Name, err)
return err
}
e.evictionBudget[cr] = e.evictionBudget[cr] - 1
return nil
}
// NewPodsEvictionRestrictionFactory creates PodsEvictionRestrictionFactory
func NewPodsEvictionRestrictionFactory(client kube_client.Interface, minReplicas int, evictionToleranceFraction float64) PodsEvictionRestrictionFactory {
return &podsEvictionRestrictionFactoryImpl{client: client, minReplicas: minReplicas, evictionToleranceFraction: evictionToleranceFraction}
}
// NewPodsEvictionRestriction creates PodsEvictionRestriction for a given set of pods.
func (f *podsEvictionRestrictionFactoryImpl) NewPodsEvictionRestriction(pods []*apiv1.Pod) PodsEvictionRestriction {
// We can evict pod only if it is a part of replica set
// For each replica set we can evict only a fraction of pods.
// Evictions may be later limited by pod disruption budget if configured.
livePods := make(map[podReplicaCreator][]*apiv1.Pod)
for _, pod := range pods {
creator, err := getPodReplicaCreator(pod)
if err != nil {
glog.Errorf("failed to obtain replication info for pod %s: %v", pod.Name, err)
continue
}
if creator == nil {
glog.V(2).Infof("pod %s not replicated", pod.Name)
continue
}
livePods[*creator] = append(livePods[*creator], pod)
}
podsCreators := make(map[string]podReplicaCreator)
creatorsEvictionBudget := make(map[podReplicaCreator]int)
for creator, replicas := range livePods {
actual := len(replicas)
if actual < f.minReplicas {
glog.V(2).Infof("too few replicas for %v %v/%v. Found %v live pods",
creator.Kind, creator.Namespace, creator.Name, actual)
continue
}
var configured int
if creator.Kind == "Job" {
// Job has no replicas configuration, so we will use actual number of live pods as replicas count.
configured = actual
} else {
var err error
configured, err = getReplicaCount(creator, f.client)
if err != nil {
glog.Errorf("failed to obtain replication info for %v %v/%v. %v",
creator.Kind, creator.Namespace, creator.Name, err)
continue
}
}
evictionTolerance := int(float64(configured) * f.evictionToleranceFraction)
currentlyEvicted := configured - actual
evictionBudget := evictionTolerance - currentlyEvicted
if evictionBudget > 0 {
creatorsEvictionBudget[creator] = evictionBudget
} else {
creatorsEvictionBudget[creator] = 1
glog.V(2).Infof("configured eviction tolerance for pods from %v %v/%v too low. Setting eviction budget to 1",
creator.Kind, creator.Namespace, creator.Name)
}
for _, pod := range replicas {
podsCreators[getPodID(pod)] = creator
}
}
return &podsEvictionRestrictionImpl{client: f.client, podsCreators: podsCreators, evictionBudget: creatorsEvictionBudget}
}
func getPodReplicaCreator(pod *apiv1.Pod) (*podReplicaCreator, error) {
creator := managingControllerRef(pod)
if creator == nil {
return nil, nil
}
podReplicaCreator := &podReplicaCreator{
Namespace: pod.Namespace,
Name: creator.Name,
Kind: creator.Kind,
}
return podReplicaCreator, nil
}
func getPodID(pod *apiv1.Pod) string {
if pod == nil {
return ""
}
return pod.Namespace + "/" + pod.Name
}
func getReplicaCount(creator podReplicaCreator, client kube_client.Interface) (int, error) {
switch creator.Kind {
case "ReplicationController":
rc, err := client.CoreV1().ReplicationControllers(creator.Namespace).Get(creator.Name, metav1.GetOptions{})
if err != nil || rc == nil {
return 0, fmt.Errorf("replication controller %s/%s is not available, err: %v", creator.Namespace, creator.Name, err)
}
if rc.Spec.Replicas == nil || *rc.Spec.Replicas == 0 {
return 0, fmt.Errorf("replication controller %s/%s has no replicas config", creator.Namespace, creator.Name)
}
return int(*rc.Spec.Replicas), nil
case "ReplicaSet":
rs, err := client.ExtensionsV1beta1().ReplicaSets(creator.Namespace).Get(creator.Name, metav1.GetOptions{})
if err != nil || rs == nil {
return 0, fmt.Errorf("replica set %s/%s is not available, err: %v", creator.Namespace, creator.Name, err)
}
if rs.Spec.Replicas == nil || *rs.Spec.Replicas == 0 {
return 0, fmt.Errorf("replica set %s/%s has no replicas config", creator.Namespace, creator.Name)
}
return int(*rs.Spec.Replicas), nil
case "StatefulSet":
ss, err := client.AppsV1beta1().StatefulSets(creator.Namespace).Get(creator.Name, metav1.GetOptions{})
if err != nil || ss == nil {
return 0, fmt.Errorf("stateful set %s/%s is not available, err: %v", creator.Namespace, creator.Name, err)
}
if ss.Spec.Replicas == nil || *ss.Spec.Replicas == 0 {
return 0, fmt.Errorf("stateful set %s/%s has no replicas config", creator.Namespace, creator.Name)
}
return int(*ss.Spec.Replicas), nil
}
return 0, nil
}
func managingControllerRef(pod *apiv1.Pod) *metav1.OwnerReference {
var managingController metav1.OwnerReference
for _, ownerReference := range pod.ObjectMeta.GetOwnerReferences() {
if *ownerReference.Controller {
managingController = ownerReference
break
}
}
return &managingController
}