Skip to content

Instantly share code, notes, and snippets.

@achetronic
Last active October 17, 2024 23:35
Show Gist options
  • Select an option

  • Save achetronic/1fb4fa285ab7a7cf83eeed322562252b to your computer and use it in GitHub Desktop.

Select an option

Save achetronic/1fb4fa285ab7a7cf83eeed322562252b to your computer and use it in GitHub Desktop.

Revisions

  1. achetronic revised this gist Oct 5, 2023. 1 changed file with 19 additions and 25 deletions.
    44 changes: 19 additions & 25 deletions get-gcloud-gke-contexts.sh
    Original file line number Diff line number Diff line change
    @@ -2,51 +2,45 @@

    LOCAL_EXIT_CODE=0

    # Obtener la lista de proyectos de Google Cloud
    # Get a list of projects from Gcloud
    PROJECTS=($(gcloud projects list --format="value(projectId)"))

    # Loop a través de los proyectos
    # 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}"
    gcloud config set project "${PROJECT}" --verbosity=none

    # Obtener una lista de clústeres en el proyecto
    # Get list of clusters for this project
    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
    # Look the clusters' list. Configure contexts
    for CLUSTER_ZONE in "${CLUSTERS[@]}"; do

    echo "Cluster found. Adopting its context: ${CLUSTER_ZONE}"

    # Separar el nombre del clúster y la zona
    # Split zone and cluster name
    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=$?
    echo "Cluster found. Adopting its context [ cluster: ${CLUSTER}, zone: ${ZONE} ]"

    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=$?
    # 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

    # 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
    # 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')
    "
  2. achetronic created this gist Oct 5, 2023.
    52 changes: 52 additions & 0 deletions get-gcloud-gke-contexts.sh
    Original 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