-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
148 lines (133 loc) · 4 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
package kube
import (
"context"
"fmt"
"os"
"time"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/transport"
)
const (
HostnameLabel = "kubernetes.io/hostname"
NodeRoleLabel = "node-role.kubernetes.io/master"
MaxRetries = 5
RetryInterval = 5
WrapTransportTimeout = 30
)
var (
KubeDefaultConfigPath = getUserHome() + "/.supkube/admin.conf"
)
func getUserHome() string {
home, err := os.UserHomeDir()
if err != nil {
fmt.Println("get user home err:", err)
os.Exit(-1)
}
return home
}
// NewClient is get clientSet by kubeConfig
func NewClient(kubeConfigPath string, k8sWrapTransport transport.WrapperFunc) (*kubernetes.Clientset, error) {
// use the current admin kubeconfig
var config *rest.Config
var err error
//if home, _ := os.UserHomeDir(); home != "" && kubeConfigPath != "" {
// kubeConfigPath = filepath.Join(home, ".kube", "config")
//}
if config, err = rest.InClusterConfig(); err != nil {
if config, err = clientcmd.BuildConfigFromFlags("", kubeConfigPath); err != nil {
return nil, err
}
}
if k8sWrapTransport != nil {
config.WrapTransport = k8sWrapTransport
}
config.Timeout = time.Second * time.Duration(WrapTransportTimeout)
K8sClientSet, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return K8sClientSet, nil
}
// GetNodeList is get all nodes
func GetNodeList(k8sClient *kubernetes.Clientset) (*v1.NodeList, error) {
return k8sClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
}
// GetNodeListByLabel is get node list by label
func GetNodeListByLabel(k8sClient *kubernetes.Clientset, label string) (*v1.NodeList, error) {
listOption := &metav1.ListOptions{LabelSelector: label}
return k8sClient.CoreV1().Nodes().List(context.TODO(), *listOption)
}
// GetNodeIPByName is get node internalIp by nodeName
func GetNodeIPByName(k8sClient *kubernetes.Clientset, nodeName string) (ip string, err error) {
node, err := k8sClient.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
if err != nil {
return "", err
}
for _, v := range node.Status.Addresses {
if v.Type == v1.NodeInternalIP {
ip = v.Address
return ip, nil
}
}
return "", apierrors.NewNotFound(schema.GroupResource{}, nodeName)
}
// GetNodeIPByLabel is is get node ip by label
func GetNodeIPByLabel(k8sClient *kubernetes.Clientset, label string) ([]string, error) {
var ips []string
if label == "" {
return ips, nil
}
nodes, err := GetNodeListByLabel(k8sClient, label)
if err != nil {
return nil, err
}
for _, node := range nodes.Items {
for _, v := range node.Status.Addresses {
if v.Type == v1.NodeInternalIP {
ips = append(ips, v.Address)
}
}
}
if len(ips) != 0 {
return ips, nil
}
return nil, fmt.Errorf("label %s is not fount in kubernetes nodes", label)
}
// GetNodeByName is get node internalIp by nodeName
func GetNodeByName(k8sClient *kubernetes.Clientset, nodeName string) (node *v1.Node, err error) {
return k8sClient.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
}
// IsNodeReady return true when node is ready
func IsNodeReady(node v1.Node) bool {
nodeConditions := node.Status.Conditions
for _, condition := range nodeConditions {
if condition.Type == v1.NodeReady && condition.Status == v1.ConditionTrue {
return true
}
}
return false
}
// TransToIP is use kubernetes label or hostname/ip to get ip
func TransToIP(k8sClient *kubernetes.Clientset, label string, hostname []string) ([]string, error) {
var ips []string
ips, err := GetNodeIPByLabel(k8sClient, label)
if err != nil {
return nil, err
}
resHost, resIP := getHostnameAndIP(hostname)
ips = append(ips, resIP...)
for _, node := range resHost {
ip, err := GetNodeIPByName(k8sClient, node)
if err == nil {
ips = append(ips, ip)
}
}
ips = removeRep(ips)
return ips, nil
}