-
Notifications
You must be signed in to change notification settings - Fork 13
/
nodes.go
273 lines (230 loc) · 6.56 KB
/
nodes.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
package kuber
import (
"fmt"
"strings"
"github.com/MagalixTechnologies/uuid-go"
kapi "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
const NodeRoleLabelPrefix = "node-role.kubernetes.io/"
type Node struct {
ID uuid.UUID `json:"id,omitempty"`
Name string `json:"name"`
IP string `json:"ip"`
Roles string `json:"roles"`
KubeletPort int32 `json:"port"`
Provider string `json:"provider,omitempty"`
Region string `json:"region,omitempty"`
InstanceType string `json:"instance_type,omitempty"`
InstanceSize string `json:"instance_size,omitempty"`
Capacity NodeCapacity `json:"capacity"`
Allocatable NodeCapacity `json:"allocatable"`
Containers int `json:"containers,omitempty"`
ContainerList []*Container `json:"container_list,omitempty"`
}
// Container user type.
type Container struct {
// cluster where host of container located in
Cluster string `json:"cluster,omitempty"`
// image of container
Image string `json:"image"`
// limits of container
Limits *ContainerResources `json:"limits,omitempty"`
// requests of container
Requests *ContainerResources `json:"requests,omitempty"`
// name of container (not guaranteed to be unique in cluster scope)
Name string `json:"name"`
// namespace where pod located in
Namespace string `json:"namespace"`
// node where container located in
Node string `json:"node"`
// pod where container located in
Pod string `json:"pod"`
}
// ContainerResources user type.
type ContainerResources struct {
CPU int `json:"cpu"`
Memory int `json:"memory"`
}
func resourceListToContainerResources(resourceList kapi.ResourceList) *ContainerResources {
res := &ContainerResources{}
if memory := resourceList.Memory(); memory != nil {
// TODO: account for differences between megabytes and mebibytes
memoryMB := memory.ScaledValue(resource.Mega)
res.Memory = int(memoryMB)
}
if cpu := resourceList.Cpu(); cpu != nil && !cpu.IsZero() {
res.CPU = int(cpu.MilliValue())
}
return res
}
type NodeCapacity struct {
CPU int `json:"cpu"`
Memory int `json:"memory"`
StorageEphemeral int `json:"storage_ephemeral"`
Pods int `json:"pods"`
}
func GetContainersByNode(pods []kapi.Pod) map[string]int {
containers := map[string]int{}
for _, pod := range pods {
containers[pod.Spec.NodeName] += len(pod.Spec.Containers)
}
return containers
}
func UpdateNodesContainers(nodes []Node, containers map[string]int) []Node {
for n, node := range nodes {
node.Containers, _ = containers[node.Name]
nodes[n] = node
}
return nodes
}
func AddContainerListToNodes(
nodes []Node,
pods []kapi.Pod,
namespace *string,
pod *string,
container *string,
) []Node {
containers, err := GetContainers(pods, namespace, pod, container)
if err != nil {
return nil
}
for k := range nodes {
node := &nodes[k]
node.ContainerList = make([]*Container, 0)
for _, container := range containers {
if container.Node == node.Name {
node.ContainerList = append(node.ContainerList, container)
}
}
}
return nodes
}
func getWorkerNodesMap(nodes []*Node) map[string]*Node {
table := map[string]*Node{}
for _, node := range nodes {
table[node.Name] = node
}
return table
}
func RangePods(
pods []kapi.Pod,
fn func(kapi.Pod) bool) error {
for _, pod := range pods {
if !fn(pod) {
return nil
}
}
return nil
}
func GetContainers(
pods []kapi.Pod,
namespace *string,
pod *string,
container *string,
) ([]*Container, error) {
containers := []*Container{}
err := RangePods(
pods,
func(kpod kapi.Pod) bool {
if namespace != nil && kpod.Namespace != *namespace {
return true
}
if pod != nil && kpod.Name != *pod {
return true
}
for _, kcontainer := range kpod.Spec.Containers {
if container != nil && kcontainer.Name != *container {
continue
}
limits := kcontainer.Resources.Limits
requests := kcontainer.Resources.Requests
containers = append(containers, &Container{
Node: kpod.Spec.NodeName,
Pod: kpod.Name,
Namespace: kpod.Namespace,
Name: kcontainer.Name,
Image: kcontainer.Image,
Limits: resourceListToContainerResources(limits),
Requests: resourceListToContainerResources(requests),
})
}
return true
})
if err != nil {
return nil, err
}
return containers, nil
}
func GetNodes(nodes []kapi.Node) []Node {
result := []Node{}
for _, node := range nodes {
labels := node.Labels
var address string
for _, addr := range node.Status.Addresses {
if addr.Type == kapi.NodeInternalIP {
address = addr.Address
}
}
instanceType, cloudProvider := labels["beta.kubernetes.io/instance-type"]
instanceSize := ""
capacity := GetNodeCapacity(node.Status.Capacity)
if cloudProvider {
_, gcloud := labels["cloud.google.com/gke-nodepool"]
if gcloud {
if strings.Contains(instanceType, "-") {
parts := strings.SplitN(instanceType, "-", 2)
instanceType, instanceSize = parts[0], parts[1]
}
} else {
if strings.Contains(instanceType, ".") {
parts := strings.SplitN(instanceType, ".", 2)
instanceType, instanceSize = parts[0], parts[1]
}
}
} else {
// for custom on-perm clusters we use node capacity as instance type
instanceType = "custom"
cpuCores := capacity.CPU / 1000
memoryGi := float64(capacity.Memory) / 1024 / 1024 / 1024
instanceSize = fmt.Sprintf(
"cpu-%d--memory-%.2f",
cpuCores,
memoryGi,
)
}
provider := strings.Split(node.Spec.ProviderID, ":")[0]
var nodeRoles []string
for key := range labels {
if strings.HasPrefix(key, NodeRoleLabelPrefix) {
nodeRoles = append(
nodeRoles,
strings.Replace(key, NodeRoleLabelPrefix, "", 1),
)
break
}
}
result = append(result, Node{
Name: node.ObjectMeta.Name,
IP: address,
Roles: strings.Join(nodeRoles, ","),
KubeletPort: node.Status.DaemonEndpoints.KubeletEndpoint.Port,
Region: labels["failure-domain.beta.kubernetes.io/region"],
InstanceType: instanceType,
InstanceSize: instanceSize,
Provider: provider,
Capacity: capacity,
Allocatable: GetNodeCapacity(node.Status.Allocatable),
})
}
return result
}
func GetNodeCapacity(resources kapi.ResourceList) NodeCapacity {
capacity := NodeCapacity{
CPU: int(resources.Cpu().MilliValue()),
Memory: int(resources.Memory().Value()),
StorageEphemeral: int(resources.StorageEphemeral().Value()),
Pods: int(resources.Pods().Value()),
}
return capacity
}