-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubectl-wait-wrapper.sh
executable file
·242 lines (221 loc) · 8.5 KB
/
kubectl-wait-wrapper.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env bash
set -e
set -o pipefail
# This is a rather minimal example Argbash potential
# Example taken from http://argbash.readthedocs.io/en/stable/example.html
#
# ARG_OPTIONAL_SINGLE([namespace],[n],[namespace name])
# ARG_OPTIONAL_REPEATED([resource-type],[t],[resource type])
# ARG_OPTIONAL_REPEATED([params],[p],[kubectl parameters in quotes])
# ARG_OPTIONAL_SINGLE([retries],[r],[number of retries if the resource is not created yet],[30])
# ARG_OPTIONAL_SINGLE([wait],[w],[wait between retries],[5])
# ARG_OPTIONAL_BOOLEAN([debug],[d],[enable debug])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.9.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
# Generated online by https://argbash.io/generate
# # When called, the process ends.
# Args:
# $1: The exit message (print to stderr)
# $2: The exit code (default is 1)
# if env var _PRINT_HELP is set to 'yes', the usage is print to stderr (prior to $1)
# Example:
# test -f "$_arg_infile" || _PRINT_HELP=yes die "Can't continue, have to supply file as an argument, got '$_arg_infile'" 4
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}
# Function that evaluates whether a value passed to it begins by a character
# that is a short option of an argument the script knows about.
# This is required in order to support getopts-like short options grouping.
begins_with_short_option()
{
local first_option all_short_options='ntprwd'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_namespace=
_arg_resource_type=()
_arg_params=()
_arg_retries="30"
_arg_wait="5"
_arg_debug="off"
# Function that prints general usage of the script.
# This is useful if users asks for it, or if there is an argument parsing error (unexpected / spurious arguments)
# and it makes sense to remind the user how the script is supposed to be called.
print_help()
{
printf 'Usage: %s [-n|--namespace <arg>] [-t|--resource-type <arg>] [-p|--params <arg>] [-r|--retries <arg>] [-w|--wait <arg>] [-d|--(no-)debug]\n' "$0"
printf '\t%s\n' "-n, --namespace: namespace name (no default)"
printf '\t%s\n' "-t, --resource-type: resource type (empty by default)"
printf '\t%s\n' "-p, --params: kubectl parameters in quotes (empty by default)"
printf '\t%s\n' "-r, --retries: number of retries if the resource is not created yet (default: '30')"
printf '\t%s\n' "-w, --wait: wait between retries (default: '5')"
printf '\t%s\n' "-d, --debug, --no-debug: enable debug (off by default)"
}
# The parsing of the command-line
parse_commandline()
{
while test $# -gt 0
do
_key="$1"
case "$_key" in
# We support whitespace as a delimiter between option argument and its value.
# Therefore, we expect the --namespace or -n value.
# so we watch for --namespace and -n.
# Since we know that we got the long or short option,
# we just reach out for the next argument to get the value.
-n|--namespace)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_namespace="$2"
shift
;;
# We support the = as a delimiter between option argument and its value.
# Therefore, we expect --namespace=value, so we watch for --namespace=*
# For whatever we get, we strip '--namespace=' using the ${var##--namespace=} notation
# to get the argument value
--namespace=*)
_arg_namespace="${_key##--namespace=}"
;;
# We support getopts-style short arguments grouping,
# so as -n accepts value, we allow it to be appended to it, so we watch for -n*
# and we strip the leading -n from the argument string using the ${var##-n} notation.
-n*)
_arg_namespace="${_key##-n}"
;;
# See the comment of option '--namespace' to see what's going on here - principle is the same.
-t|--resource-type)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_resource_type+=("$2")
shift
;;
# See the comment of option '--namespace=' to see what's going on here - principle is the same.
--resource-type=*)
_arg_resource_type+=("${_key##--resource-type=}")
;;
# See the comment of option '-n' to see what's going on here - principle is the same.
-t*)
_arg_resource_type+=("${_key##-t}")
;;
# See the comment of option '--namespace' to see what's going on here - principle is the same.
-p|--params)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_params+=("$2")
shift
;;
# See the comment of option '--namespace=' to see what's going on here - principle is the same.
--params=*)
_arg_params+=("${_key##--params=}")
;;
# See the comment of option '-n' to see what's going on here - principle is the same.
-p*)
_arg_params+=("${_key##-p}")
;;
# See the comment of option '--namespace' to see what's going on here - principle is the same.
-r|--retries)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_retries="$2"
shift
;;
# See the comment of option '--namespace=' to see what's going on here - principle is the same.
--retries=*)
_arg_retries="${_key##--retries=}"
;;
# See the comment of option '-n' to see what's going on here - principle is the same.
-r*)
_arg_retries="${_key##-r}"
;;
# See the comment of option '--namespace' to see what's going on here - principle is the same.
-w|--wait)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_wait="$2"
shift
;;
# See the comment of option '--namespace=' to see what's going on here - principle is the same.
--wait=*)
_arg_wait="${_key##--wait=}"
;;
# See the comment of option '-n' to see what's going on here - principle is the same.
-w*)
_arg_wait="${_key##-w}"
;;
# The debug argument doesn't accept a value,
# we expect the --debug or -d, so we watch for them.
-d|--no-debug|--debug)
_arg_debug="on"
test "${1:0:5}" = "--no-" && _arg_debug="off"
;;
# We support getopts-style short arguments clustering,
# so as -d doesn't accept value, other short options may be appended to it, so we watch for -d*.
# After stripping the leading -d from the argument, we have to make sure
# that the first character that follows corresponds to a short option.
-d*)
_arg_debug="on"
_next="${_key##-d}"
if test -n "$_next" -a "$_next" != "$_key"
then
{ begins_with_short_option "$_next" && shift && set -- "-d" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
fi
;;
*)
_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
;;
esac
shift
done
}
# Now call all the functions defined above that are needed to get the job done
parse_commandline "$@"
# OTHER STUFF GENERATED BY Argbash
### END OF CODE GENERATED BY Argbash (sortof) ### ])
[ "${_arg_debug}" == "on" ] && set -x
if [ "${_arg_namespace}" == "" ]; then
printf "error: missing namespace parameter\n\n"
print_help
exit 1
fi
if [ ${#_arg_resource_type[@]} -eq 0 ]; then
printf "error: missing params resource-type\n\n"
print_help
exit 1
fi
if [ ${#_arg_params[@]} -eq 0 ]; then
printf "error: missing params parameter\n\n"
print_help
exit 1
fi
retries="${_arg_retries}"
echo "going to check for the resources creation ${retries} times before fail"
for i in "${_arg_resource_type[@]}"; do
while [ "${retries}" != 0 ] ; do
result="$(kubectl -n "${_arg_namespace}" get "${i}" 2>/dev/null || true)"
if [ "${result}" == "No resources found in ${_arg_namespace} namespace." ] ; then \
echo "#${retries} - No resources found yet. Waiting ${_arg_wait} seconds before next retry"
retries=$((retries - 1))
sleep "${_arg_wait}"
else
break
fi
done
done
retries="${_arg_retries}"
for j in "${_arg_params[@]}"; do
while [ "${retries}" != 0 ] ; do
read -r -a param_array <<< "${j}"
result=$(kubectl wait -n "${_arg_namespace}" "${param_array[@]}" 2>&1 || true)
if [ "${result}" == "error: no matching resources found" ]; then
echo "#${retries} - No matching resources yet. Waiting ${_arg_wait} seconds before next retry"
retries=$((retries - 1))
sleep "${_arg_wait}"
else
echo "${result}"
break
fi
done
done