Skip to content

Instantly share code, notes, and snippets.

@othyn
Last active December 4, 2021 13:18
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 othyn/afe8f81c769017dca8f4e67a19b02ce0 to your computer and use it in GitHub Desktop.
Save othyn/afe8f81c769017dca8f4e67a19b02ce0 to your computer and use it in GitHub Desktop.
Easy ZSH/Bash alias for swapping kubectl contexts on the fly
# List available kubectl contexts
# Based on the below command context storage mechanism
function kswl() {
echo "Listing available kubectl contexts:"
ls -lah ~/.kube/*-config.yaml
}
# Switch kubectl context
# Context files should be stored in the format: ~/.kube/{context_name}-config.yaml
function ksw() {
if [ -z "$1" ]; then
echo "You need to provide the prefix for a config to switch to!"
echo "Expected usecase: {param}-config.yaml"
echo "Available contexts:"
kswl
return 1
fi
export KUBECONFIG=~/.kube/${1}-config.yaml
echo "Loaded context: $(kubectl config view --output jsonpath='{.clusters[0].name}')!"
}
# Fix kube config permissions
# Based on the above command context storage mechanism
function kcfp() {
kswl
echo "Fixing kubectl config permissions, should only be readable by the user"
chmod 600 ~/.kube/*-config.yaml
kswl
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment