This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
node.go
66 lines (56 loc) · 1.66 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
package cmd
import (
"context"
"github.com/flanksource/karina/pkg/constants"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var Node = &cobra.Command{
Use: "node",
Short: "Commands for interacting with Kubernetes nodes",
}
func init() {
annotate := &cobra.Command{
Use: "annotate",
Short: "Annotate nodes using annotations on vm master / worker pools",
Run: func(cmd *cobra.Command, args []string) {
platform := getPlatform(cmd)
clientset, err := platform.GetClientset()
if err != nil {
platform.Fatalf("failed to get clientset: %s", err)
}
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
platform.Fatalf("failed to list nodes: %v", err)
}
for _, node := range nodes.Items {
_, isMaster := node.Labels[constants.MasterNodeLabel]
if isMaster {
for k, v := range platform.Master.Annotations {
node.Annotations[k] = v
}
} else {
nodePool, found := node.Labels[constants.NodePoolLabel]
if !found {
continue
}
workerPool, found := platform.Nodes[nodePool]
if !found {
platform.Errorf("Node %s has node pool %s which was not found in platform.Nodes", node.Name, nodePool)
continue
}
annotations := workerPool.Annotations
for k, v := range annotations {
node.Annotations[k] = v
}
}
if _, err := clientset.CoreV1().Nodes().Update(context.TODO(), &node, metav1.UpdateOptions{}); err != nil {
platform.Errorf("Failed to update node %s: %v", node, err)
continue
}
platform.Infof("Node %s annotated", node.Name)
}
},
}
Node.AddCommand(annotate)
}