Add time limit for successfully deploying

- Add config var for max wait time for a deployment
- Move exit function and trap to the beginning of the script
- Execute the rollout status command using timeout
-- Use signal 2 (SIGINT; same pressing CTRL+C)
-- Abort the command if it is still running when max time is reached
-- If the command was aborted, rollback this deployment
This commit is contained in:
Devon Mackay 2018-09-21 12:42:16 -04:00 committed by Patrick Smith
parent f684990666
commit 2ba9745c2e

View File

@ -8,6 +8,17 @@ set -o errexit
set -o nounset set -o nounset
# set -o xtrace # set -o xtrace
# Config
MAX_DEPLOY_WAIT='5m'
# Remove the K8S CA file when the script exits
function cleanup {
printf "Cleaning up...\n"
rm -vf "${HOME}/k8s_ca.crt"
printf "Cleaning done."
}
trap cleanup EXIT
# Decode and save the K8S CA cert # Decode and save the K8S CA cert
echo "${K8S_CA_CRT}" | base64 -d - > "${HOME}/k8s_ca.crt" echo "${K8S_CA_CRT}" | base64 -d - > "${HOME}/k8s_ca.crt"
@ -31,13 +42,10 @@ kubectl config current-context
kubectl -n atat set image deployment.apps/atst atst="${ATAT_DOCKER_REGISTRY_URL}/${PROD_IMAGE_NAME}:${GIT_SHA}" kubectl -n atat set image deployment.apps/atst atst="${ATAT_DOCKER_REGISTRY_URL}/${PROD_IMAGE_NAME}:${GIT_SHA}"
# Wait for deployment to finish # Wait for deployment to finish
kubectl -n atat rollout status deployment/atst if ! timeout -s 2 "${MAX_DEPLOY_WAIT}" kubectl -n atat rollout status deployment/atst
then
# Remove the K8S CA file when the script exits # Deploy did not finish before max wait time; abort and rollback the deploy
function cleanup { kubectl -n atat rollout undo deployment/atst
printf "Cleaning up...\n" # Exit with a non-zero return code
rm -vf "${HOME}/k8s_ca.crt" exit 2
printf "Cleaning done." fi
}
trap cleanup EXIT