Skip to content

Instantly share code, notes, and snippets.

@ImIOImI
Created February 29, 2024 13:46
Show Gist options
  • Save ImIOImI/d66cde3188c872f35320bedace88dbec to your computer and use it in GitHub Desktop.
Save ImIOImI/d66cde3188c872f35320bedace88dbec to your computer and use it in GitHub Desktop.
Login to all clusters that you have access to in an Azure tenant
#!/usr/bin/env bash
# get the environment to deploy to
read -p "Should we login as admin? (y/n) [y]: " ADMIN
ADMIN=${ADMIN:-y}
if [ "$ADMIN" == "y" ]; then
ADMIN_STRING="--admin"
else
ADMIN_STRING=""
fi
if [ -z "$KUBE_CONFIG_PATH" ]; then
echo "KUBE_CONFIG_PATH is not set. Please set it to the path of the kubeconfig file you want to use."
echo "Default is ~/.kube/config"
read -p "Enter path to kubeconfig file or press enter to skip and set it to the default above: " KUBE_CONFIG_PATH
KUBE_CONFIG_PATH=${KUBE_CONFIG_PATH:-~/.kube/config}
export KUBE_CONFIG_PATH
echo "Please consider adding the following to your .bashrc or .zshrc file:"
echo "export KUBE_CONFIG_PATH=$KUBE_CONFIG_PATH"
fi
if [ -z "$KUBECONFIG" ]; then
echo "KUBECONFIG is not set. Please set it to the path of the kubeconfig file you want to use."
echo "Default is ~/.kube/config"
read -p "Enter path to kubeconfig file or press enter to skip and set it to the default above: " KUBECONFIG
KUBECONFIG=${KUBECONFIG:-~/.kube/config}
export KUBECONFIG
echo "Please consider adding the following to your .bashrc or .zshrc file:"
echo "export KUBECONFIG=$KUBECONFIG"
fi
echo "Current KUBECONFIG path: $KUBECONFIG (this is what kubectl uses)"
echo "Current KUBE_CONFIG_PATH path: $KUBE_CONFIG_PATH (this is what terraform uses)"
CLUSTERS=$(az aks list --query "[].{name:name, resourceGroup:resourceGroup}" -o json)
echo $CLUSTERS
arr=$(echo $CLUSTERS | jq -c '.[]')
for d in $arr; do
echo $d
NAME=$(echo $d | jq -r '.name')
RESOURCE_GROUP=$(echo $d | jq -r '.resourceGroup')
echo "merging credentials for $NAME into $KUBE_CONFIG_PATH"
az aks get-credentials --name $NAME --resource-group $RESOURCE_GROUP $ADMIN_STRING --file $KUBE_CONFIG_PATH
if [ $KUBE_CONFIG_PATH != $KUBECONFIG ]; then
echo "merging credentials for $NAME into $KUBECONFIG"
az aks get-credentials --name $NAME --resource-group $RESOURCE_GROUP $ADMIN_STRING --file $KUBECONFIG
fi
echo "-----------------"
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment