Last active
December 24, 2018 16:04
-
-
Save walkerjam/0fa51286af3cb29b996c359fd2e0cada to your computer and use it in GitHub Desktop.
Change kubernetes namespace for current context
This file contains hidden or 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 | |
| CONTEXT=$(kubectl config current-context) | |
| NAMESPACE=$(kubectl config view -o jsonpath="{.contexts[0].context.namespace}") | |
| echo "Current namespace: $(tput setaf 6)$NAMESPACE $(tput sgr0)" | |
| NEW_NAMESPACE="$1" | |
| if [ -z "$NEW_NAMESPACE" ]; then | |
| echo | |
| echo "Select a new namespace:" | |
| NAMESPACES=( $(kubectl get namespace -o name | awk -F "/" '{ print $2 }' | sort) ) | |
| PS3="$(tput setaf 6) > Namespace: $(tput sgr0)" | |
| select CHOICE in "${NAMESPACES[@]}"; do | |
| for NS in "${NAMESPACES[@]}"; do | |
| if [[ $NS == $CHOICE ]]; then | |
| NEW_NAMESPACE=$CHOICE | |
| break 2 | |
| fi | |
| done | |
| done | |
| echo | |
| fi | |
| kubectl config set-context $CONTEXT --namespace=$NEW_NAMESPACE > /dev/null | |
| echo "Set namespace to $(tput setaf 6)$NEW_NAMESPACE $(tput sgr0)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment