Skip to content

Instantly share code, notes, and snippets.

@vicenteherrera
Last active March 20, 2025 15:05
Show Gist options
  • Save vicenteherrera/8ee0e0fce64a5731a860b7f1c301c135 to your computer and use it in GitHub Desktop.
Save vicenteherrera/8ee0e0fce64a5731a860b7f1c301c135 to your computer and use it in GitHub Desktop.
Set up a cloud shell to interact with Kubernetes clusters
#!/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