Skip to content

Instantly share code, notes, and snippets.

@Colum
Last active June 23, 2021 21:51
Show Gist options
  • Save Colum/7bd7a21ab8187e2f5d1d6bc050aa95eb to your computer and use it in GitHub Desktop.
Save Colum/7bd7a21ab8187e2f5d1d6bc050aa95eb to your computer and use it in GitHub Desktop.
Quickly change kubectl and gcloud projects
#!/bin/bash
#
# Easily switch your gcloud and kubectl CLIs to a different project.
#
# Usage:
# kubeswitch --project kashoo-stage
# --project
# accepted values: tsb-stage, tsb-prod, kashoo-stage, kashoo-prod
#
# For OSX, place contents in /usr/local/bin/kubeswitch
#
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-p|--project)
PROJECT="$2"
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
GCP_P=""
GKE_CLUSTER=""
if [[ "$PROJECT" == "tsb-prod" ]] ; then
GCP_P="kashoo-legacy-production"
GKE_CLUSTER="ts --region us-west1"
elif [[ "$PROJECT" == "tsb-stage" ]] ; then
GCP_P="kashoo-legacy-staging"
GKE_CLUSTER="ts --region us-west1-a"
elif [[ "$PROJECT" == "kashoo-stage" ]] ; then
GCP_P="kashoo-legacy-staging"
GKE_CLUSTER="terraform-primary --region us-west1"
elif [[ "$PROJECT" == "kashoo-prod" ]] ; then
GCP_P="kashoo-legacy-production"
GKE_CLUSTER="terraform-primary --region us-west1"
else
echo "Expecting arg '--project' or '-p' followed by project"
echo " ex: --project [tsb-stage|tsb-prod|kashoo-stage|kashoo-prod]"
exit 0
fi
gcloud config set project $GCP_P
echo "-> gcloud CLI pointed at ${PROJECT}"
gcloud container clusters get-credentials $GKE_CLUSTER
echo "-> kubectl CLI pointed at ${PROJECT}"
if [[ $PROJECT == *prod ]] ; then
YELLOW='\033[1;33m'
NC='\033[0m'
printf "${YELLOW}WARNING! You are controlling production resources, proceed with caution.${NC}"
#echo "WARNING! You are controlling production resources, proceed with caution."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment