Created
February 28, 2025 05:44
-
-
Save tekumara/8882180bf038ba580851945ddc7272a9 to your computer and use it in GitHub Desktop.
add kube contexts for namespaces based on existing non-namespaced contexts
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 | |
if [ $# -eq 0 ]; then | |
echo "Usage: $0 namespace1 [namespace2 ...]" | |
echo "Example: $0 my-app my-other-app" | |
exit 1 | |
fi | |
CONFIG_FILE="${CONFIG_FILE:-$HOME/.kube/config}" | |
for ns in "$@"; do | |
if ! kubectl config get-contexts -o name | grep "$ns$" > /dev/null; then | |
echo "Adding context for $ns to $CONFIG_FILE" | |
# add namespaced contexts for each existing context that doesn't already have a namespace specified | |
NS=$ns yq -i '.contexts += [.contexts[] | select(.context.namespace == null) | {"context": (.context + {"namespace": strenv(NS)}), "name": .name + "/" + strenv(NS)}]' "$CONFIG_FILE" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment