-
-
Save ankuagar/f64667074b6c647c69d9890a30d624e2 to your computer and use it in GitHub Desktop.
Obtain GKE Basic Auth Credentials and use them
This file contains hidden or 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 | |
| CLUSTER_NAME="basicauth" | |
| LOCATION="--zone us-central1-a" | |
| #LOCATION="--region us-central1" | |
| # Get cluster metadata | |
| echo -n "Fetching cluster metadata..." | |
| CLUSTER="$(gcloud container clusters describe ${CLUSTER_NAME} ${LOCATION} --format=json)" | |
| echo "done." | |
| # Encode CA Cert in Base64 | |
| CA_CERT_B64="$(echo $CLUSTER | jq -r '.masterAuth.clusterCaCertificate')" | |
| # Get cluster endpoint | |
| GKE_ENDPOINT="$(echo $CLUSTER | jq -r '.endpoint')" | |
| # Get Basic Auth Pass (In GKE, user is hardcoded as "admin" | |
| BASIC_PASS="$(echo $CLUSTER | jq -r '.masterAuth.password')" | |
| echo -n "Writing file: ${CLUSTER_NAME}-kubeconfig..." | |
| cat > "${CLUSTER_NAME}-kubeconfig" <<EOF | |
| apiVersion: v1 | |
| clusters: | |
| - cluster: | |
| certificate-authority-data: ${CA_CERT_B64} | |
| server: https://${GKE_ENDPOINT} | |
| name: gke | |
| contexts: | |
| - context: | |
| cluster: gke | |
| user: gke | |
| name: gke | |
| current-context: gke | |
| kind: Config | |
| preferences: {} | |
| users: | |
| - name: gke | |
| user: | |
| password: "${BASIC_PASS}" | |
| username: "admin" | |
| EOF | |
| echo "done." | |
| echo "Running: KUBECONFIG=${CLUSTER_NAME}-kubeconfig kubectl get clusterroles" | |
| sleep 1 | |
| KUBECONFIG=${CLUSTER_NAME}-kubeconfig kubectl get clusterroles | |
| echo "" | |
| echo "KUBECONFIG=${CLUSTER_NAME}-kubeconfig kubectl ..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment