Skip to content

Instantly share code, notes, and snippets.

@InAnimaTe
Last active December 5, 2018 10:53
Show Gist options
  • Save InAnimaTe/eeeb4b01467d74c522b94f12ed009889 to your computer and use it in GitHub Desktop.
Save InAnimaTe/eeeb4b01467d74c522b94f12ed009889 to your computer and use it in GitHub Desktop.
kubectl context/namespace switcher!
function kcc () {
usage () {
echo -en "Usage: $0 <context> <namespace>\n"
}
result () {
echo -en "-> Context: \e[96m$context\e[0m\n-> Namespace: \e[92m$namespace\n\e[0m"
}
if [ $# -eq 0 ] ; then
## If no options, print the current context/cluster and namespace
context="$(kubectl config current-context 2>/dev/null)"
namespace="$(kubectl config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.namespace}")"
result
elif [ $# -eq 2 ]; then
## If options, assume time to set
context="$1"
namespace="$2"
kubectl config use-context "$context" > /dev/null
kubectl config set-context "$context" --namespace="$namespace" > /dev/null
result
else
usage
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment