Skip to content

Instantly share code, notes, and snippets.

@tanelmae
Created October 29, 2018 14:03
Show Gist options
  • Save tanelmae/b638ba113bd394702038954dd2010da6 to your computer and use it in GitHub Desktop.
Save tanelmae/b638ba113bd394702038954dd2010da6 to your computer and use it in GitHub Desktop.
Get GKE context by name
# Assumes unique cluster names
function get-context() {
local CLUSTER_NAME=${1}
[ -z ${CLUSTER_NAME} ] && echo "Give cluster name" && return 1
local CURRENT_PROJECT=$(gcloud config get-value project)
local CLUSTER_ZONE=$(gcloud container clusters list --filter=name:${CLUSTER_NAME} --format='value(zone)')
[ -z ${CLUSTER_ZONE} ] && return 1
gcloud container clusters get-credentials ${CLUSTER_NAME} --zone=${CLUSTER_ZONE}
kubectl config rename-context "gke_${CURRENT_PROJECT}_${CLUSTER_ZONE}_${CLUSTER_NAME}" ${CLUSTER_NAME}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment