From 416782d8789a188192cf0eecedb6cdcad970795f Mon Sep 17 00:00:00 2001 From: Rakshith R Date: Tue, 10 Aug 2021 11:37:36 +0530 Subject: [PATCH] ci: move kubectl_retry() to utils.sh to be able to import it Signed-off-by: Rakshith R (cherry picked from commit a15892a87a121e9e5205d71d8f3a536cdb8ce96c) --- scripts/rook.sh | 56 ++++-------------------------------------------- scripts/utils.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 52 deletions(-) create mode 100755 scripts/utils.sh diff --git a/scripts/rook.sh b/scripts/rook.sh index 1eb19defb..f3f932c1f 100755 --- a/scripts/rook.sh +++ b/scripts/rook.sh @@ -4,8 +4,10 @@ ROOK_VERSION=${ROOK_VERSION:-"v1.4.9"} ROOK_DEPLOY_TIMEOUT=${ROOK_DEPLOY_TIMEOUT:-300} ROOK_URL="https://raw.githubusercontent.com/rook/rook/${ROOK_VERSION}/cluster/examples/kubernetes/ceph" ROOK_BLOCK_POOL_NAME=${ROOK_BLOCK_POOL_NAME:-"newrbdpool"} -KUBECTL_RETRY=5 -KUBECTL_RETRY_DELAY=10 + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" +# shellcheck disable=SC1091 +[ ! -e "${SCRIPT_DIR}"/utils.sh ] || source "${SCRIPT_DIR}"/utils.sh trap log_errors ERR @@ -30,56 +32,6 @@ rook_version() { echo "${ROOK_VERSION#v}" | cut -d'.' -f"${1}" } -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" - ret=$(grep -cvw 'AlreadyExists' "${stderr}") - if [ "${ret}" -eq 0 ] - then - # Success! stderr is empty after removing all "AlreadyExists" 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 "kubectl_retry ${*} failed, will retry in ${KUBECTL_RETRY_DELAY} seconds" - - 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 - - # write output so that calling functions can consume it - cat "${stdout}" > /dev/stdout - cat "${stderr}" > /dev/stderr - - rm -f "${stdout}" "${stderr}" - - return ${ret} -} - function deploy_rook() { kubectl_retry create -f "${ROOK_URL}/common.yaml" kubectl_retry create -f "${ROOK_URL}/operator.yaml" diff --git a/scripts/utils.sh b/scripts/utils.sh new file mode 100755 index 000000000..afcae9eb3 --- /dev/null +++ b/scripts/utils.sh @@ -0,0 +1,54 @@ +#!/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" + ret=$(grep -cvw 'AlreadyExists' "${stderr}") + if [ "${ret}" -eq 0 ] + then + # Success! stderr is empty after removing all "AlreadyExists" 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 "kubectl_retry ${*} failed, will retry in ${KUBECTL_RETRY_DELAY} seconds" + + 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 + + # write output so that calling functions can consume it + cat "${stdout}" > /dev/stdout + cat "${stderr}" > /dev/stderr + + rm -f "${stdout}" "${stderr}" + + return ${ret} +}