Skip to content

Instantly share code, notes, and snippets.

@sublimino
Created August 25, 2020 08:17
Show Gist options
  • Save sublimino/8ae39b9b6ae706438c4b8f2061a0a609 to your computer and use it in GitHub Desktop.
Save sublimino/8ae39b9b6ae706438c4b8f2061a0a609 to your computer and use it in GitHub Desktop.
Useful gcloud aliases

whoami

gcloud-whoami () 
{ 
    gcloud auth list --filter=status:ACTIVE --format="value(account)"
}

what account/project/zone etc

gcloud-get-user () { 
    gcloud config get-value account
}
gcloud-get-project () { 
    gcloud config get-value project
}
gcloud-get-project-id () { 
    gcloud projects describe "$(gcloud-get-project)" --format="value(projectNumber)"
}
gcloud-get-region () { 
    gcloud config get-value compute/region
}
gcloud-get-zone () { 
    gcloud config get-value compute/zone
}

gcloud config dump

gcloud-config () { 
    gcloud config list "${@}"
}

org policy dump

gcloud-get-org-policies () { 
    gcloud alpha resource-manager org-policies list --project $(gcloud-get-project)
}

gclb backend status wrangling (probably a bit much, sorry!)

gcloud-gclb-backend-status () { 
    for SERVICE in $(gcloud compute backend-services list | awk '$3 == "HTTP" { print $1}');
    do
        echo "${SERVICE}";
        PORT=$(echo "${SERVICE}" | sed -E 's/k8s-be-(.*)--[^-]*$/\1/g');
        kservice "${PORT}" | grep --color=auto ":${PORT}/" --color=none;
        gcloud compute backend-services get-health "${SERVICE}" --global | grep --color=auto --color=auto -E '^  - healthState:' | h '\bUNHEALTHY\b' '\bHEALTHY\b';
        printf '\n';
    done
}

quota checks

gcloud-quota-check () { 
    for REGION in ${1:-$(gcloud compute regions list --format="value(name)")};
    do
        echo $REGION;
        gcloud compute regions describe "${REGION}" --format json | jq '.quotas[] | select(.limit > 0) | select(.usage >= (.limit * 0.7)) | .';
    done
}

sa perm checks

gcloud-sa () { 
    gcloud alpha iam service-accounts "${@:-}"
}
gcloud-sa-list () { 
    gcloud-sa list ${@:-}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment