Skip to content

Instantly share code, notes, and snippets.

@mdrakiburrahman
Created April 7, 2022 15:54
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 mdrakiburrahman/ce791ec4afcef7ce18f59f9a50bdb7fe to your computer and use it in GitHub Desktop.
Save mdrakiburrahman/ce791ec4afcef7ce18f59f9a50bdb7fe to your computer and use it in GitHub Desktop.
Create Kubeconfig from Service Account
# the Namespace and ServiceAccount name that is used for the config
namespace=arc
serviceAccount=arcOnboard
######################
# actual script starts
set -o errexit
secretName=$(kubectl --namespace $namespace get serviceAccount $serviceAccount -o jsonpath='{.secrets[0].name}')
ca=$(kubectl --namespace $namespace get secret/$secretName -o jsonpath='{.data.ca\.crt}')
token=$(kubectl --namespace $namespace get secret/$secretName -o jsonpath='{.data.token}' | base64 --decode)
echo "
---
apiVersion: v1
kind: Config
clusters:
- name: ${clusterName}
cluster:
certificate-authority-data: ${ca}
server: ${server}
contexts:
- name: ${serviceAccount}@${clusterName}
context:
cluster: ${clusterName}
namespace: ${namespace}
user: ${serviceAccount}
users:
- name: ${serviceAccount}
user:
token: ${token}
current-context: ${serviceAccount}@${clusterName}
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment