Last active
May 15, 2020 15:07
-
-
Save ddgenome/79581730c6e45943681155337f96797a to your computer and use it in GitHub Desktop.
Select kubectl context based on regular expression matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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