Skip to content

Instantly share code, notes, and snippets.

@adamzaninovich
Last active August 14, 2021 19:24
Show Gist options
  • Save adamzaninovich/aff97ff9888750a644b7cb295ac6ab03 to your computer and use it in GitHub Desktop.
Save adamzaninovich/aff97ff9888750a644b7cb295ac6ab03 to your computer and use it in GitHub Desktop.
chkube is a bash function that manages multiple kubectl configs using symlinks

chkube bash function

chkube is a bash function that manages multiple kubectl configs using symlinks

Setup

  • copy this function to your bashrc aliases or functions file
  • put all configs in ~/.kube/ with names like my-config.yaml (they must end in .yaml)
  • symlink one of them to ~/.kube/config
  • use chkube to manage them after that

Notes

If you don't want to use as a function and would prefer a script, change all the return statements to exit and copy the code inside the function into a file with a shebang

If you don't want to include the helper functions, you can replace all occurances of ohai, bullet, and error with echo and exclude them.

Example

chkube example

# chkube manages kubectl configs using symlinks
# To setup:
# - put all configs in ~/.kube/ with names like my-config.yaml
# - symlink one of them to ~/.kube/config
# - use chkube to manage them after that
chkube() {
local conf="$HOME/.kube"
if [[ ! -L "${conf}/config" ]]; then
error "${conf}/config is not a symbolic link"
echo
ohai "To setup:"
bullet "put all configs in ~/.kube/ with names like my-config.yaml"
bullet "symlink one of them to ~/.kube/config"
bullet "use chkube to manage them after that"
return 1
fi
local current=$(basename $(readlink "${conf}/config") | cut -d . -f 1)
local available=$(\ls "${conf}/" | grep yaml | cut -d . -f 1)
if [[ "$1" == "" ]]; then
ohai "Usage: chkube some-config"
echo
ohai "Available configs are"
IFS=$'\n'
for item in $available; do
bullet "$item"
done
echo "Currently selected is $current"
return 0
else
if [[ -f "${conf}/${1}.yaml" ]]; then
rm "${conf}/config"
ln -s "${conf}/${1}.yaml" ${conf}/config
ohai "Set default config to $1"
else
error "$1 is not an available config"
chkube
return 1
fi
fi
}
# Helper functions and string formatters
# string formatters
if [[ -t 1 ]]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_blue="$(tty_mkbold 34)"
tty_yellow="$(tty_mkbold 33)"
tty_green="$(tty_mkbold 32)"
tty_red="$(tty_mkbold 31)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
ohai() {
printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
bullet() {
printf "${tty_yellow} ● ${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
error() {
printf "${tty_red}ERROR:${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment