Last active
August 1, 2017 02:25
-
-
Save TripleDogDare/17c0519c8febef9c05d40e50b2daf1a5 to your computer and use it in GitHub Desktop.
GCP kubernetes configuration/credentials fetching
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 | |
AUTH_LIST=$(gcloud --format=json auth list) | |
ACC_LIST=$(echo $AUTH_LIST | jq -r '.[].account') | |
if [ $(echo "${ACC_LIST}" | wc -l) -gt 1 ]; then | |
>&2 echo "Select account:" | |
select ACCOUNT in $(echo "${ACC_LIST}"); do | |
break | |
done | |
gcloud config set account "$ACCOUNT" | |
fi | |
PROJECT_LIST=$(gcloud projects list --format=json | jq -r '.[].projectId') | |
if [ $(echo "${PROJECT_LIST}" | wc -l) -gt 1 ]; then | |
>&2 echo "Select a project:" | |
select PROJECT in $(echo "${PROJECT_LIST}"); do | |
break | |
done | |
else | |
PROJECT=${PROJECT_LIST} | |
fi | |
>&2 echo "Getting cluster list for ${PROJECT}" | |
CLUSTER_DATA=$(gcloud --project=$PROJECT container clusters list --format=json) | |
CLUSTER_LIST=$(echo "$CLUSTER_DATA" | jq -r '.[].name') | |
if [ $(echo "$CLUSTER_LIST" | wc -l) -gt 1 ]; then | |
select CLUSTER in $(echo "$CLUSTER_LIST"); do | |
break | |
done | |
else | |
CLUSTER=$CLUSTER_LIST | |
fi | |
ZONE=$(echo "$CLUSTER_DATA" | jq -r 'select( .[].name == "cluster-1") | .[0].zone') | |
>&2 echo "Getting credentials for $PROJECT $CLUSTER $ZONE" | |
gcloud container clusters get-credentials "${CLUSTER}" --zone "${ZONE}" --project "${PROJECT}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment