-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
pods_eviction_restriction.go
393 lines (354 loc) · 14.1 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
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 (
"context"
"fmt"
"time"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
appsinformer "k8s.io/client-go/informers/apps/v1"
coreinformer "k8s.io/client-go/informers/core/v1"
kube_client "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
)
const (
resyncPeriod time.Duration = 1 * time.Minute
)
// 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.
// Returns error if pod cannot be evicted or if client returned error.
Evict(pod *apiv1.Pod, eventRecorder record.EventRecorder) error
// CanEvict checks if pod can be safely evicted
CanEvict(pod *apiv1.Pod) bool
}
type podsEvictionRestrictionImpl struct {
client kube_client.Interface
podToReplicaCreatorMap map[string]podReplicaCreator
creatorToSingleGroupStatsMap map[podReplicaCreator]singleGroupStats
}
type singleGroupStats struct {
configured int
pending int
running int
evictionTolerance int
evicted int
}
// PodsEvictionRestrictionFactory creates PodsEvictionRestriction
type PodsEvictionRestrictionFactory interface {
// NewPodsEvictionRestriction creates PodsEvictionRestriction for given set of pods,
// controlled by a single VPA object.
NewPodsEvictionRestriction(pods []*apiv1.Pod, vpa *vpa_types.VerticalPodAutoscaler) PodsEvictionRestriction
}
type podsEvictionRestrictionFactoryImpl struct {
client kube_client.Interface
rcInformer cache.SharedIndexInformer // informer for Replication Controllers
ssInformer cache.SharedIndexInformer // informer for Stateful Sets
rsInformer cache.SharedIndexInformer // informer for Replica Sets
dsInformer cache.SharedIndexInformer // informer for Daemon Sets
minReplicas int
evictionToleranceFraction float64
}
type controllerKind string
const (
replicationController controllerKind = "ReplicationController"
statefulSet controllerKind = "StatefulSet"
replicaSet controllerKind = "ReplicaSet"
daemonSet controllerKind = "DaemonSet"
job controllerKind = "Job"
)
type podReplicaCreator struct {
Namespace string
Name string
Kind controllerKind
}
// CanEvict checks if pod can be safely evicted
func (e *podsEvictionRestrictionImpl) CanEvict(pod *apiv1.Pod) bool {
cr, present := e.podToReplicaCreatorMap[getPodID(pod)]
if present {
singleGroupStats, present := e.creatorToSingleGroupStatsMap[cr]
if pod.Status.Phase == apiv1.PodPending {
return true
}
if present {
shouldBeAlive := singleGroupStats.configured - singleGroupStats.evictionTolerance
if singleGroupStats.running-singleGroupStats.evicted > shouldBeAlive {
return true
}
// If all pods are running and eviction tolerance is small evict 1 pod.
if singleGroupStats.running == singleGroupStats.configured &&
singleGroupStats.evictionTolerance == 0 &&
singleGroupStats.evicted == 0 {
return true
}
}
}
return false
}
// Evict sends eviction instruction to api client. Returns 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, eventRecorder record.EventRecorder) error {
cr, present := e.podToReplicaCreatorMap[getPodID(podToEvict)]
if !present {
return fmt.Errorf("pod not suitable for eviction %s/%s: not in replicated pods map", podToEvict.Namespace, podToEvict.Name)
}
if !e.CanEvict(podToEvict) {
return fmt.Errorf("cannot evict pod %s/%s: eviction budget exceeded", podToEvict.Namespace, podToEvict.Name)
}
eviction := &policyv1.Eviction{
ObjectMeta: metav1.ObjectMeta{
Namespace: podToEvict.Namespace,
Name: podToEvict.Name,
},
}
err := e.client.CoreV1().Pods(podToEvict.Namespace).EvictV1(context.TODO(), eviction)
if err != nil {
klog.Errorf("failed to evict pod %s/%s, error: %v", podToEvict.Namespace, podToEvict.Name, err)
return err
}
eventRecorder.Event(podToEvict, apiv1.EventTypeNormal, "EvictedByVPA",
"Pod was evicted by VPA Updater to apply resource recommendation.")
if podToEvict.Status.Phase != apiv1.PodPending {
singleGroupStats, present := e.creatorToSingleGroupStatsMap[cr]
if !present {
return fmt.Errorf("Internal error - cannot find stats for replication group %v", cr)
}
singleGroupStats.evicted = singleGroupStats.evicted + 1
e.creatorToSingleGroupStatsMap[cr] = singleGroupStats
}
return nil
}
// NewPodsEvictionRestrictionFactory creates PodsEvictionRestrictionFactory
func NewPodsEvictionRestrictionFactory(client kube_client.Interface, minReplicas int,
evictionToleranceFraction float64) (PodsEvictionRestrictionFactory, error) {
rcInformer, err := setUpInformer(client, replicationController)
if err != nil {
return nil, fmt.Errorf("Failed to create rcInformer: %v", err)
}
ssInformer, err := setUpInformer(client, statefulSet)
if err != nil {
return nil, fmt.Errorf("Failed to create ssInformer: %v", err)
}
rsInformer, err := setUpInformer(client, replicaSet)
if err != nil {
return nil, fmt.Errorf("Failed to create rsInformer: %v", err)
}
dsInformer, err := setUpInformer(client, daemonSet)
if err != nil {
return nil, fmt.Errorf("Failed to create dsInformer: %v", err)
}
return &podsEvictionRestrictionFactoryImpl{
client: client,
rcInformer: rcInformer, // informer for Replication Controllers
ssInformer: ssInformer, // informer for Replica Sets
rsInformer: rsInformer, // informer for Stateful Sets
dsInformer: dsInformer, // informer for Daemon Sets
minReplicas: minReplicas,
evictionToleranceFraction: evictionToleranceFraction}, nil
}
// NewPodsEvictionRestriction creates PodsEvictionRestriction for a given set of pods,
// controlled by a single VPA object.
func (f *podsEvictionRestrictionFactoryImpl) NewPodsEvictionRestriction(pods []*apiv1.Pod, vpa *vpa_types.VerticalPodAutoscaler) 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 {
klog.Errorf("failed to obtain replication info for pod %s: %v", klog.KObj(pod), err)
continue
}
if creator == nil {
klog.Warningf("pod %s not replicated", pod.Name)
continue
}
livePods[*creator] = append(livePods[*creator], pod)
}
podToReplicaCreatorMap := make(map[string]podReplicaCreator)
creatorToSingleGroupStatsMap := make(map[podReplicaCreator]singleGroupStats)
// Use per-VPA minReplicas if present, fall back to the global setting.
required := f.minReplicas
if vpa.Spec.UpdatePolicy != nil && vpa.Spec.UpdatePolicy.MinReplicas != nil {
required = int(*vpa.Spec.UpdatePolicy.MinReplicas)
klog.V(3).Infof("overriding minReplicas from global %v to per-VPA %v for VPA %s",
f.minReplicas, required, klog.KObj(vpa))
}
for creator, replicas := range livePods {
actual := len(replicas)
if actual < required {
klog.V(2).Infof("too few replicas for %v %v/%v. Found %v live pods, needs %v (global %v)",
creator.Kind, creator.Namespace, creator.Name, actual, required, f.minReplicas)
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 = f.getReplicaCount(creator)
if err != nil {
klog.Errorf("failed to obtain replication info for %v %v/%v. %v",
creator.Kind, creator.Namespace, creator.Name, err)
continue
}
}
singleGroup := singleGroupStats{}
singleGroup.configured = configured
singleGroup.evictionTolerance = int(float64(configured) * f.evictionToleranceFraction)
for _, pod := range replicas {
podToReplicaCreatorMap[getPodID(pod)] = creator
if pod.Status.Phase == apiv1.PodPending {
singleGroup.pending = singleGroup.pending + 1
}
}
singleGroup.running = len(replicas) - singleGroup.pending
creatorToSingleGroupStatsMap[creator] = singleGroup
}
return &podsEvictionRestrictionImpl{
client: f.client,
podToReplicaCreatorMap: podToReplicaCreatorMap,
creatorToSingleGroupStatsMap: creatorToSingleGroupStatsMap}
}
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: controllerKind(creator.Kind),
}
return podReplicaCreator, nil
}
func getPodID(pod *apiv1.Pod) string {
if pod == nil {
return ""
}
return pod.Namespace + "/" + pod.Name
}
func (f *podsEvictionRestrictionFactoryImpl) getReplicaCount(creator podReplicaCreator) (int, error) {
switch creator.Kind {
case replicationController:
rcObj, exists, err := f.rcInformer.GetStore().GetByKey(creator.Namespace + "/" + creator.Name)
if err != nil {
return 0, fmt.Errorf("replication controller %s/%s is not available, err: %v", creator.Namespace, creator.Name, err)
}
if !exists {
return 0, fmt.Errorf("replication controller %s/%s does not exist", creator.Namespace, creator.Name)
}
rc, ok := rcObj.(*apiv1.ReplicationController)
if !ok {
return 0, fmt.Errorf("Failed to parse Replication Controller")
}
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:
rsObj, exists, err := f.rsInformer.GetStore().GetByKey(creator.Namespace + "/" + creator.Name)
if err != nil {
return 0, fmt.Errorf("replica set %s/%s is not available, err: %v", creator.Namespace, creator.Name, err)
}
if !exists {
return 0, fmt.Errorf("replica set %s/%s does not exist", creator.Namespace, creator.Name)
}
rs, ok := rsObj.(*appsv1.ReplicaSet)
if !ok {
return 0, fmt.Errorf("Failed to parse Replicaset")
}
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:
ssObj, exists, err := f.ssInformer.GetStore().GetByKey(creator.Namespace + "/" + creator.Name)
if err != nil {
return 0, fmt.Errorf("stateful set %s/%s is not available, err: %v", creator.Namespace, creator.Name, err)
}
if !exists {
return 0, fmt.Errorf("stateful set %s/%s does not exist", creator.Namespace, creator.Name)
}
ss, ok := ssObj.(*appsv1.StatefulSet)
if !ok {
return 0, fmt.Errorf("Failed to parse StatefulSet")
}
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
case daemonSet:
dsObj, exists, err := f.dsInformer.GetStore().GetByKey(creator.Namespace + "/" + creator.Name)
if err != nil {
return 0, fmt.Errorf("daemon set %s/%s is not available, err: %v", creator.Namespace, creator.Name, err)
}
if !exists {
return 0, fmt.Errorf("daemon set %s/%s does not exist", creator.Namespace, creator.Name)
}
ds, ok := dsObj.(*appsv1.DaemonSet)
if !ok {
return 0, fmt.Errorf("Failed to parse DaemonSet")
}
if ds.Status.NumberReady == 0 {
return 0, fmt.Errorf("daemon set %s/%s has no number ready pods", creator.Namespace, creator.Name)
}
return int(ds.Status.NumberReady), 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
}
func setUpInformer(kubeClient kube_client.Interface, kind controllerKind) (cache.SharedIndexInformer, error) {
var informer cache.SharedIndexInformer
switch kind {
case replicationController:
informer = coreinformer.NewReplicationControllerInformer(kubeClient, apiv1.NamespaceAll,
resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
case replicaSet:
informer = appsinformer.NewReplicaSetInformer(kubeClient, apiv1.NamespaceAll,
resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
case statefulSet:
informer = appsinformer.NewStatefulSetInformer(kubeClient, apiv1.NamespaceAll,
resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
case daemonSet:
informer = appsinformer.NewDaemonSetInformer(kubeClient, apiv1.NamespaceAll,
resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
default:
return nil, fmt.Errorf("Unknown controller kind: %v", kind)
}
stopCh := make(chan struct{})
go informer.Run(stopCh)
synced := cache.WaitForCacheSync(stopCh, informer.HasSynced)
if !synced {
return nil, fmt.Errorf("Failed to sync %v cache.", kind)
}
return informer, nil
}