Skip to content

Instantly share code, notes, and snippets.

@jgreat
Last active February 5, 2018 22:47
Show Gist options
  • Save jgreat/7757c6c21622ab886b757693e0654c01 to your computer and use it in GitHub Desktop.
Save jgreat/7757c6c21622ab886b757693e0654c01 to your computer and use it in GitHub Desktop.
Export a kubectl portable config. Import into rancher or other tools.
#!/bin/bash
### kubectl should really allow you to export a singel context.
### if you only have one context you can just run `kubectl config view --flatten`
### if you have more than one context use this script.
###
### **Requires jq: https://stedolan.github.io/jq/
###
context=${1:-"minikube"}
full_config=$(kubectl config view --flatten -o json)
## extract one context
contexts=$( echo $full_config | jq -r ".contexts[] | select(.name == \"${context}\")" )
if [[ -z $contexts ]]; then
echo "${context} context not found in kubectl config" >&2
exit 1
fi
cluster=$( echo $contexts | jq -r ".context.cluster" )
user=$( echo $contexts | jq -r ".context.user" )
clusters=$( echo $full_config | jq -r ".clusters[] | select(.name == \"${cluster}\")" )
users=$( echo $full_config | jq -r ".users[] | select(.name == \"${user}\")" )
output=$(cat <<EOF
{
"apiVersion": "v1",
"kind": "Config",
"current-context": "${context}",
"contexts": [ ${contexts} ],
"users": [ ${users} ],
"clusters": [ ${clusters} ]
}
EOF
)
echo $output | jq '.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment