Skip to content

Instantly share code, notes, and snippets.

@steven-terrana
Last active February 16, 2021 00:59
Show Gist options
  • Save steven-terrana/7260e278ba2f1307c9d56417ea4bd324 to your computer and use it in GitHub Desktop.
Save steven-terrana/7260e278ba2f1307c9d56417ea4bd324 to your computer and use it in GitHub Desktop.
Create KubeConfig
# from: https://stackoverflow.com/a/47776588
# which service account?
SA=
NAMESPACE=
# your k8s api endpoint goes here
server=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
# the name of the secret containing the service account token goes here
token=$(kubectl get sa ${SA} -n ${NAMESPACE} -o jsonpath='{.secrets[0].name}')
ca=$(kubectl get secret/$name -n ${NAMESPACE} -o jsonpath='{.data.ca\.crt}')
token=$(kubectl get secret/$name -n ${NAMESPACE} -o jsonpath='{.data.token}' | base64 --decode)
namespace=$(kubectl get secret/$name -n ${NAMESPACE} -o jsonpath='{.data.namespace}' | base64 --decode)
echo "
apiVersion: v1
kind: Config
clusters:
- name: default-cluster
cluster:
certificate-authority-data: ${ca}
server: ${server}
contexts:
- name: default-context
context:
cluster: default-cluster
namespace: default
user: default-user
current-context: default-context
users:
- name: default-user
user:
token: ${token}
" > sa.kubeconfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment