-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
151 lines (134 loc) · 3.42 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
150
151
package node
import (
v1 "github.com/rancher/types/apis/core/v1"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
)
const (
externalAddressAnnotation = "rke.cattle.io/external-ip"
LabelNodeName = "management.cattle.io/nodename"
)
func GetNodeName(machine *v3.Node) string {
if machine.Status.NodeName != "" {
return machine.Status.NodeName
}
// to handle the case when machine was provisioned first
if machine.Status.NodeConfig != nil {
if machine.Status.NodeConfig.HostnameOverride != "" {
return machine.Status.NodeConfig.HostnameOverride
}
}
return ""
}
// IsNodeForNode returns true if node names or addresses are equal
func IsNodeForNode(node *corev1.Node, machine *v3.Node) bool {
nodeName := GetNodeName(machine)
if nodeName == node.Name {
return true
}
machineAddress := ""
if machine.Status.NodeConfig != nil {
if machine.Status.NodeConfig.InternalAddress == "" {
// rke defaults internal address to address
machineAddress = machine.Status.NodeConfig.Address
} else {
machineAddress = machine.Status.NodeConfig.InternalAddress
}
}
if machineAddress == "" {
return false
}
if machineAddress == GetNodeInternalAddress(node) {
return true
}
return false
}
func GetNodeInternalAddress(node *corev1.Node) string {
for _, address := range node.Status.Addresses {
if address.Type == corev1.NodeInternalIP {
return address.Address
}
}
return ""
}
func GetEndpointNodeIP(node *v3.Node) string {
externalIP := ""
internalIP := ""
for _, ip := range node.Status.InternalNodeStatus.Addresses {
if ip.Type == "ExternalIP" && ip.Address != "" {
externalIP = ip.Address
break
} else if ip.Type == "InternalIP" && ip.Address != "" {
internalIP = ip.Address
}
}
if externalIP != "" {
return externalIP
}
if node.Annotations != nil {
externalIP = node.Status.NodeAnnotations[externalAddressAnnotation]
if externalIP != "" {
return externalIP
}
}
return internalIP
}
func GetNodeByNodeName(nodes []*v3.Node, nodeName string) *v3.Node {
for _, m := range nodes {
if GetNodeName(m) == nodeName {
return m
}
}
return nil
}
func GetNodeForMachine(machine *v3.Node, nodeLister v1.NodeLister) (*corev1.Node, error) {
nodeName := ""
if machine.Labels != nil {
nodeName = machine.Labels[LabelNodeName]
}
var nodes []*corev1.Node
var err error
if nodeName != "" {
var node *corev1.Node
node, err = nodeLister.Get("", nodeName)
if err != nil && !apierrors.IsNotFound(err) {
return nil, err
}
if node != nil {
nodes = append(nodes, node)
}
}
if len(nodes) == 0 {
nodes, err = nodeLister.List("", labels.NewSelector())
if err != nil {
return nil, err
}
}
for _, n := range nodes {
if IsNodeForNode(n, machine) {
return n, nil
}
}
return nil, nil
}
func GetMachineForNode(node *corev1.Node, clusterNamespace string, machineLister v3.NodeLister) (*v3.Node, error) {
labelsSearchSet := labels.Set{LabelNodeName: node.Name}
machines, err := machineLister.List(clusterNamespace, labels.SelectorFromSet(labelsSearchSet))
if err != nil {
return nil, err
}
if len(machines) == 0 {
machines, err = machineLister.List(clusterNamespace, labels.NewSelector())
if err != nil {
return nil, err
}
}
for _, machine := range machines {
if IsNodeForNode(node, machine) {
return machine, nil
}
}
return nil, nil
}