Skip to content

Instantly share code, notes, and snippets.

@ngraf
Last active August 7, 2023 11:01
Show Gist options
  • Save ngraf/35f0fc30f7118e97bda69ae89fa3759c to your computer and use it in GitHub Desktop.
Save ngraf/35f0fc30f7118e97bda69ae89fa3759c to your computer and use it in GitHub Desktop.
A script to switch kubectl context easily. For example by creating this file in "/usr/local/bin/context" you can switch context easily by executing "context" in console on any place.
#!/usr/bin/env bash
CONTEXTS=`kubectl config get-contexts -o name | sort`
# Save current IFS
SAVEIFS=$IFS
# Change IFS to new line.
IFS=$'\n'
CONTEXTS=($CONTEXTS)
# Restore IFS
IFS=$SAVEIFS
echo -e "\nselect context: (current context: `kubectl config current-context`)\n"
select CONTEXT in "${CONTEXTS[@]}"
do
echo ""
kubectl config use-context $CONTEXT
break;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment