Skip to content

Instantly share code, notes, and snippets.

@walkerjam
Last active December 24, 2018 16:04
Show Gist options
  • Select an option

  • Save walkerjam/0fa51286af3cb29b996c359fd2e0cada to your computer and use it in GitHub Desktop.

Select an option

Save walkerjam/0fa51286af3cb29b996c359fd2e0cada to your computer and use it in GitHub Desktop.
Change kubernetes namespace for current context
#!/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