ceph-csi/scripts/utils.sh
Niels de Vos 89e2ff39f1 ci: do not count Warning: matches in kubectl_retry()
Depending on the Kubernetes version, the following warning is reported
regulary:

> Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+,
> unavailable in v1.25+

The warning is written to stderr, so skipping AlreadyExists or NotFound
is not sufficient to trigger a retry. Ignoring '^Warning:' in the stderr
output should prevent unneeded failures while deploying Rook or other
components.

Signed-off-by: Niels de Vos <ndevos@redhat.com>
2022-04-26 20:55:18 +00:00

69 lines
2.0 KiB
Bash
Executable File

#!/bin/bash -E
KUBECTL_RETRY=5
KUBECTL_RETRY_DELAY=10
kubectl_retry() {
local retries=0 action="${1}" ret=0 stdout stderr
shift
# temporary files for kubectl output
stdout=$(mktemp rook-kubectl-stdout.XXXXXXXX)
stderr=$(mktemp rook-kubectl-stderr.XXXXXXXX)
while ! ( kubectl "${action}" "${@}" 2>"${stderr}" 1>"${stdout}" )
do
# in case of a failure when running "create", ignore errors with "AlreadyExists"
if [ "${action}" == 'create' ]
then
# count lines in stderr that do not have "AlreadyExists" or "Warning"
ret=$(grep -cvw -e 'AlreadyExists' -e '^Warning:' "${stderr}")
if [ "${ret}" -eq 0 ]
then
# Success! stderr is empty after removing all "AlreadyExists" lines.
break
fi
fi
# in case of a failure when running "delete", ignore errors with "NotFound"
if [ "${action}" == 'delete' ]
then
# count lines in stderr that do not have "NotFound" or "Warning"
ret=$(grep -cvw -e 'NotFound' -e '^Warning:' "${stderr}")
if [ "${ret}" -eq 0 ]
then
# Success! stderr is empty after removing all "NotFound" lines.
break
fi
fi
retries=$((retries+1))
if [ ${retries} -eq ${KUBECTL_RETRY} ]
then
ret=1
break
fi
# log stderr and empty the tmpfile
cat "${stderr}" > /dev/stderr
true > "${stderr}"
echo "$(date): 'kubectl_retry ${*}' failed (${retries}/${KUBECTL_RETRY}), will retry in ${KUBECTL_RETRY_DELAY} seconds" > /dev/stderr
sleep ${KUBECTL_RETRY_DELAY}
# reset ret so that a next working kubectl does not cause a non-zero
# return of the function
ret=0
done
echo "$(date): 'kubectl_retry ${*}' done (ret=${ret})" > /dev/stderr
# write output so that calling functions can consume it
cat "${stdout}" > /dev/stdout
cat "${stderr}" > /dev/stderr
rm -f "${stdout}" "${stderr}"
return ${ret}
}