Created
April 24, 2020 00:49
-
-
Save andymotta/ec1e9e62172185a7e00e417d98f36473 to your computer and use it in GitHub Desktop.
Log into all Kubernetes Clusters across all your GCP Projects
This file contains 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 characters
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