Skip to content

Instantly share code, notes, and snippets.

@walkerjam
Last active December 24, 2018 16:04
Show Gist options
  • Save walkerjam/0fa51286af3cb29b996c359fd2e0cada to your computer and use it in GitHub Desktop.
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)"
CUR_NAMESPACE=$(kubectl config get-contexts | grep "*" | awk '{ print $5 }')
NEW_NAMESPACE=
LIST_OBJECTS=
while :; do
case $1 in
-h|-\?|--help)
cat << EOF
Usage: ${0##*/} [-h] [-l|--list] [NAMESPACE]
Change current Kubernetes namespace, and optionally list objects.
-h display this help and exit
-l list objects after changing namespace
EOF
exit
;;
-l|--list)
LIST_OBJECTS=1
;;
*)
if [[ -n "$1" ]]; then
NEW_NAMESPACE="$1"
fi
esac
shift
if [[ -z $1 ]]; then
break;
fi
done
if [ -z "$NEW_NAMESPACE" ]; then
echo "Select a new namespace:"
echo
NAMESPACES=( )
kubectl get namespace 2>/dev/null >/dev/null
if [ $? -eq 0 ]; then
# Our account can list cluster namespaces
NAMESPACES=( $(kubectl get namespace -o name | awk -F "/" '{ print $2 }' | sort) )
else
# Our account can NOT list cluster namespaces - they must be configured in the kubeconfig
NAMESPACES=( $(kubectl config get-contexts -o name | sort) )
fi
NS_OPTS=()
NS_LABELS=()
for NS in "${NAMESPACES[@]}"; do
NS_OPTS+=("$NS")
if [[ $NS == $CUR_NAMESPACE ]]; then
NS_LABELS+=("$(tput setaf 6)$NS$(tput sgr0) (current)")
else
NS_LABELS+=("$NS")
fi
done
I=0
for LABEL in "${NS_LABELS[@]}"; do
printf '%s\n' "$((++I))) $LABEL"
done >&2
NUM_ITEMS=${#NS_LABELS[@]}
PS3="$(tput setaf 6) > Namespace: $(tput sgr0)"
while :; do
printf %s "${PS3}" >&2
read -r INDEX
# Make sure that the input is either empty or that a valid index was entered.
[[ -z $INDEX ]] && break # empty input
(( INDEX >= 1 && INDEX <= NUM_ITEMS )) 2>/dev/null || { continue; }
break
done
if [[ -n $INDEX ]]; then
NEW_NAMESPACE="${NS_OPTS[$INDEX - 1]}"
else
NEW_NAMESPACE=$CUR_NAMESPACE
fi
fi
if [[ -n $NEW_NAMESPACE ]]; then
if [[ $CUR_NAMESPACE != $NEW_NAMESPACE ]]; then
kubectl config set-context $CONTEXT --namespace=$NEW_NAMESPACE > /dev/null
echo "Set namespace to $(tput setaf 6)$NEW_NAMESPACE$(tput sgr0)"
else
echo "Current namespace $(tput setaf 6)$CUR_NAMESPACE$(tput sgr0) (not changed)"
fi
echo
fi
if [[ -z $LIST_OBJECTS ]]; then
printf %s "List objects? [Y/n]" >&2
read -r LIST_OBJECTS
fi
if [[ $LIST_OBJECTS != 'n' ]]; then
echo
echo "----- $(tput setaf 6)Deployments$(tput sgr0) -----"
kubectl get deployments
echo
echo "----- $(tput setaf 6)Pods$(tput sgr0) -----"
kubectl get pods
echo
echo "----- $(tput setaf 6)Services$(tput sgr0) -----"
kubectl get services
echo
echo "----- $(tput setaf 6)Ingress$(tput sgr0) -----"
kubectl get ingress
echo
echo "----- $(tput setaf 6)Secrets$(tput sgr0) -----"
kubectl get secrets
echo
echo "----- $(tput setaf 6)Config Maps$(tput sgr0) -----"
kubectl get configmap
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment