Skip to content

Instantly share code, notes, and snippets.

@kam1kaze
Last active September 11, 2019 14:02
Show Gist options
  • Save kam1kaze/e0ed8a58fba0fe73a55443c41e8db053 to your computer and use it in GitHub Desktop.
Save kam1kaze/e0ed8a58fba0fe73a55443c41e8db053 to your computer and use it in GitHub Desktop.
GKE switcher
#!/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