-
Notifications
You must be signed in to change notification settings - Fork 3
/
drain_node_handler.go
283 lines (241 loc) · 7.32 KB
/
drain_node_handler.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
package actions
import (
"context"
"errors"
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
v1 "k8s.io/api/core/v1"
"k8s.io/api/policy/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"github.com/castai/cluster-controller/castai"
)
var (
errPodPresent = errors.New("pod is still present")
)
type drainNodeConfig struct {
podsDeleteTimeout time.Duration
podDeleteRetries uint64
podDeleteRetryDelay time.Duration
podEvictRetryDelay time.Duration
}
func newDrainNodeHandler(log logrus.FieldLogger, clientset kubernetes.Interface) ActionHandler {
return &drainNodeHandler{
log: log,
clientset: clientset,
cfg: drainNodeConfig{
podsDeleteTimeout: 5 * time.Minute,
podDeleteRetries: 5,
podDeleteRetryDelay: 5 * time.Second,
podEvictRetryDelay: 5 * time.Second,
},
}
}
type drainNodeHandler struct {
log logrus.FieldLogger
clientset kubernetes.Interface
cfg drainNodeConfig
}
func (h *drainNodeHandler) Handle(ctx context.Context, data interface{}) error {
req, ok := data.(*castai.ActionDrainNode)
if !ok {
return fmt.Errorf("unexpected type %T for delete drain handler", data)
}
log := h.log.WithField("node_name", req.NodeName)
node, err := h.clientset.CoreV1().Nodes().Get(ctx, req.NodeName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
log.Info("node not found, skipping draining")
return nil
}
return err
}
log.Infof("draining node, drain_timeout_seconds=%d, force=%v", req.DrainTimeoutSeconds, req.Force)
if err := h.taintNode(ctx, node); err != nil {
return fmt.Errorf("tainting node %q: %w", req.NodeName, err)
}
pods, err := h.listNodePods(ctx, node)
if err != nil {
return fmt.Errorf("listing pods for node %q: %w", req.NodeName, err)
}
// First try to evict pods gracefully.
evictCtx, evictCancel := context.WithTimeout(ctx, time.Duration(req.DrainTimeoutSeconds)*time.Second)
defer evictCancel()
err = h.evictPods(evictCtx, log, pods)
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
return err
}
if errors.Is(err, context.DeadlineExceeded) {
if !req.Force {
return err
}
// If force is set and evict timeout exceeded delete pods.
deleteCtx, deleteCancel := context.WithTimeout(ctx, h.cfg.podsDeleteTimeout)
defer deleteCancel()
if err := h.deletePods(deleteCtx, log, pods.Items); err != nil {
return err
}
}
log.Info("node drained")
return nil
}
func (h *drainNodeHandler) deletePods(ctx context.Context, log logrus.FieldLogger, pods []v1.Pod) error {
log.Infof("forcefully deleting %d pods", len(pods))
g, ctx := errgroup.WithContext(ctx)
for _, pod := range pods {
pod := pod
if daemonSetPod(pod) {
continue
}
g.Go(func() error {
err := h.deletePod(ctx, pod)
if err != nil {
return err
}
return h.waitPodTerminated(ctx, log, pod)
})
}
if err := g.Wait(); err != nil {
return fmt.Errorf("deleting pods: %w", err)
}
return nil
}
func (h *drainNodeHandler) deletePod(ctx context.Context, pod v1.Pod) error {
b := backoff.WithContext(backoff.WithMaxRetries(backoff.NewConstantBackOff(h.cfg.podDeleteRetryDelay), h.cfg.podDeleteRetries), ctx) // nolint:gomnd
action := func() error {
err := h.clientset.CoreV1().Pods(pod.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{})
if err != nil {
// Pod is not found - ignore.
if apierrors.IsNotFound(err) {
return nil
}
// Pod is misconfigured - stop retry.
if apierrors.IsInternalError(err) {
return backoff.Permanent(err)
}
}
// Other errors - retry.
return err
}
if err := backoff.Retry(action, b); err != nil {
return fmt.Errorf("deleting pod %s in namespace %s: %w", pod.Name, pod.Namespace, err)
}
return nil
}
// taintNode to make it unshedulable.
func (h *drainNodeHandler) taintNode(ctx context.Context, node *v1.Node) error {
if node.Spec.Unschedulable {
return nil
}
err := patchNode(ctx, h.clientset, node, func(n *v1.Node) error {
n.Spec.Unschedulable = true
return nil
})
if err != nil {
return fmt.Errorf("patching node unschedulable: %w", err)
}
return nil
}
// listNodePods returns a list of all pods scheduled on the provided node.
func (h *drainNodeHandler) listNodePods(ctx context.Context, node *v1.Node) (*v1.PodList, error) {
var pods *v1.PodList
err := backoff.Retry(func() error {
p, err := h.clientset.CoreV1().Pods(metav1.NamespaceAll).List(ctx, metav1.ListOptions{
FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": node.Name}).String(),
})
if err != nil {
return err
}
pods = p
return nil
}, defaultBackoff(ctx))
return pods, err
}
func (h *drainNodeHandler) evictPods(ctx context.Context, log logrus.FieldLogger, pods *v1.PodList) error {
log.Infof("evicting %d pods", len(pods.Items))
g, ctx := errgroup.WithContext(ctx)
for _, pod := range pods.Items {
pod := pod
if daemonSetPod(pod) {
continue
}
g.Go(func() error {
err := h.evictPod(ctx, pod)
if err != nil {
return err
}
return h.waitPodTerminated(ctx, log, pod)
})
}
if err := g.Wait(); err != nil {
return fmt.Errorf("evicting pods: %w", err)
}
return nil
}
func (h *drainNodeHandler) waitPodTerminated(ctx context.Context, log logrus.FieldLogger, pod v1.Pod) error {
b := backoff.WithContext(backoff.NewConstantBackOff(5*time.Second), ctx) // nolint:gomnd
err := backoff.Retry(func() error {
p, err := h.clientset.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
if err != nil && apierrors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
// replicaSets will recreate pods with equal name and namespace, therefore we compare UIDs
if p.GetUID() == pod.GetUID() {
return errPodPresent
}
return nil
}, b)
if err != nil && errors.Is(err, errPodPresent) {
log.Infof("timeout waiting for pod %s in namespace %s to terminate", pod.Name, pod.Namespace)
return nil
}
if err != nil {
return fmt.Errorf("waiting for pod %s in namespace %s termination: %w", pod.Name, pod.Namespace, err)
}
return nil
}
// evictPod from the k8s node. Error handling is based on eviction api documentation:
// https://kubernetes.io/docs/tasks/administer-cluster/safely-drain-node/#the-eviction-api
func (h *drainNodeHandler) evictPod(ctx context.Context, pod v1.Pod) error {
b := backoff.WithContext(backoff.NewConstantBackOff(h.cfg.podEvictRetryDelay), ctx) // nolint:gomnd
action := func() error {
err := h.clientset.CoreV1().Pods(pod.Namespace).Evict(ctx, &v1beta1.Eviction{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy/v1beta1",
Kind: "Eviction",
},
ObjectMeta: metav1.ObjectMeta{
Name: pod.Name,
Namespace: pod.Namespace,
},
})
if err != nil {
// Pod is not found - ignore.
if apierrors.IsNotFound(err) {
return nil
}
// Pod is misconfigured - stop retry.
if apierrors.IsInternalError(err) {
return backoff.Permanent(err)
}
}
// Other errors - retry.
return err
}
if err := backoff.Retry(action, b); err != nil {
return fmt.Errorf("evicting pod %s in namespace %s: %w", pod.Name, pod.Namespace, err)
}
return nil
}
func daemonSetPod(pod v1.Pod) bool {
controller := metav1.GetControllerOf(&pod)
return controller != nil && controller.Kind == "DaemonSet"
}