Created
June 10, 2018 20:09
-
-
Save ralucas/e1aa1af291d463804acdf76d7edc194f to your computer and use it in GitHub Desktop.
Bash script for switching kubernetes contexts
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 -e | |
APP=$1 | |
ENV=$2 | |
if [ -z "$APP" ] || [ -z "$ENV" ]; then | |
echo -e "Missing parameters\n\nUsage:\n\tswitch_kubectl [app_name] [environment]" | |
exit 0 | |
fi | |
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then | |
echo -e "Usage:\n\tswitch_kubectl [app_name] [environment]" | |
exit 0 | |
fi | |
RED=`tput setaf 1` | |
GREEN=`tput setaf 2` | |
RESET=`tput sgr0` | |
CONTEXT=`kubectl config view | grep "${APP}-${ENV}" -m 1 | sed -E "s/[[:space:]]+name\:[[:space:]]//"` | |
echo "Using ${CONTEXT}" | |
SWITCHED_CONTEXT=`kubectl config use-context ${CONTEXT}` | |
echo $SWITCHED_CONTEXT | |
SC=`echo ${SWITCHED_CONTEXT} | grep -o "${CONTEXT}"` | |
if [ "${SC}" != "${CONTEXT}" ]; then | |
echo "Bad kubectl context!" | |
else | |
echo "Successfully switched kubernetes to application [ ${RED}${APP}${RESET} ] on [ ${GREEN}${ENV}${RESET} ]" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment