-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
149 lines (130 loc) · 3.04 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
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
/*
@Version : 1.0
@Author : steven.wong
@Email : 'wangxk1991@gamil.com'
@Time : 2024/03/22 23:08:47
Desc :
*/
package kube
import (
"context"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Node struct {
*LinkInfo
*v1.Node
client *KubeClient
ctx context.Context
}
func NewNode(ctx context.Context) *Node {
if ctx == nil {
ctx = context.TODO()
}
return &Node{
Node: &v1.Node{
TypeMeta: metav1.TypeMeta{
Kind: "Node",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{},
},
LinkInfo: &LinkInfo{},
client: nil,
ctx: ctx,
}
}
func (n *Node) Link(region, config string) *Node {
n.Region = region
n.Config = config
n.client = NewKubeClient(region, config)
return n
}
func (n *Node) Metadata(name string) *Node {
n.Name = name
return n
}
func (n *Node) Labels(labels map[string]string) *Node {
if n.Node.Labels == nil {
n.Node.Labels = make(map[string]string)
}
for k, v := range labels {
n.Node.Labels[k] = v
}
return n
}
func (n *Node) Annotations(annotations map[string]string) *Node {
if n.Node.Annotations == nil {
n.Node.Annotations = make(map[string]string)
}
for k, v := range annotations {
n.Node.Annotations[k] = v
}
return n
}
func (n *Node) Create() error {
Nodes := n.client.CoreV1().Nodes()
_, err := Nodes.Create(n.ctx, n.Node, metav1.CreateOptions{})
return err
}
func (n *Node) Delete() error {
Nodes := n.client.CoreV1().Nodes()
return Nodes.Delete(n.ctx, n.Name, metav1.DeleteOptions{})
}
func (n *Node) Update() error {
Nodes := n.client.CoreV1().Nodes()
_, err := Nodes.Update(n.ctx, n.Node, metav1.UpdateOptions{})
return err
}
func (n *Node) Get() (*v1.Node, error) {
return n.client.CoreV1().Nodes().Get(n.ctx, n.Name, metav1.GetOptions{})
}
func (n *Node) Fetch() (*Node, error) {
node, err := n.client.CoreV1().Nodes().Get(n.ctx, n.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
n.Node = node
return n, nil
}
func (n *Node) List() (*v1.NodeList, error) {
return n.client.CoreV1().Nodes().List(n.ctx, metav1.ListOptions{})
}
func (n *Node) ListPods() ([]v1.Pod, error) {
podList, err := n.client.CoreV1().Pods("").List(n.ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
var podsOnNode []v1.Pod
for _, pod := range podList.Items {
if pod.Spec.NodeName == n.Name {
podsOnNode = append(podsOnNode, pod)
}
}
return podsOnNode, nil
}
func (n *Node) CreateOrUpdate() error {
_, err := n.client.CoreV1().Nodes().Get(n.ctx, n.Name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return n.Create()
}
return err
}
return n.Update()
}
func (n *Node) Empty() bool {
_, err := n.client.CoreV1().Nodes().Get(n.ctx, n.Name, metav1.GetOptions{})
return errors.IsNotFound(err)
}
func (n *Node) Equal(keys []string) bool {
secret, err := n.Get()
if err != nil && !errors.IsNotFound(err) {
panic(err)
}
if len(keys) == 0 {
keys = []string{"Data", "Type", "StringData", "Immutable"}
}
return ResourceEqual(n.Node, secret, keys)
}