Skip to content

Instantly share code, notes, and snippets.

@andymotta
Created April 24, 2020 00:49
Show Gist options
  • Save andymotta/ec1e9e62172185a7e00e417d98f36473 to your computer and use it in GitHub Desktop.
Save andymotta/ec1e9e62172185a7e00e417d98f36473 to your computer and use it in GitHub Desktop.
Log into all Kubernetes Clusters across all your GCP Projects
import json
import os
def projects_list():
projList = []
data = json.loads(os.popen('gcloud projects list --format json').read())
for item in data:
if item['lifecycleState'] == "ACTIVE":
projList.append(item['projectId'])
return projList
def main():
for p in projects_list():
clusters = json.loads(os.popen('gcloud container clusters list --project {} --format json'.format(p)).read())
for cluster in clusters:
if cluster['status'] == "RUNNING":
print("Creating kubernetes context for %s location %s in project %s" % (cluster['name'], cluster['zone'], p))
os.popen('gcloud container clusters get-credentials {} --zone {} --project {} --format json'.format(cluster['name'],cluster['zone'],p)).read()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment