-
-
Save anjmao/6c396d79273407be5e8116db7987fd34 to your computer and use it in GitHub Desktop.
Create an admin 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
#!/usr/bin/env bash | |
set -ex | |
kubectl create serviceaccount admin | |
kubectl create clusterrolebinding admin --clusterrole=cluster-admin --serviceaccount=default:admin | |
SECRET_NAME=$(kubectl get sa admin -o=jsonpath='{.secrets[0].name}') | |
TOKEN=$(kubectl get secret "${SECRET_NAME}" -ojsonpath='{.data.token}' | base64 -d) | |
CA=$(kubectl get secret "${SECRET_NAME}" -ojsonpath='{.data.ca\.crt}') | |
CURRENT_CONTEXT=$(kubectl config current-context) | |
CURRENT_CLUSTER=$(kubectl config view --raw -o=go-template='{{range .contexts}}{{if eq .name "'''"${CURRENT_CONTEXT}"'''"}}{{ index .context "cluster" }}{{end}}{{end}}') | |
CLUSTER_SERVER=$(kubectl config view --raw -o=go-template='{{range .clusters}}{{if eq .name "'''"${CURRENT_CLUSTER}"'''"}}{{ .cluster.server }}{{end}}{{ end }}') | |
cat << EOF > admin.yaml | |
apiVersion: v1 | |
kind: Config | |
current-context: ${CURRENT_CONTEXT} | |
contexts: | |
- name: ${CURRENT_CONTEXT} | |
context: | |
cluster: ${CURRENT_CONTEXT} | |
user: admin | |
namespace: default | |
clusters: | |
- name: ${CURRENT_CONTEXT} | |
cluster: | |
certificate-authority-data: ${CA} | |
server: ${CLUSTER_SERVER} | |
users: | |
- name: admin | |
user: | |
token: ${TOKEN} | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment