Last active
March 20, 2025 15:05
-
-
Save vicenteherrera/8ee0e0fce64a5731a860b7f1c301c135 to your computer and use it in GitHub Desktop.
Set up a cloud shell to interact with Kubernetes clusters
This file contains 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
#!/bin/bash | |
# Use it with: | |
# eval "$(curl -fsSL https://gist.githubusercontent.com/vicenteherrera/8ee0e0fce64a5731a860b7f1c301c135/raw/5b0bdcab2d13af894fce60a3423cbca982b07f64/config.sh)" | |
# Set up aliases for kubectl | |
alias k='kubectl' | |
# Function to change the default namespace or list all namespaces | |
kns() { | |
if [ -z "$1" ]; then | |
current=$(kubectl config view --minify -o jsonpath='{..namespace}') | |
current=${current:-default} | |
echo "Available namespaces:" | |
namespaces=$(kubectl get namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') | |
for ns in $namespaces; do | |
if [ "$ns" == "$current" ]; then | |
echo "* $ns (current)" | |
else | |
echo " $ns" | |
fi | |
done | |
else | |
kubectl config set-context --current --namespace="$1" | |
echo "Switched to namespace '$1'" | |
fi | |
} | |
# Export the aliases and functions | |
export -f kns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment