This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 560
/
node.go
179 lines (162 loc) · 4.43 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package node
import (
"context"
"encoding/json"
"fmt"
"log"
"os/exec"
"regexp"
"strings"
"time"
"github.com/Azure/acs-engine/test/e2e/kubernetes/util"
)
const (
//ServerVersion is used to parse out the version of the API running
ServerVersion = `(Server Version:\s)+(v\d+.\d+.\d+)+`
)
// Node represents the kubernetes Node Resource
type Node struct {
Status Status `json:"status"`
Metadata Metadata `json:"metadata"`
}
// Metadata contains things like name and created at
type Metadata struct {
Name string `json:"name"`
CreatedAt time.Time `json:"creationTimestamp"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
}
// Status parses information from the status key
type Status struct {
Info Info `json:"Info"`
NodeAddresses []Address `json:"addresses"`
Conditions []Condition `json:"conditions"`
}
// Address contains an address and a type
type Address struct {
Address string `json:"address"`
Type string `json:"type"`
}
// Info contains information like what version the kubelet is running
type Info struct {
ContainerRuntimeVersion string `json:"containerRuntimeVersion"`
KubeProxyVersion string `json:"kubeProxyVersion"`
KubeletProxyVersion string `json:"kubeletVersion"`
OperatingSystem string `json:"operatingSystem"`
}
// Condition contains various status information
type Condition struct {
LastHeartbeatTime time.Time `json:"lastHeartbeatTime"`
LastTransitionTime time.Time `json:"lastTransitionTime"`
Message string `json:"message"`
Reason string `json:"reason"`
Status string `json:"status"`
Type string `json:"type"`
}
// List is used to parse out Nodes from a list
type List struct {
Nodes []Node `json:"items"`
}
// AreAllReady returns a bool depending on cluster state
func AreAllReady(nodeCount int) bool {
list, _ := Get()
if list != nil && len(list.Nodes) == nodeCount {
for _, node := range list.Nodes {
for _, condition := range node.Status.Conditions {
if condition.Type == "KubeletReady" && condition.Status == "false" {
return false
}
}
}
return true
}
return false
}
// WaitOnReady will block until all nodes are in ready state
func WaitOnReady(nodeCount int, sleep, duration time.Duration) bool {
readyCh := make(chan bool, 1)
errCh := make(chan error)
ctx, cancel := context.WithTimeout(context.Background(), duration)
defer cancel()
go func() {
for {
select {
case <-ctx.Done():
errCh <- fmt.Errorf("Timeout exceeded (%s) while waiting for Nodes to become ready", duration.String())
default:
if AreAllReady(nodeCount) {
readyCh <- true
}
time.Sleep(sleep)
}
}
}()
for {
select {
case <-errCh:
return false
case ready := <-readyCh:
return ready
}
}
}
// Get returns the current nodes for a given kubeconfig
func Get() (*List, error) {
cmd := exec.Command("kubectl", "get", "nodes", "-o", "json")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to run 'kubectl get nodes':%s", string(out))
return nil, err
}
nl := List{}
err = json.Unmarshal(out, &nl)
if err != nil {
log.Printf("Error unmarshalling nodes json:%s", err)
}
return &nl, nil
}
// Version get the version of the server
func Version() (string, error) {
cmd := exec.Command("kubectl", "version", "--short")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to run 'kubectl version':%s", string(out))
return "", err
}
split := strings.Split(string(out), "\n")
exp, err := regexp.Compile(ServerVersion)
if err != nil {
log.Printf("Error while compiling regexp:%s", ServerVersion)
}
s := exp.FindStringSubmatch(split[1])
return s[2], nil
}
// GetAddressByType will return the Address object for a given Kubernetes node
func (ns *Status) GetAddressByType(t string) *Address {
for _, a := range ns.NodeAddresses {
if a.Type == t {
return &a
}
}
return nil
}
// GetByPrefix will return a []Node of all nodes that have a name that match the prefix
func GetByPrefix(prefix string) ([]Node, error) {
list, err := Get()
if err != nil {
return nil, err
}
nodes := make([]Node, 0)
for _, n := range list.Nodes {
exp, err := regexp.Compile(prefix)
if err != nil {
return nil, err
}
if exp.MatchString(n.Metadata.Name) {
nodes = append(nodes, n)
}
}
return nodes, nil
}