forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubernetes.go
143 lines (124 loc) · 3.92 KB
/
kubernetes.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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"log"
"os/exec"
"time"
)
// kubectlGetNodes lists nodes by executing kubectl get nodes, parsing the output into a nodeList object
func kubectlGetNodes(cmd string) (*nodeList, error) {
if cmd == "" {
cmd = "kubectl"
}
o, err := control.Output(exec.Command(cmd, "get", "nodes", "-ojson"))
if err != nil {
log.Printf("kubectl get nodes failed: %s\n%s", wrapError(err).Error(), string(o))
return nil, err
}
nodes := &nodeList{}
if err := json.Unmarshal(o, nodes); err != nil {
return nil, fmt.Errorf("error parsing kubectl get nodes output: %v", err)
}
return nodes, nil
}
// isReady checks if the node has a Ready Condition that is True
func isReady(node *node) bool {
for _, c := range node.Status.Conditions {
if c.Type == "Ready" {
return c.Status == "True"
}
}
return false
}
// waitForReadyNodes polls the nodes until we see at least desiredCount that are Ready
// We can also pass requiredConsecutiveSuccesses to require that we see this N times in a row
func waitForReadyNodes(desiredCount int, timeout time.Duration, requiredConsecutiveSuccesses int) error {
stop := time.Now().Add(timeout)
consecutiveSuccesses := 0
for {
if time.Now().After(stop) {
break
}
nodes, err := kubectlGetNodes("")
if err != nil {
log.Printf("kubectl get nodes failed, sleeping: %v", err)
consecutiveSuccesses = 0
time.Sleep(30 * time.Second)
continue
}
readyNodes := countReadyNodes(nodes)
if readyNodes >= desiredCount {
consecutiveSuccesses++
if consecutiveSuccesses >= requiredConsecutiveSuccesses {
log.Printf("%d ready nodes found, %d sequential successes - cluster is ready",
readyNodes,
consecutiveSuccesses)
return nil
}
log.Printf("%d ready nodes found, waiting for %d sequential successes (success count = %d)",
readyNodes,
requiredConsecutiveSuccesses,
consecutiveSuccesses)
time.Sleep(2 * time.Second)
} else {
consecutiveSuccesses = 0
log.Printf("%d (ready nodes) < %d (requested instances), sleeping", readyNodes, desiredCount)
time.Sleep(30 * time.Second)
}
}
return fmt.Errorf("waiting for ready nodes timed out")
}
// countReadyNodes returns the number of nodes that have isReady == true
func countReadyNodes(nodes *nodeList) int {
var ready []*node
for i := range nodes.Items {
node := &nodes.Items[i]
if isReady(node) {
ready = append(ready, node)
}
}
return len(ready)
}
// nodeList is a simplified version of the v1.NodeList API type
type nodeList struct {
Items []node `json:"items"`
}
// node is a simplified version of the v1.Node API type
type node struct {
Metadata metadata `json:"metadata"`
Status nodeStatus `json:"status"`
}
// nodeStatus is a simplified version of the v1.NodeStatus API type
type nodeStatus struct {
Addresses []nodeAddress `json:"addresses"`
Conditions []nodeCondition `json:"conditions"`
}
// nodeAddress is a simplified version of the v1.NodeAddress API type
type nodeAddress struct {
Address string `json:"address"`
Type string `json:"type"`
}
// nodeCondition is a simplified version of the v1.NodeCondition API type
type nodeCondition struct {
Message string `json:"message"`
Reason string `json:"reason"`
Status string `json:"status"`
Type string `json:"type"`
}
// metadata is a simplified version of the kubernetes metadata types
type metadata struct {
Name string `json:"name"`
}