Last active
February 28, 2022 07:32
-
-
Save babarot/bb820b99fdba605ea4bd4fb29046ce58 to your computer and use it in GitHub Desktop.
Get current GCP project name / Kubernetes context which you are on.
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
#!/bin/bash | |
if ! type jq &>/dev/null; then | |
echo "jq: not found" >&2 | |
exit 1 | |
fi | |
if ! type gcloud &>/dev/null; then | |
echo "gcloud: not found" >&2 | |
exit 1 | |
fi | |
gcloud config configurations list --format='json' \ | |
| jq -r '.[] | select(.is_active==true) | .properties.core.project' |
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
#!/bin/bash | |
while (( ${#} > 0 )) | |
do | |
case "${1}" in | |
--context) | |
context="${2}" | |
shift | |
shift | |
;; | |
--context=*) | |
context="${1#*=}" | |
shift | |
;; | |
esac | |
done | |
if ! type kubectl &>/dev/null; then | |
echo "kubectl command not found" >&2 | |
exit 1 | |
fi | |
if ! context="$(kubectl config current-context 2>/dev/null)"; then | |
echo "NaN" | |
exit 0 | |
fi | |
namespace="$(kubectl config view -o "jsonpath={.contexts[?(@.name==\"${context}\")].context.namespace}")" | |
if [[ -z "${namespace}" ]]; then | |
namespace="default" | |
fi | |
regions=( | |
asia-northeast1-a | |
asia-northeast1-b | |
asia-northeast1-c | |
asia-east1-a | |
asia-east1-b | |
asia-east1-c | |
) | |
for region in "${regions[@]}" | |
do | |
context=${context%_${region}*} | |
done | |
echo "${context}/${namespace}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment