Last active
February 29, 2024 13:12
-
-
Save kimdre/818953ef99ba59963e1bac1f15219b18 to your computer and use it in GitHub Desktop.
kubectl alias for namespaces
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
function kns { | |
if [[ "$1" == "-h" || "$1" == "--help" ]]; then | |
echo -e "Use kns to change or get the current namespace:\n" | |
echo -e " kns\t\t Show current namespace" | |
echo -e " kns <namespace> Change to namespace" | |
return 0 | |
fi | |
if [ "$1" ]; then | |
namespaces="$(k get ns | grep "$1")" | |
ns_count="$(echo "$namespaces" | grep -c "^$1")" | |
[ "$ns_count" -gt 1 ] && echo "Error: Found $ns_count namespaces with given pattern:" && echo $namespaces && return 2 | |
[ "$ns_count" -lt 1 ] && echo "Error: No namespace found with given pattern" && return 1 | |
kubectl config set-context --current --namespace $1 | |
else | |
echo -n "Current namespace: " | |
kubectl config view --minify | grep namespace | cut -d" " -f6 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment