Last active
August 1, 2024 20:14
-
-
Save mainda01/261333e9a6f39c5728b1e1a15093ef47 to your computer and use it in GitHub Desktop.
Kubeconfig builder with aks polling
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 | |
| # Use this any time a cluster has been added, rebuilt or removed or if you gain access to | |
| # one you didn't previously have. | |
| # Function to check Azure CLI login status | |
| check_az_login() { | |
| if az account show &>/dev/null; then | |
| echo "Azure CLI is logged in." | |
| else | |
| az login --use-device-code | |
| fi | |
| } | |
| # Main script starts here | |
| check_az_login | |
| mkdir -p /mnt/c/users/$USER/.kube/configs | |
| pushd /mnt/c/users/$USER/.kube/configs >/dev/null | |
| # cleanup after any previous runs | |
| rm -f /mnt/c/users/$USER/.kube/configs/*.cnf /mnt/c/users/$USER/.kube/configs/az.log | |
| #!/usr/bin/bash | |
| # credentials are merged with .kube/config | |
| [ -e /mnt/c/users/$USER/.kube/config ] && mv /mnt/c/users/$USER/.kube/config "/mnt/c/users/$USER/.kube/config.backup.$(date -u +"%y-%m-%dT%H-%M-%S")" | |
| for sub in `az account list --all | jq -r '.[] | select(.name | contains("us-mbs")).name'`; do | |
| echo "azure $sub:" | |
| for cluster in $(az aks list --subscription $sub | jq -r '.[] | .name + "|" + .resourceGroup'); do | |
| read name rg <<<"$(sed 's/|/ /' <<<"$cluster")" | |
| echo " $name" | |
| az aks get-credentials --subscription $sub -n $name -g $rg -f "/mnt/c/users/$USER/.kube/configs/$name.cnf" >>/mnt/c/users/$USER/.kube/configs/az.log 2>&1 | |
| done | |
| done | |
| KUBECONFIG=$(find /mnt/c/users/$USER/.kube/configs -name "*.cnf" -exec realpath {} \; | sed 's/\\/\\\\/g' | xargs | sed 's/ /:/g') kubectl config view --flatten >/mnt/c/users/$USER/.kube/config | |
| KUBECONFIG=/mnt/c/users/$USER/.kube/config | |
| # # Use kubelogon to fix the new file | |
| echo "Using kubelogin to convert config for azcli" | |
| [ -n "$(which kubelogin)" ] && kubelogin convert-kubeconfig -l azurecli | |
| # # copy the new config out to windoze if the WINHOME var exists | |
| echo "Migrating .kube/config to Onedrive" | |
| [ "/mnt/c/users/$USER/.kube/config" != "$(realpath ~/.kube/config)" ] && cp -f /mnt/c/users/$USER/.kube/config ~/.kube/ | |
| popd >/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment