Skip to content

Instantly share code, notes, and snippets.

@cbrgm
Created April 8, 2019 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbrgm/56866a8c82b0e205e5cadbf560e1c608 to your computer and use it in GitHub Desktop.
Save cbrgm/56866a8c82b0e205e5cadbf560e1c608 to your computer and use it in GitHub Desktop.
Create a kubeconfig for an existing serviceaccount in kubernetes
#!/bin/bash
# your server name goes here
server=https://my.k8s.cluster:6443
# the name of the secret containing the service account token goes here
name=super-secret-token-2l269
# the namespace containing the secret
namespace=kube-system
# don't touch this!
ca=$(kubectl get -n $namespace secret/$name -o jsonpath='{.data.ca\.crt}' | base64 --decode)
token=$(kubectl get -n $namespace secret/$name -o jsonpath='{.data.token}' | 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}
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment