Skip to content

Instantly share code, notes, and snippets.

@ddgenome
Last active May 15, 2020 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddgenome/79581730c6e45943681155337f96797a to your computer and use it in GitHub Desktop.
Save ddgenome/79581730c6e45943681155337f96797a to your computer and use it in GitHub Desktop.
Select kubectl context based on regular expression matching
# alias k for kubectl
# usage: k KUBECTL_ARGS...
function k () {
kubectl "$@"
}
# print or switch kubectl kubeconfig contexts
# usage: kc [MATCH_REGEXP]
function kc ()
{
local context=$1
if [[ ! -n $context ]]; then
k config get-contexts
else
local match
match=$(k config get-contexts | sed -e 1d -e 's/^[* ]*//' | awk "/$context/ { print \$1; exit 0 }")
if [[ $? -ne 0 || ! -n $match ]]; then
echo "kc: no context matched /$context/: $match" 1>&2
return 1
fi;
k config use-context "$match"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment