Last active
September 11, 2019 14:02
-
-
Save kam1kaze/e0ed8a58fba0fe73a55443c41e8db053 to your computer and use it in GitHub Desktop.
GKE switcher
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
#!/usr/bin/env bash | |
set -eu | |
sedi () { | |
sed --version &>/dev/null && sed -i -- "$@" || sed -i "" "$@" | |
} | |
if ! command -v fzf &>/dev/null; then | |
echo 'Please install `fzf` https://github.com/junegunn/fzf#using-homebrew-or-linuxbrew' | |
return 1 | |
fi | |
project=$( | |
gcloud config configurations list \ | |
| fzf --header="### Choose google cloud project" --header-lines=1 -1 --layout=reverse \ | |
| awk '{print $1}' | |
) | |
gcloud config configurations activate "${project}" | |
cluster_info=$( | |
gcloud container clusters list \ | |
| fzf --header="### Choose GKE cluster" --header-lines=1 -1 --layout=reverse \ | |
| awk '{print $1, $2}' | |
) | |
IFS=" " read -r -a cluster <<< "${cluster_info}" | |
command="gcloud container clusters get-credentials" | |
[[ ${cluster[1]} =~ -[a-z]$ ]] && location="--zone=" || location+="--region=" | |
location+=${cluster[1]} | |
gcloud container clusters get-credentials "$location" "${cluster[0]}" | |
eval $(gcloud container clusters describe "$location" "${cluster[0]}" --format="config[export](privateClusterConfig)") | |
sedi "s,https://$privateClusterConfig_privateEndpoint$,https://$privateClusterConfig_publicEndpoint,g" ~/.kube/config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment