forked from groundnuty/k8s-wait-for
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_for.sh
executable file
·181 lines (160 loc) · 5.86 KB
/
wait_for.sh
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
180
181
#!/usr/bin/env sh
# This script is aimed to be POSIX-compliant and style consistent with help of these tools:
# - https://github.com/koalaman/shellcheck
# - https://github.com/openstack-dev/bashate
trap "exit 1" TERM
TOP_PID=$$
KUBECTL_ARGS=""
WAIT_TIME="${WAIT_TIME:-2}" # seconds
DEBUG="${DEBUG:-0}"
usage() {
cat <<EOF
This script waits until a job, pod or service enter ready state.
${0##*/} job [<job name> | -l<kubectl selector>]
${0##*/} pod [<pod name> | -l<kubectl selector>]
${0##*/} service [<service name> | -l<kubectl selector>]
Examples:
Wait for all pods with with a following label to enter 'Ready' state:
${0##*/} pod -lapp=develop-volume-gluster-krakow
Wait for all the pods in that job to have a 'Succeeded' state:
${0##*/} job develop-volume-s3-krakow-init
Wait for all selected pods to enter the 'Ready' state:
${0##*/} pod -l"release in (develop), chart notin (cross-support-job-3p)"
EOF
exit 1
}
# Job or set of pods is considered ready if all of the are ready
# example output with 3 pods, where 2 are not ready would be: "false false"
get_pod_state() {
get_pod_state_name="$1"
get_pod_state_flags="$2"
get_pod_state_output1=$(kubectl get pods "$get_pod_state_name" $get_pod_state_flags $KUBECTL_ARGS -o go-template='{{- if .items -}}
{{- if gt (len .items) 0}}
{{- range .items -}}
{{- range .status.conditions -}}
{{- if and (eq .type "Ready") (eq .status "False") -}}
{{- if .reason -}}
{{- if ne .reason "PodCompleted" -}}
{{ .status }}
{{- end -}}
{{- else -}}
{{ .status }}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{- range .status.conditions -}}
{{- if and (eq .type "Ready") (eq .status "False") -}}
{{- if .reason -}}
{{- if ne .reason "PodCompleted" -}}
{{ .status }}
{{- end -}}
{{- else -}}
{{ .status }}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{- printf "No resources found.\n" -}}
{{- end -}}' 2>&1)
if [ $? -ne 0 ]; then
if expr match "$get_pod_state_output1" '\(.*not found$\)' 1>/dev/null ; then
echo "No pods found, waiting for them to be created..." >&2
echo "$get_pod_state_output1" >&2
else
echo "$get_pod_state_output1" >&2
kill -s TERM $TOP_PID
fi
elif [ $DEBUG -ge 2 ]; then
echo "$get_pod_state_output1" >&2
fi
get_pod_state_output2=$(printf "%s" "$get_pod_state_output1" | xargs )
if [ $DEBUG -ge 1 ]; then
echo "$get_pod_state_output2" >&2
fi
echo "$get_pod_state_output2"
}
# Service or set of service is considered ready if all of the pods matched my service selector are considered ready
# example output with 2 services each matching a single pod would be: "falsefalse"
get_service_state() {
get_service_state_name="$1"
get_service_state_selectors=$(kubectl get service "$get_service_state_name" $KUBECTL_ARGS -ojson 2>&1 | jq -cr 'if . | has("items") then .items[] else . end | [ .spec.selector | to_entries[] | "-l\(.key)=\(.value)" ] | join(",") ')
get_service_state_states=""
for get_service_state_selector in $get_service_state_selectors ; do
get_service_state_selector=$(echo "$get_service_state_selector" | tr ',' ' ')
get_service_state_state=$(get_pod_state "" "$get_service_state_selector")
get_service_state_states="${get_service_state_states}${get_service_state_state}" ;
done
echo "$get_service_state_states"
}
# Job or set of jobs is considered ready if all of them succeeded at least once
# example output with 2 still running jobs would be "0 0"
# this function considers the line:
# Pods Statuses: 0 Running / 1 Succeeded / 0 Failed
# in a 'kubectl describe' job output.
get_job_state() {
get_job_state_name="$1"
get_job_state_output=$(kubectl describe jobs $get_job_state_name $KUBECTL_ARGS 2>&1)
if [ $? -ne 0 ]; then
echo "$get_job_state_output" >&2
kill -s TERM $TOP_PID
elif [ $DEBUG -ge 2 ]; then
echo "$get_job_state_output" >&2
fi
if [ "$get_job_state_output" = "" ]; then
echo "wait_for.sh: No jobs found!" >&2
kill -s TERM $TOP_PID
fi
get_job_state_output1=$(printf "%s" "$get_job_state_output" | sed -nr 's#.*/ (0+) .*/.*#\1#p' 2>&1)
if [ $? -ne 0 ]; then
echo "$get_job_state_output" >&2
echo "$get_job_state_output1" >&2
kill -s TERM $TOP_PID
elif [ $DEBUG -ge 2 ]; then
echo "$get_job_state_output1" >&2
fi
get_job_state_output2=$(printf "%s" "$get_job_state_output1" | xargs )
if [ $DEBUG -ge 1 ]; then
echo "$get_job_state_output2" >&2
fi
echo "$get_job_state_output2"
}
wait_for_resource() {
wait_for_resource_type=$1
wait_for_resource_descriptor="$2"
while [ -n "$(get_${wait_for_resource_type}_state "$wait_for_resource_descriptor")" ] ; do
print_KUBECTL_ARGS="$KUBECTL_ARGS"
[ "$print_KUBECTL_ARGS" != "" ] && print_KUBECTL_ARGS=" $print_KUBECTL_ARGS"
echo "Waiting for $wait_for_resource_type $wait_for_resource_descriptor${print_KUBECTL_ARGS}..."
sleep "$WAIT_TIME"
done
ready "$wait_for_resource_type" "$wait_for_resource_descriptor"
}
ready() {
print_KUBECTL_ARGS="$KUBECTL_ARGS"
[ "$print_KUBECTL_ARGS" != "" ] && print_KUBECTL_ARGS=" $print_KUBECTL_ARGS"
printf "[%s] %s %s%s is ready.\\n" "$(date +'%Y-%m-%d %H:%M:%S')" "$1" "$2" "$print_KUBECTL_ARGS"
}
main() {
if [ $# -lt 2 ]; then
usage
fi
case "$1" in
pod|service|job)
main_resource=$1
shift
;;
*)
printf 'ERROR: Unknown resource type: %s\n' "$1" >&2
exit 1
;;
esac
main_name="$1"
shift
KUBECTL_ARGS="${*}"
wait_for_resource "$main_resource" "$main_name"
exit 0
}
main "$@"