forked from Qihoo360/wayne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
113 lines (94 loc) · 2.75 KB
/
Copy pathnode.go
File metadata and controls
113 lines (94 loc) · 2.75 KB
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
package node
import (
"strconv"
"k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type NodeStatistics struct {
Total int `json:"total,omitempty"`
Details map[string]int `json:"details,omitempty"`
}
type Node struct {
Name string `json:"name,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
CreationTimestamp metaV1.Time `json:"creationTimestamp"`
Spec NodeSpec `json:"spec,omitempty"`
Status NodeStatus `json:"status,omitempty"`
}
type NodeSpec struct {
Unschedulable bool `json:"unschedulable"`
// If specified, the node's taints.
// +optional
Taints []v1.Taint `json:"taints,omitempty"`
Ready v1.ConditionStatus `json:"ready"`
}
type NodeStatus struct {
Capacity map[v1.ResourceName]string `json:"capacity,omitempty"`
NodeInfo v1.NodeSystemInfo `json:"nodeInfo,omitempty"`
}
func GetNodeCounts(cli *kubernetes.Clientset) (int, error) {
nodes, err := cli.CoreV1().Nodes().List(metaV1.ListOptions{})
if err != nil {
return 0, err
}
return len(nodes.Items), nil
}
func ListNode(cli *kubernetes.Clientset, listOptions metaV1.ListOptions) ([]Node, error) {
nodeList, err := cli.CoreV1().Nodes().List(listOptions)
if err != nil {
return nil, err
}
nodes := make([]Node, 0)
for _, node := range nodeList.Items {
nodes = append(nodes, toNode(node))
}
return nodes, nil
}
func toNode(knode v1.Node) Node {
node := Node{
Name: knode.Name,
Labels: knode.Labels,
CreationTimestamp: knode.CreationTimestamp,
Spec: NodeSpec{
Unschedulable: knode.Spec.Unschedulable,
Taints: knode.Spec.Taints,
},
Status: NodeStatus{
NodeInfo: knode.Status.NodeInfo,
},
}
capacity := make(map[v1.ResourceName]string)
for resourceName, value := range knode.Status.Capacity {
if resourceName == v1.ResourceCPU {
// cpu unit core
capacity[resourceName] = strconv.Itoa(int(value.Value()))
}
if resourceName == v1.ResourceMemory {
// memory unit Gi
capacity[resourceName] = strconv.Itoa(int(value.Value() / (1024 * 1024 * 1024)))
}
}
node.Status.Capacity = capacity
for _, condition := range knode.Status.Conditions {
if condition.Type == v1.NodeReady {
node.Spec.Ready = condition.Status
}
}
return node
}
func UpdateNode(cli *kubernetes.Clientset, node *v1.Node) (*v1.Node, error) {
newNode, err := cli.CoreV1().Nodes().Update(node)
if err != nil {
return nil, err
}
return newNode, nil
}
func DeleteNode(cli *kubernetes.Clientset, name string) error {
return cli.CoreV1().Nodes().Delete(name, &metaV1.DeleteOptions{})
}
func GetNodeByName(cli *kubernetes.Clientset, name string) (*v1.Node, error) {
return cli.CoreV1().
Nodes().
Get(name, metaV1.GetOptions{})
}