-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes_client.go
456 lines (385 loc) · 11.8 KB
/
kubernetes_client.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
package main
import (
"context"
"fmt"
"math"
"strings"
"sync"
"time"
"github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
"k8s.io/api/policy/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
//go:generate mockgen -package=main -destination ./kubernetes_client_mock.go -source=kubernetes_client.go
type KubernetesClient interface {
DrainNode(ctx context.Context, nodeName string, drainTimeout int) (err error)
DrainKubeDNSFromNode(ctx context.Context, nodeName string, drainTimeout int) (err error)
GetNode(ctx context.Context, nodeName string) (node *v1.Node, err error)
DeleteNode(ctx context.Context, nodeName string) (err error)
GetPreemptibleNodes(ctx context.Context, filters map[string]string) (nodes *v1.NodeList, err error)
GetProjectIdAndZoneFromNode(ctx context.Context, nodeName string) (projectID string, zone string, err error)
SetNodeAnnotation(ctx context.Context, nodeName string, key string, value string) (err error)
SetUnschedulableState(ctx context.Context, nodeName string, unschedulable bool) (err error)
}
// NewKubernetesClient return a Kubernetes client
func NewKubernetesClient(kubeClientset *kubernetes.Clientset) (kubernetes KubernetesClient, err error) {
return &kubernetesClient{
kubeClientset: kubeClientset,
}, nil
}
type kubernetesClient struct {
kubeClientset *kubernetes.Clientset
}
// GetProjectIdAndZoneFromNode returns project id and zone from given node name
// by getting informations from node spec provider id
func (c *kubernetesClient) GetProjectIdAndZoneFromNode(ctx context.Context, nodeName string) (projectID string, zone string, err error) {
node, err := c.GetNode(ctx, nodeName)
if err != nil {
return
}
s := strings.Split(node.Spec.ProviderID, "/")
projectID = s[2]
zone = s[3]
return
}
// GetPreemptibleNodes return a list of preemptible node
func (c *kubernetesClient) GetPreemptibleNodes(ctx context.Context, filters map[string]string) (nodes *v1.NodeList, err error) {
labelSelector := labels.Set{
"yandex.cloud/preemptible": "true",
}
for key, value := range filters {
labelSelector[key] = value
}
nodes, err = c.kubeClientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{
LabelSelector: labelSelector.String(),
})
if err != nil {
return
}
return
}
// GetNode return the node object from given name
func (c *kubernetesClient) GetNode(ctx context.Context, nodeName string) (node *v1.Node, err error) {
node, err = c.kubeClientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
if err != nil {
return
}
return
}
func (c *kubernetesClient) DeleteNode(ctx context.Context, nodeName string) (err error) {
err = c.kubeClientset.CoreV1().Nodes().Delete(ctx, nodeName, metav1.DeleteOptions{})
if err != nil {
return
}
return
}
// SetNodeAnnotation add an annotation (key/value) to a node from a given node name
// As the nodes are constantly being updated, the k8s client doesn't support patch feature yet and
// to reduce the chance to hit a failure 409 we fetch the node before update
func (c *kubernetesClient) SetNodeAnnotation(ctx context.Context, nodeName string, key string, value string) (err error) {
newNode, err := c.GetNode(ctx, nodeName)
if err != nil {
err = fmt.Errorf("Error getting node information before setting annotation:\n%v", err)
return
}
newNode.ObjectMeta.Annotations[key] = value
_, err = c.kubeClientset.CoreV1().Nodes().Update(ctx, newNode, metav1.UpdateOptions{})
if err != nil {
return
}
return
}
// SetUnschedulableState set the unschedulable state of a given node name
func (c *kubernetesClient) SetUnschedulableState(ctx context.Context, nodeName string, unschedulable bool) (err error) {
node, err := c.GetNode(ctx, nodeName)
if err != nil {
err = fmt.Errorf("Error getting node information before setting unschedulable state:\n%v", err)
return
}
node.Spec.Unschedulable = unschedulable
_, err = c.kubeClientset.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
if err != nil {
return
}
return
}
// filterOutPodByOwnerReferenceKind filter out a list of pods by its owner references kind
func filterOutPodByOwnerReferenceKind(podList []v1.Pod, kind string) (output []v1.Pod) {
for _, pod := range podList {
for _, ownerReference := range pod.ObjectMeta.OwnerReferences {
if ownerReference.Kind != kind {
output = append(output, pod)
}
}
}
return
}
// filterOutPodByNode filters out a list of pods by its node
func filterOutPodByNode(podList []v1.Pod, nodeName string) (output []v1.Pod) {
for _, pod := range podList {
if pod.Spec.NodeName == nodeName {
output = append(output, pod)
}
}
return
}
// DrainNode delete every pods from a given node and wait that all pods are removed before it succeed
// it also make sure we don't select DaemonSet because they are not subject to unschedulable state
func (c *kubernetesClient) DrainNode(ctx context.Context, nodeName string, drainTimeout int) (err error) {
// Select all pods sitting on the node except the one from kube-system
fieldSelector := fmt.Sprintf("spec.nodeName=%v,metadata.namespace!=kube-system", nodeName)
podList, err := c.kubeClientset.CoreV1().Pods("").List(ctx, metav1.ListOptions{
FieldSelector: fieldSelector,
})
if err != nil {
return
}
// Filter out DaemonSet from the list of pods
filteredPodList := filterOutPodByOwnerReferenceKind(podList.Items, "DaemonSet")
log.Info().
Str("host", nodeName).
Msgf("%d pod(s) found", len(filteredPodList))
stopEvicting := make(chan bool)
stopPolling := make(chan bool)
errCh := make(chan error)
defer func() {
if len(errCh) > 0 {
err = <-errCh
}
}()
go func() {
if err := c.evictPods(ctx, filteredPodList, stopEvicting); err != nil {
errCh <- err
}
}()
doneDraining := make(chan bool)
// Wait until all pods are deleted
go func() {
for {
sleepTime := ApplyJitter(10)
sleepDuration := time.Duration(sleepTime) * time.Second
pendingPodList, err := c.kubeClientset.CoreV1().Pods("").List(ctx, metav1.ListOptions{
FieldSelector: fieldSelector,
})
if err != nil {
log.Error().
Err(err).
Str("host", nodeName).
Msgf("Error getting list of pods, sleeping %ds", sleepTime)
time.Sleep(sleepDuration)
continue
}
// Filter out DaemonSet from the list of pods
filteredPendingPodList := filterOutPodByOwnerReferenceKind(pendingPodList.Items, "DaemonSet")
podsPending := len(filteredPendingPodList)
if podsPending == 0 {
doneDraining <- true
return
}
log.Info().
Str("host", nodeName).
Msgf("%d pod(s) pending deletion, sleeping %ds", podsPending, sleepTime)
select {
case <-stopPolling:
return
default:
time.Sleep(sleepDuration)
}
}
}()
select {
case <-doneDraining:
break
case <-time.After(time.Duration(drainTimeout) * time.Second):
log.Warn().
Str("host", nodeName).
Msg("Draining node timeout reached")
close(stopPolling)
close(stopEvicting)
return
case <-ctx.Done():
close(stopPolling)
close(stopEvicting)
return
}
log.Info().
Str("host", nodeName).
Msg("Done draining node")
return
}
// DrainKubeDNSFromNode deletes any kube-dns pods running on the node
func (c *kubernetesClient) DrainKubeDNSFromNode(ctx context.Context, nodeName string, drainTimeout int) (err error) {
// Select all pods sitting on the node except the one from kube-system
labelSelector := labels.Set{
"k8s-app": "kube-dns",
}
podList, err := c.kubeClientset.CoreV1().Pods("kube-system").List(ctx, metav1.ListOptions{
LabelSelector: labelSelector.String(),
})
if err != nil {
return
}
// Filter out pods running on other nodes
filteredPodList := filterOutPodByNode(podList.Items, nodeName)
log.Info().
Str("host", nodeName).
Msgf("%d kube-dns pod(s) found", len(filteredPodList))
stopEvicting := make(chan bool)
stopPolling := make(chan bool)
errCh := make(chan error)
defer func() {
if len(errCh) > 0 {
err = <-errCh
}
}()
go func() {
if err := c.evictPods(ctx, filteredPodList, stopEvicting); err != nil {
errCh <- err
}
}()
doneDraining := make(chan bool)
// Wait until all pods are deleted
go func() {
for {
sleepTime := ApplyJitter(10)
sleepDuration := time.Duration(sleepTime) * time.Second
podList, err := c.kubeClientset.CoreV1().Pods("kube-system").List(ctx, metav1.ListOptions{
LabelSelector: labelSelector.String(),
})
if err != nil {
log.Error().
Err(err).
Str("host", nodeName).
Msgf("Error getting list of kube-dns pods, sleeping %ds", sleepTime)
time.Sleep(sleepDuration)
continue
}
// Filter out DaemonSet from the list of pods
filteredPendingPodList := filterOutPodByNode(podList.Items, nodeName)
podsPending := len(filteredPendingPodList)
if podsPending == 0 {
doneDraining <- true
return
}
log.Info().
Str("host", nodeName).
Msgf("%d pod(s) pending deletion, sleeping %ds", podsPending, sleepTime)
select {
case <-stopPolling:
return
default:
time.Sleep(sleepDuration)
}
}
}()
select {
case <-doneDraining:
break
case <-time.After(time.Duration(drainTimeout) * time.Second):
log.Warn().
Str("host", nodeName).
Msg("Draining kube-dns node timeout reached")
close(stopPolling)
close(stopEvicting)
return
case <-ctx.Done():
close(stopPolling)
close(stopEvicting)
return
}
log.Info().
Str("host", nodeName).
Msg("Done draining kube-dns from node")
return
}
func (c *kubernetesClient) evictPods(ctx context.Context, pods []v1.Pod, stop <-chan bool) (lastErr error) {
podsPerBatch := 10
numPodsLeft := len(pods)
podsProcessedSoFar := 0
errCh := make(chan error)
defer func() {
if len(errCh) > 0 {
thisErr := <-errCh
lastErr = fmt.Errorf("error evicting pods, last error was: %s", thisErr.Error())
}
}()
for numPodsLeft > 0 {
numPodsThisBatch := int(math.Min(float64(numPodsLeft), float64(podsPerBatch)))
podsThisBatch := pods[podsProcessedSoFar : podsProcessedSoFar+numPodsThisBatch]
stopChs := make([]chan bool, numPodsThisBatch)
for i := 0; i < len(stopChs); i++ {
stopChs[i] = make(chan bool)
}
wg := &sync.WaitGroup{}
for i := 0; i < numPodsThisBatch; i++ {
wg.Add(1)
go func(i int) {
thisPod := podsThisBatch[i]
if err := c.evictPod(ctx, thisPod, stopChs[i]); err != nil {
log.Error().
Err(err).
Msgf("failed to evict pod %s", thisPod.Name)
errCh <- err
}
wg.Done()
}(i)
}
select {
case <-stop:
for _, ch := range stopChs {
close(ch)
}
return
default:
wg.Wait()
numPodsLeft -= numPodsThisBatch
podsProcessedSoFar += numPodsThisBatch
}
}
return
}
func (c *kubernetesClient) evictPod(ctx context.Context, pod v1.Pod, stop <-chan bool) error {
log.Info().
Str("host", pod.Spec.NodeName).
Msgf("Evicting pod %s", pod.Name)
eviction := &v1beta1.Eviction{
ObjectMeta: metav1.ObjectMeta{
Name: pod.Name,
Namespace: pod.Namespace,
},
}
for {
err := c.kubeClientset.PolicyV1beta1().Evictions(eviction.Namespace).Evict(ctx, eviction)
if err == nil {
log.Info().
Msgf("pod %s evicted", pod.Name)
break
} else if errors.IsNotFound(err) {
log.Info().
Msgf("pod %s already gone", pod.Name)
break
} else if errors.IsTooManyRequests(err) { //We get a 429 in the case of disruption budget related failures
log.Info().
Err(err).
Msgf("too many evictions while evicting %s, this may be due to pod disruption budget. trying again soon", pod.Name)
time.Sleep(5 * time.Second)
} else if errors.IsForbidden(err) && errors.HasStatusCause(err, v1.NamespaceTerminatingCause) {
log.Warn().
Msgf("cannot evict %s, namespace is being deleted", pod.Name)
//namespace is being deleted, finalizers should take care of deleting the pod
break
} else {
return err
}
select {
case <-stop:
return nil
default:
}
}
return nil
}