-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode.go
More file actions
119 lines (104 loc) · 3.02 KB
/
node.go
File metadata and controls
119 lines (104 loc) · 3.02 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
114
115
116
117
118
119
package main
import (
"errors"
"k8s.io/client-go/pkg/api"
v1 "k8s.io/client-go/pkg/api/v1"
)
// Node models models a k8s worker node v1.Node
type Node struct {
Name string
Address NodeAddress
}
// NodeAddress models a k8s worker node address v1.NodeAddress (node.status.addresses)
type NodeAddress struct {
Hostname string
ExternalIP string
InternalIP string
}
// ValidateNodeObjectType return wether or not the given object
// is of type *api.Node or *v1.Node -> valid true, valid false otherwise
func ValidateNodeObjectType(obj interface{}) error {
switch obj.(type) {
case *v1.Node:
return nil
case *api.Node:
return errors.New("unsupported type api.* (must be v1.*)")
}
return errors.New("unexpected type")
}
// GetNodeName return validated service type's name, error otherwise.
func GetNodeName(obj interface{}) (string, error) {
node, ok := obj.(*v1.Node)
if !ok {
return "", errors.New("type assertion failure")
}
return string(node.Name), nil
}
// GetNodeNamespace return validated service type's namespace, error otherwise.
func GetNodeNamespace(obj interface{}) (string, error) {
node, ok := obj.(*v1.Node)
if !ok {
return "", errors.New("type assertion failure")
}
return string(node.Namespace), nil
}
// GetNodePodCIDR returns the node's pod cidr ip address value as a string, or an error
func GetNodePodCIDR(obj interface{}) (string, error) {
node, ok := obj.(*v1.Node)
if !ok {
return "", errors.New("type assertion failure")
}
return string(node.Spec.PodCIDR), nil
}
// GetNodeAddress returns the node's addresses object, or an error
func GetNodeAddress(obj interface{}) (NodeAddress, error) {
nodeAddr := NodeAddress{}
node, ok := obj.(*v1.Node)
if !ok {
return nodeAddr, errors.New("type assertion failure")
}
for _, addr := range node.Status.Addresses {
switch addr.Type {
case v1.NodeHostName:
nodeAddr.Hostname = addr.Address
case v1.NodeExternalIP:
nodeAddr.ExternalIP = addr.Address
case v1.NodeInternalIP:
nodeAddr.InternalIP = addr.Address
}
}
return nodeAddr, nil
}
// GetNodeExternalIP returns the node's external ip address value as a string, or an error
func GetNodeExternalIP(obj interface{}) (string, error) {
addrs, err := GetNodeAddress(obj)
if err != nil {
return "", err
}
return addrs.ExternalIP, err
}
// GetNodeInternalIP returns the node's internal ip address value as a string, or an error
func GetNodeInternalIP(obj interface{}) (string, error) {
addrs, err := GetNodeAddress(obj)
if err != nil {
return "", err
}
return addrs.InternalIP, err
}
// GetNodeHostname returns the node's internal ip address value as a string, or an error
func GetNodeHostname(obj interface{}) (string, error) {
addrs, err := GetNodeAddress(obj)
if err != nil {
return "", err
}
return addrs.Hostname, nil
}
// IsNodeScheduleable returns the node's schedulability status, true if the
// node can schedule pods, false otherwise
func IsNodeScheduleable(obj interface{}) bool {
node, ok := obj.(*v1.Node)
if !ok {
return false
}
return node.Spec.Unschedulable == false
}