Commandline tricks: Get your Azure Kubernetes Clusters kubeconfig

One of the subjects I wanted to touch on this blog is my journey in my new job. And with my new job a new cloud adventure started. In my previous job I was primarily focussed on IBM Cloud, AWS and a bit of Google Cloud. Although all clouds are in some way the same, there are also a lot of difficulties, hick-ups and new ideas marching around the corner.

Because of our work for different clients, my second bash script I wrote was a script to get a kubeconfig for one of the Azure Kubernetes Clusters we have to manage:

Kubernetes switching

A simular approach as in Commandline Tricks: Switching between Azure Subscriptions worked for me here as well, adding Kubernetes contexts. I don’t want to do the lookup myself;)

#!/usr/bin/env bash

unset options i
while IFS= read -r -d $'\n' f; do
  options[i++]="$f"
done < <(az aks list -o tsv | cut -f 15,20 )
tLen=${#options[@]}
select opt in "${options[@]}" "Stop script"; do
  echo $opt
  if [ "$REPLY" -le "$tLen" ]; 
  then
    clustername=`echo $opt | cut -f 1 -d " "`
    resourcegroup=`echo $opt | cut -f 2 -d " "`
    echo -e "\n--> Get Credentials: az aks get-credentials -n ${clustername} -g ${resourcegroup}"
    az aks get-credentials -n $clustername -g $resourcegroup
    echo -e "\n--> Contexts available are:"
    kubectl ctx
    break
  elif [ "$REPLY" -eq "$(($tLen+1))" ];
  then
    echo -e "\n--> Script stopped"
    break
  else
    echo -e "\n--> Script stopped"
    echo "Ho ho ho. Something went wrong. ${REPLY} is no good a selector"
    break
  fi
done

For the people who are like me (lazy and not born from the bash shell): add this to your ~/.bashrc or ~/.zshrc after cloning my azure scripts repo:

alias gkc='~/$GIT_REPO_CLONED_HERE/azure-scripts/get_kubeconfig.sh'

To be honest, I use this every day a few times a day. In the future it will probably change a bit due to the level of automation I’m planning to bring into the organisation, but for now it works like a charm.

comments powered by Disqus