Skip to content

Instantly share code, notes, and snippets.

@miminar
Created February 1, 2022 13:27
Show Gist options
  • Save miminar/27ebf53e314f652b6f75635ec5c79c53 to your computer and use it in GitHub Desktop.
Save miminar/27ebf53e314f652b6f75635ec5c79c53 to your computer and use it in GitHub Desktop.
Manipulate SAP DI HelmDeployment's state
#!/usr/bin/env bash
set -euo pipefail
scriptName="$(basename "${BASH_SOURCE[0]}")"
usage="$scriptName [options] NAME STATE
Switch state of HelmDeployment/NAME to the STATE. Allowed state values are \"deployed\" and
\"failed\".
Example:
$scriptName -n sdi diagnostic DEPLOYED
Options:
-h | --help
Show this message and exit.
-n | --namespace NAMESPACE
Kubernetes namespace where SAP Data Intelligence runs. Defaults to the current namespace."
readonly usage
function join() { IFS="$1"; shift; echo "$*"; }
longOptions=( namespace: help: )
function getSAToken() {
local saname secret token
saname="${nm}-vora-deployment-operator"
secret="$(oc get -o json "sa/$saname" -n "$nm" | \
jq -r '.secrets[] | .name | select(test("-token-"))')"
if [[ -z "${secret:-}" ]]; then
printf 'Failed to determine the token secret for serviceaccount "%s"\n!' "$saname"
return 1
fi
token="$(oc get "secret/$secret" -n "$nm" -o json | jq -r '.data.token' | base64 -d)"
if [[ -z "${token:-}" ]]; then
printf 'Failed to determine the token from secret "%s"\n!' "$secret"
return 1
fi
printf '%s' "$token"
}
function getToken() {
local token
token="$(oc whoami -t)"
if [[ -n "${token:-}" ]] && grep -q yes < <(oc auth can-i update helmdeployment -n "$nm"); then
printf '%s' "$token"
return 0
fi
getSAToken
}
TMPARGS="$(getopt -n "$(basename "${BASH_SOURCE[0]}")" -o hn: \
-l "$(join , "${longOptions[@]}")" -- "$@")"
eval set -- "$TMPARGS"
nm=""
while true; do
case "$1" in
-n | --namespace)
nm="$2"
shift 2
;;
-h | --help)
printf '%s\n' "$usage"
exit 0
;;
--)
shift
break
;;
*)
printf 'Unrecognized option "%s"!\n' "$1" >&2
exit 1
;;
esac
done
if [[ "$#" -lt 2 ]]; then
printf 'Missing NAME and/or STATE arguments!\n' >&1
exit 1
fi
name="$1"
state="$2"
state="$(tr '[:lower:]' '[:upper:]' <<<"$state")"
if [[ ! "$state" =~ ^(DEPLOYED|FAILED)$ ]]; then
printf 'STATE must be one of DEPLOYED, FAILED!\n' >&2
exit 1
fi
if [[ -z "${nm:-}" ]]; then
nm="$(oc project -q)"
fi
api="$(oc cluster-info | sed -n 's/.*is running at \(.\+\)/\1/p' | \
sed "s,\x1B\[[0-9;]*[a-zA-Z],,g")"
if [[ -z "${api:-}" ]]; then
printf 'Failed to determine API endpoint of the OpenShift server!\n'
exit 1
fi
token="$(getToken)"
if [[ -z "${token:-}" ]]; then
exit 1
fi
url="$(printf \
'%s/apis/installers.datahub.sap.com/v1alpha1/namespaces/%s/helmdeployments/%s/status' \
"$api" "$nm" "$name")"
obj="$(curl -s -L \
-H "Accept: application/json" \
-H "Authorization: Bearer $token" \
"$url")"
if [[ -z "${obj:-}" ]]; then
printf 'Could not get the current status of HelmDeployment/%s!\n' >&2 "$name"
exit 1
fi
if [[ "$(jq -r '.kind' <<<"$obj")" == 'Status' ]]; then
printf '%s\n' "$obj"
exit 1
fi
currentState="$(jq '.status.state' <<<"$obj")"
if [[ -z "${currentState:-}" ]]; then
printf 'Could not determine the current currentState!\n' >&2
exit 1
fi
printf 'Current state of HelmDeployment/%s: %s\n' "$name" "$state"
patch="$(jq -n -c --arg state "$state" '{
"status": {
"state": $state
}
}')"
curl -XPATCH \
-H "Accept: application/json" \
-H "Content-Type: application/merge-patch+json" \
-H "Authorization: Bearer $token" \
--data "$patch" "$url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment