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
84 lines (68 loc) · 2.01 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cmd
import (
"context"
"fmt"
"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 {
var annotations = platform.Master.Annotations
if nodePoolName, ok := node.Labels[constants.NodePoolLabel]; ok {
if pool, ok := platform.Nodes[nodePoolName]; ok {
annotations = pool.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)
}
},
}
ips := &cobra.Command{
Use: "ips",
Short: "List all internal node IP's",
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 {
for _, address := range node.Status.Addresses {
if address.Type == "InternalIP" {
fmt.Println(address.Address)
}
}
}
},
}
Node.AddCommand(annotate, ips)
}