/
drain.go
483 lines (407 loc) · 12.7 KB
/
drain.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package updatestrategy
import (
"context"
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/cenkalti/backoff"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
v1 "k8s.io/api/core/v1"
policy "k8s.io/api/policy/v1beta1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
const (
drainStartAnnotation = "cluster-lifecycle-manager.zalando.org/drain-start"
lastForcedDrainAnnotation = "cluster-lifecycle-manager.zalando.org/last-forced-drain"
)
func timestampAnnotation(logger *log.Entry, node *Node, annotation string, fallback time.Time) time.Time {
ts, ok := node.Annotations[annotation]
if !ok {
return fallback
}
value, err := time.Parse(time.RFC3339, ts)
if err != nil {
logger.Warnf("invalid value for %s (will use %s): %v", annotation, fallback, err)
return fallback
}
return value
}
// drain tries to cleanly evict all of the pods on a node, and then forcibly terminates the remaining ones.
func (m *KubernetesNodePoolManager) drain(ctx context.Context, node *Node) error {
m.logger.WithField("node", node.Name).Info("Draining node")
err := m.labelNode(ctx, node, lifecycleStatusLabel, lifecycleStatusDraining)
if err != nil {
return err
}
drainStart := timestampAnnotation(m.logger, node, drainStartAnnotation, time.Now())
err = m.annotateNode(ctx, node, drainStartAnnotation, drainStart.Format(time.RFC3339))
if err != nil {
return err
}
lastForcedTermination := timestampAnnotation(m.logger, node, lastForcedDrainAnnotation, time.Unix(0, 0))
// fast eviction (in parallel) as long as we can evict something without violating PDBs
for {
err = ctx.Err()
if err != nil {
return err
}
pods, err := m.evictablePods(ctx, node.Name)
if err != nil {
return err
}
if len(pods) == 0 {
return nil
}
evicted, err := m.evictParallel(ctx, pods)
if err == nil {
err = ctx.Err()
}
if err != nil {
return err
}
if !evicted {
break
}
time.Sleep(m.drainConfig.PollInterval)
}
// slow eviction, one by one
for {
err = ctx.Err()
if err != nil {
return err
}
pods, err := m.evictablePods(ctx, node.Name)
if err != nil {
return err
}
if len(pods) == 0 {
return nil
}
forceEvicted, err := m.evictOrForceTerminatePod(ctx, pods, drainStart, lastForcedTermination)
if err != nil {
return err
}
if forceEvicted {
lastForcedTermination = time.Now()
err := m.annotateNode(ctx, node, lastForcedDrainAnnotation, lastForcedTermination.Format(time.RFC3339))
if err != nil {
return err
}
}
time.Sleep(m.drainConfig.PollInterval)
}
}
func (m *KubernetesNodePoolManager) evictParallel(ctx context.Context, pods []v1.Pod) (bool, error) {
var evicted int64
var group errgroup.Group
for _, pod := range pods {
pod := pod
group.Go(func() error {
err := evictPod(ctx, m.kube, m.logger, pod)
if err != nil {
if isPDBViolation(err) {
m.logPdbViolated(pod)
return nil
}
return err
}
atomic.AddInt64(&evicted, 1)
return nil
})
}
err := group.Wait()
return atomic.LoadInt64(&evicted) > 0, err
}
func (m *KubernetesNodePoolManager) podLogger(pod v1.Pod) *log.Entry {
return m.logger.WithFields(log.Fields{
"ns": pod.Namespace,
"pod": pod.Name,
"node": pod.Spec.NodeName,
})
}
func (m *KubernetesNodePoolManager) logPdbViolated(pod v1.Pod) {
m.podLogger(pod).Info("Pod Disruption Budget violated")
}
func (m *KubernetesNodePoolManager) evictOrForceTerminatePod(ctx context.Context, pods []v1.Pod, drainStart, lastForcedTermination time.Time) (bool, error) {
for _, pod := range pods {
err := ctx.Err()
if err != nil {
return false, err
}
// try evicting normally
err = evictPod(ctx, m.kube, m.logger, pod)
if err == nil {
return false, nil
}
if !isPDBViolation(err) {
return false, err
}
// PDB violation, log and check if we can force terminate the pod
m.logPdbViolated(pod)
forceTerminate, _ := m.forceTerminationAllowed(ctx, pod, time.Now(), drainStart, lastForcedTermination)
if forceTerminate {
err = deletePod(ctx, m.kube, m.podLogger(pod), pod)
if err != nil {
return false, err
}
return true, nil
}
}
return false, nil
}
func (m *KubernetesNodePoolManager) forceTerminationAllowed(ctx context.Context, pod v1.Pod, now, drainStart, lastForcedTermination time.Time) (bool, error) {
// too early to start force terminating
if now.Before(drainStart.Add(m.drainConfig.ForceEvictionGracePeriod)) {
m.podLogger(pod).Debug("Won't force terminate (node in grace period)")
return false, nil
}
// we've recently force killed a pod
if now.Before(lastForcedTermination.Add(m.drainConfig.ForceEvictionInterval)) {
m.podLogger(pod).Debug("Won't force terminate (recently force terminated a pod)")
return false, nil
}
// pod too young
if now.Before(pod.GetCreationTimestamp().Add(m.drainConfig.MinPodLifetime)) {
m.podLogger(pod).Debug("Won't force terminate (pod too young)")
return false, nil
}
// find all other pods matched by the same PDBs
allPods, err := m.getPodsByNamespace(ctx, pod.GetNamespace())
if err != nil {
return false, err
}
allPdbs, err := m.getPDBsByNamespace(ctx, pod.GetNamespace())
if err != nil {
return false, err
}
siblingPods := findSiblingPods(m.logger, pod, allPods.Items, allPdbs.Items)
// check if PDB siblings are old enough
siblingsOldEnough := true
for _, siblingPod := range siblingPods {
waitTime := m.drainConfig.MinUnhealthyPDBSiblingLifetime
if podReady(siblingPod) {
waitTime = m.drainConfig.MinHealthyPDBSiblingLifetime
}
if now.Before(siblingPod.GetCreationTimestamp().Add(waitTime)) {
siblingsOldEnough = false
break
}
}
if !siblingsOldEnough {
var siblingNames []string
for _, siblingPod := range siblingPods {
siblingNames = append(siblingNames, siblingPod.GetName())
}
m.podLogger(pod).Debugf("Won't force terminate (siblings %s too young)", strings.Join(siblingNames, ", "))
return false, nil
}
return true, nil
}
func podReady(pod v1.Pod) bool {
for _, condition := range pod.Status.Conditions {
if condition.Type == v1.PodReady {
return condition.Status == v1.ConditionTrue
}
}
return false
}
func findSiblingPods(logger *log.Entry, pod v1.Pod, allPods []v1.Pod, allPdbs []policy.PodDisruptionBudget) []v1.Pod {
var siblingSelectors []labels.Selector
for _, pdb := range allPdbs {
selector, err := metav1.LabelSelectorAsSelector(pdb.Spec.Selector)
if err != nil {
logger.Debugf("pdb %s/%s has an invalid selector: %v", pod.GetNamespace(), pdb.GetName(), err)
continue
}
if selector.Matches(labels.Set(pod.Labels)) {
siblingSelectors = append(siblingSelectors, selector)
}
}
var result []v1.Pod
for _, candidate := range allPods {
if candidate.GetName() == pod.GetName() {
continue
}
if ignoredSiblingPod(&candidate) {
continue
}
for _, selector := range siblingSelectors {
if selector.Matches(labels.Set(candidate.Labels)) {
result = append(result, candidate)
break
}
}
}
return result
}
// ignoredSiblingPod returns true if a sibling pod should be ignored (e.g. because it's a cronjob pod)
func ignoredSiblingPod(pod *v1.Pod) bool {
// terminated pod, ignore
if podTerminated(pod) {
return true
}
for _, owner := range pod.GetObjectMeta().GetOwnerReferences() {
// job/cronjob pod, ignore
if owner.Kind == "Job" {
return true
}
}
return false
}
func isPDBViolation(err error) bool {
return apiErrors.IsTooManyRequests(err) || strings.Contains(err.Error(), multiplePDBsErrMsg)
}
var deletePod = func(ctx context.Context, client kubernetes.Interface, logger *log.Entry, pod v1.Pod) error {
err := client.CoreV1().Pods(pod.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{
GracePeriodSeconds: pod.Spec.TerminationGracePeriodSeconds,
})
if err != nil {
if apiErrors.IsNotFound(err) {
return nil
}
logger.Errorf("Failed to delete pod: %v", err)
return err
}
// wait for pod to be terminated and gone from the node.
err = waitForPodTermination(ctx, client, pod)
if err != nil {
logger.Warnf("Pod not terminated within grace period: %s", err)
}
logger.Info("Pod deleted")
return nil
}
func podTerminated(pod *v1.Pod) bool {
return pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed
}
// evictPod tries to evict a pod from a node.
// Note: this is defined as a variable so it can be easily mocked in tests.
var evictPod = func(ctx context.Context, client kubernetes.Interface, logger *log.Entry, pod v1.Pod) error {
localLogger := logger.WithFields(log.Fields{
"ns": pod.Namespace,
"pod": pod.Name,
"node": pod.Spec.NodeName,
})
updated, err := client.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
if err != nil {
if apiErrors.IsNotFound(err) {
return nil
}
return err
}
if podTerminated(updated) {
// Completed, just ignore
return nil
}
if updated.Spec.NodeName != pod.Spec.NodeName {
// Already evicted
return nil
}
eviction := &policy.Eviction{
ObjectMeta: metav1.ObjectMeta{
Name: pod.Name,
Namespace: pod.Namespace,
},
DeleteOptions: &metav1.DeleteOptions{
GracePeriodSeconds: pod.Spec.TerminationGracePeriodSeconds,
},
}
err = client.CoreV1().Pods(pod.Namespace).Evict(ctx, eviction)
if err != nil {
if apiErrors.IsNotFound(err) {
return nil
}
return err
}
localLogger.Info("Evicting pod")
// wait for the pod to be actually evicted and gone from the node.
// It has TerminationGracePeriodSeconds time to clean up.
start := time.Now().UTC()
err = waitForPodTermination(ctx, client, pod)
if err != nil {
localLogger.Warnf("Pod not terminated within grace period: %s", err)
}
localLogger.Infof("Pod evicted. (Observed termination period: %s)", time.Now().UTC().Sub(start))
return nil
}
// waitForPodTermination waits for a pod to be terminated by looking up the pod
// in the API server.
// It waits for up to TerminationGracePeriodSeconds as specified on the pod +
// an additional eviction head room.
// This is to fully respect the termination expectations as described in:
// https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods
func waitForPodTermination(ctx context.Context, client kubernetes.Interface, pod v1.Pod) error {
if pod.Spec.TerminationGracePeriodSeconds == nil {
// if no grace period is defined, we don't wait.
return nil
}
waitForTermination := func() error {
newpod, err := client.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
if err != nil {
if apiErrors.IsNotFound(err) {
return nil
}
return err
}
// statefulset pods have the same name after restart, check the uid as well
if newpod.GetObjectMeta().GetUID() == pod.GetObjectMeta().GetUID() {
return fmt.Errorf("pod not terminated")
}
return nil
}
gracePeriod := time.Duration(*pod.Spec.TerminationGracePeriodSeconds)*time.Second + podEvictionHeadroom
backoffCfg := backoff.NewExponentialBackOff()
backoffCfg.MaxElapsedTime = gracePeriod
return backoff.Retry(waitForTermination, backoffCfg)
}
// evictablePods returns all evictable pods currently scheduled to a node, regardless of their status.
func (m *KubernetesNodePoolManager) evictablePods(ctx context.Context, nodeName string) ([]v1.Pod, error) {
opts := metav1.ListOptions{
FieldSelector: fmt.Sprintf("spec.nodeName=%s", nodeName),
}
podList, err := m.kube.CoreV1().Pods(v1.NamespaceAll).List(ctx, opts)
if err != nil {
return nil, err
}
var result []v1.Pod
for _, pod := range podList.Items {
if m.isEvictablePod(pod) {
result = append(result, pod)
}
}
return result, nil
}
func (m *KubernetesNodePoolManager) getPodsByNamespace(ctx context.Context, namespace string) (*v1.PodList, error) {
return m.kube.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
}
func (m *KubernetesNodePoolManager) getPDBsByNamespace(ctx context.Context, namespace string) (*policy.PodDisruptionBudgetList, error) {
return m.kube.PolicyV1beta1().PodDisruptionBudgets(namespace).List(ctx, metav1.ListOptions{})
}
// isEvictablePod detects whether it makes sense to evict a pod.
// Non-evictable pods are pods managed by DaemonSets and mirror pods.
func (m *KubernetesNodePoolManager) isEvictablePod(pod v1.Pod) bool {
logger := m.logger.WithFields(log.Fields{
"ns": pod.Namespace,
"pod": pod.Name,
"node": pod.Spec.NodeName,
})
if _, ok := pod.Annotations[mirrorPodAnnotation]; ok {
logger.Debug("Mirror Pod not evictable")
return false
}
if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
logger.Debug("Pod terminated")
return false
}
for _, owner := range pod.GetOwnerReferences() {
if owner.Kind == "DaemonSet" {
logger.Debug("DaemonSet Pod not evictable")
return false
}
}
return true
}