forked from GoogleCloudPlatform/k8s-config-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 6d3f51d2a8253079787a628613f639ab7ecbec5b Move retry script into hack by Config Connector Team <[email protected]> - 33c1191e42d961b79c4d2043b80f7d48d44092c4 Add docker-build / docker-push to operator Makefile by Config Connector Team <[email protected]> - fad6aa89ca67813c61179fb45303a1c23b0d0252 More github makefiles to root, internal makefiles to inte... by Config Connector Team <[email protected]> - f57fd36fc4e8ff0c20fb3e63a83ed3dbfb192701 Move more excluded scripts under google-internal by Config Connector Team <[email protected]> GitOrigin-RevId: 6d3f51d2a8253079787a628613f639ab7ecbec5b
- Loading branch information
1 parent
262f90a
commit 691a8c8
Showing
5 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/env bash | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
COMMAND_TO_RUN="" | ||
MAX_RETRIES=10 | ||
SLEEP_SECONDS=5 | ||
SUCCESS_REGEX="" | ||
|
||
while [[ $# -gt 0 ]]; do | ||
case "${1}" in | ||
# The number of seconds to wait after a failed command before retrying, default 5, example: | ||
# --sleep-seconds 15 | ||
--sleep-seconds) SLEEP_SECONDS="${2:-}"; shift ;; | ||
# The maximum number of times to try the command, default 10, example | ||
# --max-retries 3 | ||
--max-retries) MAX_RETRIES="${2:-}"; shift ;; | ||
# The command to retry, example | ||
# --command 'gcloud projects add-iam-policy-binding my-project-id --member "user:[email protected]" --role "roles/owner"' | ||
--command) COMMAND_TO_RUN="${2:-}"; shift ;; | ||
# if the stdout / stderr of the command matches the success-regex, then the command will be considered a success, | ||
# regardless of the exit code. This is to help handle ill-behaving CLIs that don't provide granular exit | ||
# codes which indicate success. | ||
--success-regex) SUCCESS_REGEX="${2:-}"; shift;; | ||
*) echo "Unrecognized command line parameter: $1"; exit 1 ;; | ||
esac | ||
shift | ||
done | ||
|
||
if [[ "${COMMAND_TO_RUN}" == "" ]]; then | ||
echo "The --command parameter is required" | ||
exit 1 | ||
fi | ||
|
||
RETRY_NUM=1 | ||
RESULT=0 | ||
|
||
for (( RETRY_NUM=1; RETRY_NUM<=${MAX_RETRIES}; RETRY_NUM++ )) | ||
do | ||
TEST_FILE=$(mktemp) | ||
trap "rm -f $TEST_FILE" EXIT | ||
echo "Attempt ${RETRY_NUM}/${MAX_RETRIES} of '${COMMAND_TO_RUN}'..." | ||
if eval "${COMMAND_TO_RUN} &> $TEST_FILE"; then | ||
echo "Succeeded." | ||
exit $? | ||
else | ||
RESULT=$? | ||
OUTPUT=$(cat "${TEST_FILE}") | ||
if [[ "${SUCCESS_REGEX}" != "" ]] && echo "${OUTPUT}" | grep -q "${SUCCESS_REGEX}"; then | ||
echo "Succeeded, since output \"$OUTPUT\" matched regex \"${SUCCESS_REGEX}\"" | ||
exit 0 | ||
else | ||
echo "Failed with exit code ${RESULT} and output ${OUTPUT}" | ||
if [[ ${RETRY_NUM} -lt ${MAX_RETRIES} ]]; then | ||
sleep "${SLEEP_SECONDS}" | ||
# Some backoff | ||
sleep "${RETRY_NUM}" | ||
# Some randomization | ||
sleep $((RANDOM % 10)) | ||
fi | ||
fi | ||
fi | ||
done | ||
|
||
echo "FAIL: The command '${COMMAND_TO_RUN}' failed ${MAX_RETRIES} times" | ||
exit ${RESULT} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters