18
18
#
19
19
20
20
err () {
21
- echo -e " ERROR: $* \n" >&2
21
+ echo -e " $( date -u +%H:%M:%S ) 😱 ERROR: $* \n" >&2
22
22
}
23
23
24
24
info () {
25
- echo -e " INFO : $* \n" >&2
25
+ echo -e " $( date -u +%H:%M:%S) 🔔 INFO : $* \n" >&2
26
+ }
27
+
28
+ header () {
29
+ local title=" 🔆🔆🔆 $* 🔆🔆🔆 "
30
+
31
+ local len=40
32
+ if [[ ${# title} -gt $len ]]; then
33
+ len=${# title}
34
+ fi
35
+
36
+ echo -e " \n\n \033[1m${title} \033[0m"
37
+ echo -n " ━━━━━"
38
+ printf ' ━%.0s' $( seq " $len " )
39
+ echo " ━━━━━━━"
40
+
26
41
}
27
42
28
43
die () {
29
- echo -e " FATAL: $* \n" >&2
44
+ echo -e " $( date -u +%H:%M:%S ) 💀 FATAL: $* \n" >&2
30
45
exit 1
31
46
}
32
47
@@ -52,61 +67,101 @@ is_set() {
52
67
[[ " $1 " =~ true| TRUE| True ]]
53
68
}
54
69
55
- wait_for_pods_in_namespace () {
56
- local namespace=$1
57
- shift
58
- local timeout=" ${1:- 15m} "
59
-
60
- info " Waiting for all pods in $namespace to be ready (max $timeout ) ..."
61
- kubectl wait --for=condition=Ready pod --all -n " $namespace " --timeout=" $timeout " || {
62
- kubectl get pods --field-selector status.phase! =Running -n " $namespace "
63
- fail " pods above in $namespace failed to run"
70
+ # wait_for_resource waits for max_tries x timeout for resource to be in condition
71
+ # if it does not reach in the condition in the given time, the function returns 1
72
+ # NOTE: a selector must be passed to the function as additional argument. E.g.
73
+ # wait_for_resource 5 1m nodes --all (--all is the selector)
74
+ wait_for_resource () {
75
+ local max_tries=" $1 "
76
+ local timeout=" $2 "
77
+ local resource=" $3 "
78
+ local condition=" $4 "
79
+ shift 4
80
+
81
+ info " Waiting for $resource to be in $condition state"
82
+
83
+ local -i tries=0
84
+ while ! kubectl wait --for=condition=" $condition " --timeout=" 2s" \
85
+ " $resource " " $@ " && [[ $tries -lt $max_tries ]]; do
86
+
87
+ tries=$(( tries + 1 ))
88
+ echo " ... [$tries / $max_tries ]: waiting ($timeout ) for $resource to be $condition "
89
+ sleep " $timeout "
90
+ done
91
+
92
+ kubectl wait --for=condition=" $condition " " $resource " " $@ " --timeout=0 || {
93
+ fail " $resource $* failed to be in $condition state"
64
94
return 1
65
95
}
66
96
67
- ok " All pods in $namespace are running "
97
+ ok " $resource matching $* are in $condition state "
68
98
return 0
69
99
}
70
100
71
- # shellcheck disable=SC2120
101
+ wait_for_crds () {
102
+ header " Waiting for crds to be established"
103
+
104
+ # ensure kubectl get crds works before trying to wait for crds to be established
105
+ [[ $( kubectl get crds -o name | wc -l) -eq 0 ]] && {
106
+ info " no crds found; not waiting for crds to be established"
107
+ return 0
108
+ }
109
+ wait_for_resource 20 15 crds Established --all
110
+ }
111
+
72
112
wait_for_nodes () {
73
- local timeout=" ${1:- 15m} "
113
+ header " Waiting for nodes to be ready"
114
+
115
+ # ensure kubectl get nodes works before trying to wait for them
116
+ info " Waiting for nodes to come up"
117
+ local -i max_tries=10
118
+ local -i tries=0
119
+ while [[ $( kubectl get nodes -o name | wc -l) -eq 0 ]] && [[ $tries -lt $max_tries ]]; do
120
+ tries=$(( tries + 1 ))
121
+ echo " ... [$tries / $max_tries ] waiting for at least one node to come up"
122
+ sleep 20
123
+ done
124
+
125
+ wait_for_resource 20 30 nodes Ready --all
126
+ }
74
127
75
- info " Waiting for cluster nodes to be ready (max $timeout ) ..."
128
+ wait_for_all_pods () {
129
+ header " Waiting for all pods to be ready"
76
130
77
- kubectl wait --for=condition=Ready " $( kubectl get nodes -o name) " --timeout=" $timeout " || {
78
- fail " node is not in ready state"
131
+ wait_for_resource 20 30 pods Ready --all --all-namespaces || {
132
+ fail " Pods below failed to run"
133
+ kubectl get pods --field-selector status.phase! =Running --all-namespaces || true
79
134
return 1
80
135
}
81
- ok " all nodes in cluster are running "
136
+
82
137
return 0
83
138
}
84
139
85
- # shellcheck disable=SC2120
86
- wait_for_all_pods () {
87
- local timeout= " ${1 :- 15m} "
140
+ wait_for_pods_in_namespace () {
141
+ local namespace= " $1 "
142
+ shift 1
88
143
89
- info " Waiting for all pods to be ready (max $timeout ) ... "
144
+ header " Waiting for pods in $namespace to be ready"
90
145
91
- kubectl wait --for=condition=Ready pods --all --all-namespaces --timeout= " $timeout " || {
92
- kubectl get pods --field-selector status.phase ! =Running --all-namespaces
93
- fail " pods above failed to run "
146
+ wait_for_resource 10 30 pods Ready --all -n " $namespace " || {
147
+ fail " Pods below failed to run "
148
+ kubectl get pods --field-selector status.phase ! =Running -n " $namespace " || true
94
149
return 1
95
-
96
150
}
97
151
98
- ok " All pods in the cluster are running"
152
+ ok " All pods in $namespace are running"
99
153
return 0
100
154
}
101
155
102
156
wait_for_cluster_ready () {
103
- wait_for_nodes
104
-
105
- info " Waiting for cluster to be ready"
157
+ header " Waiting for cluster to be ready"
106
158
kubectl cluster-info
107
159
160
+ wait_for_nodes
108
161
wait_for_pods_in_namespace kube-system
162
+ wait_for_crds
109
163
wait_for_all_pods
164
+
110
165
ok " Cluster is ready\n\n"
111
166
}
112
167
0 commit comments