-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_info.go
87 lines (71 loc) · 1.62 KB
/
node_info.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
package main
import (
"context"
"strconv"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type NodeInfo struct {
Name string
InternalIP string
GPUCount int
EachGpuMemory int64
}
func getNodes() []NodeInfo {
result := make([]NodeInfo, 0)
nodeList, err := clientSet.CoreV1().Nodes().List(context.Background(), meta.ListOptions{})
if err != nil {
return result
}
for _, node := range nodeList.Items {
if isNodeLabelExist(node, LabelGPUManagerKey, LabelGPUManagerValue) || isNodeLabelExist(node, LabelVCudaKey, "") {
info := NodeInfo{
Name: node.Name,
InternalIP: getInternalIPFromNode(node),
GPUCount: getGPUCountFromNode(node),
EachGpuMemory: getGPUMemoryFromNode(node),
}
result = append(result, info)
}
}
return result
}
func isNodeLabelExist(node core.Node, key, value string) bool {
labels := node.GetLabels()
if v, ok := labels[key]; ok {
if v == value {
return true
}
}
return false
}
func getInternalIPFromNode(node core.Node) string {
for _, address := range node.Status.Addresses {
if address.Type == StatusAddressInternalIP {
return address.Address
}
}
return ""
}
func getGPUCountFromNode(node core.Node) int {
labels := node.GetLabels()
if value, ok := labels[LabelGPUCount]; ok {
v, err := strconv.Atoi(value)
if err != nil {
return 0
}
return v
}
return 0
}
func getGPUMemoryFromNode(node core.Node) int64 {
labels := node.GetLabels()
if value, ok := labels[LabelGPUMemory]; ok {
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return 0
}
return v
}
return 0
}