Last active
October 17, 2024 23:35
-
-
Save achetronic/1fb4fa285ab7a7cf83eeed322562252b to your computer and use it in GitHub Desktop.
Revisions
-
achetronic revised this gist
Oct 5, 2023 . 1 changed file with 19 additions and 25 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,51 +2,45 @@ LOCAL_EXIT_CODE=0 # Get a list of projects from Gcloud PROJECTS=($(gcloud projects list --format="value(projectId)")) # Create a directory to split clusters kubeconfig files later mkdir -p "${HOME}/.kube/clusters" # Loop the projects for PROJECT in "${PROJECTS[@]}"; do echo "Selecting project: ${PROJECT}" gcloud config set project "${PROJECT}" --verbosity=none # Get list of clusters for this project CLUSTERS=($(gcloud container clusters list --project="$PROJECT" --format="csv[separator='@',no-heading](name,zone)")) # Look the clusters' list. Configure contexts for CLUSTER_ZONE in "${CLUSTERS[@]}"; do # Split zone and cluster name CLUSTER=$(echo "${CLUSTER_ZONE}" | cut -d'@' -f1) ZONE=$(echo "${CLUSTER_ZONE}" | cut -d'@' -f2) echo "Cluster found. Adopting its context [ cluster: ${CLUSTER}, zone: ${ZONE} ]" # Configure the context for current cluster export KUBECONFIG="${HOME}/.kube/clusters/${PROJECT}__${ZONE}__${CLUSTER}" gcloud container clusters get-credentials "$CLUSTER" --project="$PROJECT" --zone="$ZONE" || LOCAL_EXIT_CODE=$? #--user-output-enabled --verbosity=debug || LOCAL_EXIT_CODE=$? if [[ "${LOCAL_EXIT_CODE}" -ne 0 ]]; then echo "[···] Context not retrieved. Skipping operation for it."; continue fi done done # Throw an advise printf "Hey, put this on your .bashrc or .zshrc file: \n\n%s" " # [CUSTOM] Show kubectl where to find our kubeconfig files export KUBECONFIG=\$(find ~/.kube/clusters -type f | sed ':a;N;s/\n/:/;ba') " -
achetronic created this gist
Oct 5, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ #!/bin/bash LOCAL_EXIT_CODE=0 # Obtener la lista de proyectos de Google Cloud PROJECTS=($(gcloud projects list --format="value(projectId)")) # Loop a través de los proyectos for PROJECT in "${PROJECTS[@]}"; do echo "Selecting project: ${PROJECT}" gcloud config set project "${PROJECT}" # Obtener una lista de clústeres en el proyecto CLUSTERS=($(gcloud container clusters list --project="$PROJECT" --format="csv[separator='@',no-heading](name,zone)")) # Loop a través de los clústeres y configurar los contextos for CLUSTER_ZONE in "${CLUSTERS[@]}"; do echo "Cluster found. Adopting its context: ${CLUSTER_ZONE}" # Separar el nombre del clúster y la zona CLUSTER=$(echo "${CLUSTER_ZONE}" | cut -d'@' -f1) ZONE=$(echo "${CLUSTER_ZONE}" | cut -d'@' -f2) echo "CLUSTER: ${CLUSTER} - ZONE: ${ZONE}" # Configurar el contexto para el clúster actual #gcloud container clusters get-credentials "$CLUSTER" --project="$PROJECT" --zone="$ZONE" || LOCAL_EXIT_CODE=$? KUBECONFIG="${HOME}/.kube/clusters/$PROJECT__$ZONE__$CLUSTER" \ gcloud container clusters get-credentials "$CLUSTER" --project="$PROJECT" --zone="$ZONE" \ --user-output-enabled --verbosity=debug || LOCAL_EXIT_CODE=$? if [[ "${LOCAL_EXIT_CODE}" -ne 0 ]]; then echo "[···] Context not retrieved. Skipping operation for it."; continue fi # Obtener el nombre del contexto generado automáticamente por gcloud #CONTEXT=$(kubectl config current-context) # Renombrar el contexto para que sea más descriptivo (opcional) #kubectl config rename-context "$CONTEXT" "$PROJECT##$CLUSTER##$ZONE" echo "Contexto creado para el clúster $CLUSTER en la zona $ZONE del proyecto $PROJECT: $PROJECT/$CLUSTER" done done # Listar los contextos configurados #kubectl config get-contexts