Skip to content

Commit addb732

Browse files
authored
Merge pull request #47 from sthaha/fix-sleep-60
fix: Wait for resources to reach a condition properly
2 parents a966b1b + 7e6c7f6 commit addb732

File tree

2 files changed

+86
-33
lines changed

2 files changed

+86
-33
lines changed

lib/utils.sh

Lines changed: 86 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,30 @@
1818
#
1919

2020
err() {
21-
echo -e "ERROR: $*\n" >&2
21+
echo -e "$(date -u +%H:%M:%S) 😱 ERROR: $*\n" >&2
2222
}
2323

2424
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+
2641
}
2742

2843
die() {
29-
echo -e "FATAL: $*\n" >&2
44+
echo -e "$(date -u +%H:%M:%S) 💀 FATAL: $*\n" >&2
3045
exit 1
3146
}
3247

@@ -52,61 +67,101 @@ is_set() {
5267
[[ "$1" =~ true|TRUE|True ]]
5368
}
5469

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"
6494
return 1
6595
}
6696

67-
ok "All pods in $namespace are running"
97+
ok "$resource matching $* are in $condition state"
6898
return 0
6999
}
70100

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+
72112
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+
}
74127

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"
76130

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
79134
return 1
80135
}
81-
ok "all nodes in cluster are running"
136+
82137
return 0
83138
}
84139

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
88143

89-
info "Waiting for all pods to be ready (max $timeout) ..."
144+
header "Waiting for pods in $namespace to be ready"
90145

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
94149
return 1
95-
96150
}
97151

98-
ok "All pods in the cluster are running"
152+
ok "All pods in $namespace are running"
99153
return 0
100154
}
101155

102156
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"
106158
kubectl cluster-info
107159

160+
wait_for_nodes
108161
wait_for_pods_in_namespace kube-system
162+
wait_for_crds
109163
wait_for_all_pods
164+
110165
ok "Cluster is ready\n\n"
111166
}
112167

providers/microshift/microshift.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ function _setup_microshift() {
8787

8888
export KUBECONFIG="$MICROSHIFT_KUBECONFIG"
8989

90-
info "Sleeping for 60s"
91-
sleep 60
9290
wait_for_cluster_ready
9391
}
9492

0 commit comments

Comments
 (0)