#!/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)"