Skip to content

Instantly share code, notes, and snippets.

@acarmisc
Created March 15, 2024 17:55
Show Gist options
  • Save acarmisc/8fb7fe99529b7cca7342d8f8646b2bfc to your computer and use it in GitHub Desktop.
Save acarmisc/8fb7fe99529b7cca7342d8f8646b2bfc to your computer and use it in GitHub Desktop.
Export each kubernetes context to single file
#!/bin/bash
# create target folder
mkdir -p contexts
# get all contexts name
contexts=$(kubectl config get-contexts -o=name)
# cycle on each context
for context in $contexts; do
slug=$(echo "$context" | tr -s '[:upper:]' '[:lower:]' | tr -s '[:space:]' '-' | tr -d ':' | tr '/' '-')
slug=${slug%-}
kubectl config view --minify --flatten --context="$context" > "contexts/${slug}.kubeconfig"
echo "Context $context exported to contexts/${slug}.kubeconfig"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment