Last active
December 4, 2021 13:18
-
-
Save othyn/afe8f81c769017dca8f4e67a19b02ce0 to your computer and use it in GitHub Desktop.
Easy ZSH/Bash alias for swapping kubectl contexts on the fly
This file contains hidden or 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
# 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