-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
69 lines (56 loc) · 1.73 KB
/
node.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
package node
import (
"context"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/kubectl/pkg/drain"
)
func CordonNode(ctx context.Context, clientset kubernetes.Interface, node *v1.Node) error {
log := ctx.Value("logger").(*zap.SugaredLogger)
// check if the node is cordoned
if node.Spec.Unschedulable {
log.Infow("Node is already cordoned, skipping cordon", "node", node.Name)
return nil
}
// update the labels to show mechanic cordoned the node and cordon the node
node.Spec.Unschedulable = true
labels := node.GetLabels()
labels["mechanic.cordoned"] = "true"
node.SetLabels(labels)
_, err := clientset.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
if err != nil {
log.Errorw("Failed to cordon node", "node", node.Name, "error", err)
return err
}
log.Infow("Node cordoned", "node", node.Name)
return nil
}
func UncordonNode(ctx context.Context, clientset kubernetes.Interface, node *v1.Node) error {
labels := node.GetLabels()
delete(labels, "mechanic.cordoned")
node.Spec.Unschedulable = false
node.SetLabels(labels)
_, err := clientset.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}
func DrainNode(ctx context.Context, clientset kubernetes.Interface, node *v1.Node) error {
log := ctx.Value("logger").(*zap.SugaredLogger)
// drain the node
log.Infow("Beginning node drain", "node", node.Name)
drainHelper := &drain.Helper{
Client: clientset,
Force: true,
DeleteEmptyDirData: true,
IgnoreAllDaemonSets: true,
Timeout: 0,
}
if err := drain.RunNodeDrain(drainHelper, node.Name); err != nil {
return err
}
return nil
}