Last active
March 8, 2021 23:40
-
-
Save muvaf/dbd9417f6aed9a90a55a9ba7d45222cc to your computer and use it in GitHub Desktop.
Get kubeconfig for a Service Account without modifying existing kubeconfig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eE | |
# jq has to be installed. | |
# ./get-kubeconfig.sh sa-name > tmp-cfg | |
SA_NAME=${1} | |
SECRET_NAME=$(kubectl get serviceaccount ${SA_NAME} -o jsonpath='{.secrets[0].name}') | |
CERT_AUTH_DATA=$(kubectl get secret ${SECRET_NAME} -o jsonpath='{.data.ca\.crt}') | |
TOKEN=$(kubectl get secret ${SECRET_NAME} -o jsonpath='{.data.token}' | base64 -D) | |
CONTEXT=$(kubectl config current-context) | |
URL=$(kubectl config view -o json | jq ".clusters[] | select(.name == \"${CONTEXT}\")" | jq '.cluster.server') | |
echo "apiVersion: v1 | |
clusters: | |
- cluster: | |
certificate-authority-data: ${CERT_AUTH_DATA} | |
server: ${URL} | |
name: kind-kind | |
contexts: | |
- context: | |
cluster: kind-kind | |
namespace: default | |
user: ${SA_NAME} | |
name: ${SA_NAME} | |
current-context: ${SA_NAME} | |
kind: Config | |
preferences: {} | |
users: | |
- name: ${SA_NAME} | |
user: | |
token: ${TOKEN}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment