Skip to content

Instantly share code, notes, and snippets.

@malambra
Last active October 7, 2022 15:19
Show Gist options
  • Save malambra/5005c4709637fe92968f21fa8bed3c1c to your computer and use it in GitHub Desktop.
Save malambra/5005c4709637fe92968f21fa8bed3c1c to your computer and use it in GitHub Desktop.
Script para regenerar kubeconfig con todas los aks de nuestros resource_groups
#!/usr/bin/env bash
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on undeclared variable
set -o pipefail # don't hide errors within pipes
# set -o xtrace # track what is running - debugging
# Set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # <-- get path of the script
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" # <-- get full path name of the script
__base="$(basename ${__file} .sh)" # <-- get name of the script
__root="$(cd "$(dirname "${__dir}")" && pwd)" # <-- get root path of the script
# Set noname posibility to variable $1 :- like :-noname
arg1="${1:-}"
# Set variables
ahora=$(date '+%Y%m%d%H%M%S')
backupdir="${HOME}/back_kubeconfig"
backupfile="${backupdir}/${ahora}_config"
kubeconfig="${HOME}/.kube/config"
# Create backup directory
if [ ! -d "${backupdir}" ]; then
echo "Creando directorio de backups: ${backupdir}"
mkdir ${backupdir}
fi
# Copy kubeconfig to backup path
if [ -f "${kubeconfig}" ]; then
echo "Moviendo Backup a: ${backupfile}"
mv ${kubeconfig} ${backupfile}
fi
# IFS define separation character with new line
# aks list obtain list of aks envoirements. -n +3 without summary
# create commands to add new context to kubeconfig. Use columns 3 and 1
# execute each command with eval
IFS=$'\n'
for i in $(az aks list --output table|tail -n +3); do
command=$(echo $i|awk '{print "az aks get-credentials --resource-group " $3 " --name " $1}')
eval ${command}
done
# give possibility to change context
context=$(kubectl config current-context)
echo "Quiere cambiar de contexto actual: ${context}"
read -p 'New Context: ' context
if [ "${context}" != "" ]; then
kubectl config use-context ${context}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment