Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Last active May 3, 2017 09:56
Show Gist options
  • Save ahawkins/578bc428c24fb71fb0e4d5afaa926e57 to your computer and use it in GitHub Desktop.
Save ahawkins/578bc428c24fb71fb0e4d5afaa926e57 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# This script is a workaround for https://github.com/kubernetes/helm/issues/2288.
# helm install --wait should do everything this script does. It should be deleted
# when the bug is fixed.
set -euo pipefail
main() {
local counter=0 release timeout pod_status
scratch="$(mktemp)"
while getopts ':r:t:' opt; do
case "${opt}" in
r)
release="${OPTARG}"
;;
t)
timeout="${OPTARG}"
;;
\?)
echo "Unknown argument: -${OPTARG}" 1>&2
return 1
;;
:)
echo "Option -${OPTARG} requires an argument" 1>&2
return 1
;;
esac
done
shift $((OPTIND-1))
while [ $counter -lt $timeout ]; do
helm ls -q --kube-context "${KUBE_CONTEXT}" > "${scratch}"
if ! grep -qF "${release}" "${scratch}" ; then
echo "${release} not found. ${counter}/${timeout} checks completed; retrying."
cat "${scratch}" 2>&1
echo 1>&2
# NOTE: The pre-increment usage. This makes the arithmatic expression
# always exit 0. The post-increment form exits non-zero when counter
# is zero. More information here: http://wiki.bash-hackers.org/syntax/arith_expr#arithmetic_expressions_and_return_codes
((++counter))
sleep 10
else
break
fi
done
if [ $counter -eq $timeout ]; then
echo "${release} failed to appear."
return 1
fi
counter=0
while [ $counter -lt $timeout ]; do
pending_pods="$(kubectl get pods \
-l "release=${release}" \
-o 'custom-columns=NAME:.metadata.name,STATUS:.status.phase' \
-n "${KUBE_NAMESPACE}" \
--context "${KUBE_CONTEXT}" \
| tail -n +2 \
)"
if echo "${pending_pods}" | grep -qvF 'Running'; then
echo "${release} pods not ready. ${counter}/${timeout} checks completed; retrying."
echo "${pending_pods}" | grep -vF 'Running' 1>&2
echo 1>&2
# NOTE: The pre-increment usage. This makes the arithmatic expression
# always exit 0. The post-increment form exits non-zero when counter
# is zero. More information here: http://wiki.bash-hackers.org/syntax/arith_expr#arithmetic_expressions_and_return_codes
((++counter))
sleep 10
else
echo "All ${release} pods running. Done!"
return 0
fi
done
echo "Release ${release} did not complete in time" 1>&2
return 1
}
main "$@"
#!/usr/bin/env bash
set -euo pipefail
main() {
local kinds=( Service Deployment Pod Secret ConfigMap )
local counter=0 attempts=30
for release in "$@"; do
echo "--> Deleting release ${release}"
if helm ls --kube-context "${KUBE_CONTEXT}" | grep -qF "${release}"; then
echo "==> Found helm release; deleting with --purge"
helm delete "${release}" --purge --kube-context "${KUBE_CONTEXT}"
else
echo "==> No release found; deleting manually"
for kind in "${kinds[@]}"; do
echo "==> Deleting any dangling ${kind}"
kubectl delete "${kind}" \
-l "release=${release}" \
-n "${KUBE_NAMESPACE}" \
--force \
--grace-period 0 \
--context "${KUBE_CONTEXT}" 2>/dev/null
done
fi
echo "--> Awaiting resource deleting confirmation"
for kind in "${kinds[@]}"; do
counter=0
while [ $counter -lt $attempts ]; do
pending_resources="$(kubectl get "${kind}" \
-o wide \
-l "release=${release}" \
-n "${KUBE_NAMESPACE}" \
--context "${KUBE_CONTEXT}" 2>/dev/null
)"
if [ -n "${pending_resources}" ]; then
echo "${release} ${kind} still running. ${counter}/${attempts} tests completed; retrying."
echo "${pending_resources}" 1>&2
echo 1>&2
# NOTE: The pre-increment usage. This makes the arithmatic expression
# always exit 0. The post-increment form exits non-zero when counter
# is zero. More information here: http://wiki.bash-hackers.org/syntax/arith_expr#arithmetic_expressions_and_return_codes
((++counter))
sleep 10
else
break
fi
done
if [ $counter -eq $attempts ]; then
echo "${release} ${kind} failed to delete in time.";
return 1
fi
done
echo "--> Awaiting helm confirmation"
counter=0
while [ $counter -lt $attempts ]; do
if helm ls --all --kube-context "${KUBE_CONTEXT}" | grep -qF "${release}"; then
echo "${release} still in tiller. ${counter}/${attempts} checks completed; retrying."
# NOTE: The pre-increment usage. This makes the arithmatic expression
# always exit 0. The post-increment form exits non-zero when counter
# is zero. More information here: http://wiki.bash-hackers.org/syntax/arith_expr#arithmetic_expressions_and_return_codes
((++counter))
sleep 10
else
break
fi
done
if [ $counter -eq $attempts ]; then
echo "${release} failed to purge from tiller delete in time.";
return 1
fi
done
}
main "$@"
#!/usr/bin/env bash
set -euo pipefail
main() {
local success=0 failure=0
while true; do
echo "=> Attemping"
echo
if script/ci/test; then
((++success))
else
((++failure))
fi
echo
echo "=> Results: failure=${failure} success=${success}"
done
}
main "$@"
#!/usr/bin/env bash
set -euo pipefail
main() {
local release="${1:-}" test_output
test_output="$(mktemp)"
if [ -z "${release}" ]; then
echo "USAGE: ${0} RELEASE" 1>&2
return 1
fi
kubectl delete pod \
-l "release=${release},app=smoke-test" \
-n "${KUBE_NAMESPACE}" \
--context "${KUBE_CONTEXT}" &> /dev/null
# FIXME: This is a work around for https://github.com/kubernetes/helm/issues/2166
set +e
helm test "${release}" --timeout 600 --kube-context "${KUBE_CONTEXT}" > "${test_output}"
set -e
if grep -qF 'PASSED' "${test_output}"; then
cat "${test_output}"
kubectl delete pod \
-l "release=${release},app=smoke-test" \
-n "${KUBE_NAMESPACE}" \
--context "${KUBE_CONTEXT}" &> /dev/null
return 0
else
echo "${release} test failed! Capturing logs and cleaning up"
echo
cat "${test_output}"
while read -r pod; do
echo "${pod} Logs:"
echo
kubectl logs "${pod}" -n "${KUBE_NAMESPACE}" --context "${KUBE_CONTEXT}"
kubectl delete pod "${pod}" -n "${KUBE_NAMESPACE}" --context "${KUBE_CONTEXT}"
echo
done < <(kubectl get pod \
--show-all \
-o "custom-columns=NAME:.metadata.name" \
-l "release=${release},app=smoke-test" \
-n "${KUBE_NAMESPACE}" \
--context "${KUBE_CONTEXT}" \
| tail -n +2)
return 1
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment