Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Last active September 18, 2023 13:16
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 iTrooz/55c694bbe020415ea5c77b9eeb665304 to your computer and use it in GitHub Desktop.
Save iTrooz/55c694bbe020415ea5c77b9eeb665304 to your computer and use it in GitHub Desktop.
kubeenv
function kubeenv() {
DIR=$(realpath ~/.config/kube)
if [ $# -eq 0 ]; then
>&2 echo "$0: Please specify a config file or -l to list them"
return 1
fi
if [[ "$1" = "-l" ]]; then
echo "Config files in $DIR :"
ls $DIR
return 0
fi
if [ "${1:0:1}" = "/" ]; then # check if absolute path
filepath=$1
else
filepath="$DIR/$1"
fi
if [ -f $filepath ]; then
export KUBECONFIG=$filepath
echo "$0: Success"
else
>&2 echo "$0: file $filepath doesn't exist"
return 1
fi
}
# What is this
function used to set your KUBECONFIG variable to a config file easily. I prefer this rather than `kubectl config use-context` becauses it is dependnet to the current shell process, and so forces me to opt in a given config
# Installation
Simply put this function in your .zshrc, .bashrc, or similar
Then you can run `kubeenv <config file>` as a regular shell command
# Licence
CC0 / Public Domain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment