ci: prevent ERR trap inheritance for kubectl_retry

`bash -E` causes inheritance of the ERR trap into shell functions,
command substitutions, and commands executed in a subshell environment.
Because the `kubectl_retry` function depends on detection an error of a
subshell, the ERR trap is not needed to be executed. The trap contains
extra logging, and exits the script in the `rook.sh` case. The aborting
of the script is not wanted when a retry is expected to be done.

While checking for known failures, the `grep` command may exit with 1,
if there are no matches. That means, the `ret` variable will be set to
0, but there will also be an error exit status. This causes `bash -E` to
abort the function, and call the ERR trap.

Signed-off-by: Niels de Vos <ndevos@redhat.com>
(cherry picked from commit 4891e534d3)
This commit is contained in:
Niels de Vos 2022-04-27 11:28:50 +02:00 committed by mergify[bot]
parent 691714228e
commit 2673a0b97a

View File

@ -1,4 +1,4 @@
#!/bin/bash -E #!/bin/bash
KUBECTL_RETRY=5 KUBECTL_RETRY=5
KUBECTL_RETRY_DELAY=10 KUBECTL_RETRY_DELAY=10
@ -27,14 +27,20 @@ kubectl_retry() {
while ! ( kubectl "${action}" "${@}" 2>"${stderr}" 1>>"${stdout}" ) while ! ( kubectl "${action}" "${@}" 2>"${stderr}" 1>>"${stdout}" )
do do
# write logs to stderr and empty stderr (only)
cat "${stdout}" > /dev/stderr
cat "${stderr}" > /dev/stderr
echo "$(date): 'kubectl_retry ${action} ${*}' try #${retries} failed, checking errors" > /dev/stderr
# in case of a failure when running "create", ignore errors with "AlreadyExists" # in case of a failure when running "create", ignore errors with "AlreadyExists"
if [ "${action}" == 'create' ] if [ "${action}" == 'create' ]
then then
# count lines in stderr that do not have "AlreadyExists" or "Warning" # count lines in stderr that do not have "AlreadyExists" or "Warning"
ret=$(grep -cvw -e 'AlreadyExists' -e '^Warning:' "${stderr}") ret=$(grep -cvw -e 'AlreadyExists' -e '^Warning:' "${stderr}" || true)
if [ "${ret}" -eq 0 ] if [ "${ret}" -eq 0 ]
then then
# Success! stderr is empty after removing all "AlreadyExists" lines. # Success! stderr is empty after removing all "AlreadyExists" lines.
echo "$(date): 'kubectl_retry ${action} ${*}' succeeded without unknown errors" > /dev/stderr
break break
fi fi
fi fi
@ -43,10 +49,11 @@ kubectl_retry() {
if [ "${action}" == 'delete' ] if [ "${action}" == 'delete' ]
then then
# count lines in stderr that do not have "NotFound" or "Warning" # count lines in stderr that do not have "NotFound" or "Warning"
ret=$(grep -cvw -e 'NotFound' -e '^Warning:' "${stderr}") ret=$(grep -cvw -e 'NotFound' -e '^Warning:' "${stderr}" || true)
if [ "${ret}" -eq 0 ] if [ "${ret}" -eq 0 ]
then then
# Success! stderr is empty after removing all "NotFound" lines. # Success! stderr is empty after removing all "NotFound" lines.
echo "$(date): 'kubectl_retry ${action} ${*}' succeeded without unknown errors" > /dev/stderr
break break
fi fi
fi fi
@ -54,15 +61,14 @@ kubectl_retry() {
retries=$((retries+1)) retries=$((retries+1))
if [ ${retries} -eq ${KUBECTL_RETRY} ] if [ ${retries} -eq ${KUBECTL_RETRY} ]
then then
echo "$(date): 'kubectl_retry ${action} ${*}' failed, no more retries left (${retries}/${KUBECTL_RETRY})" > /dev/stderr
ret=1 ret=1
break break
fi fi
# write logs to stderr and empty stderr (only) # empty stderr for the next loop
cat "${stdout}" > /dev/stderr
cat "${stderr}" > /dev/stderr
true > "${stderr}" true > "${stderr}"
echo "$(date): 'kubectl_retry ${*}' failed (${retries}/${KUBECTL_RETRY}), will retry in ${KUBECTL_RETRY_DELAY} seconds" > /dev/stderr echo "$(date): 'kubectl_retry ${action} ${*}' failed (${retries}/${KUBECTL_RETRY}), will retry in ${KUBECTL_RETRY_DELAY} seconds" > /dev/stderr
sleep ${KUBECTL_RETRY_DELAY} sleep ${KUBECTL_RETRY_DELAY}
@ -71,7 +77,7 @@ kubectl_retry() {
ret=0 ret=0
done done
echo "$(date): 'kubectl_retry ${*}' done (ret=${ret})" > /dev/stderr echo "$(date): 'kubectl_retry ${action} ${*}' done (ret=${ret})" > /dev/stderr
# write output so that calling functions can consume it # write output so that calling functions can consume it
cat "${stdout}" > /dev/stdout cat "${stdout}" > /dev/stdout