NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

# Python version of the script
import os
import subprocess
import time

# Output file
gke_output_file = "gkeout.txt"

# Start time
start_time = time.time()
start_time_readable = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time))

with open(gke_output_file, "w") as file:
file.write(f"Script started at: {start_time_readable}n")
file.write("GCP Project ID | Cluster Name | Kubernetes Version | Cluster IP Addressn")
file.write("-------------------------------------------------------------n")

# Get a list of all GCP projects
projects = subprocess.check_output("gcloud projects list --format="value(projectId)"", shell=True).decode().splitlines()

if not projects:
with open(gke_output_file, "a") as file:
file.write("No GCP projects found. Exiting.n")
exit()

project_count = 0

# Traverse through each project
for project_id in projects:
project_count += 1
print(f"Inspecting project {project_count}: {project_id}")

# Set the project in gcloud
result = subprocess.run(f"gcloud config set project {project_id}", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if result.returncode != 0:
with open(gke_output_file, "a") as file:
file.write(f"{project_count} | {project_id} | Failed to set project. Skipping.n")
continue

# Get the list of GKE clusters and their details
clusters = subprocess.check_output(
"gcloud container clusters list --format="value(name,currentMasterVersion,endpoint)"",
shell=True
).decode().splitlines()

if not clusters:
with open(gke_output_file, "a") as file:
file.write(f"{project_count} | {project_id} | No GKE clusters found. Skipping.n")
continue

# Append cluster details to the output file
with open(gke_output_file, "a") as file:
for cluster in clusters:
parts = cluster.split()
cluster_name = parts[0]
cluster_version = parts[1]
cluster_ip = parts[2]
file.write(f"{project_count} | {project_id} | {cluster_name} | {cluster_version} | {cluster_ip}n")

# End time
end_time = time.time()
end_time_readable = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))
time_taken_minutes = int((end_time - start_time) / 60)

# Analyze and summarize
version_summary = {}
region_summary = {}
total_clusters = 0

with open(gke_output_file, "r") as file:
lines = file.readlines()[3:]
for line in lines:
parts = line.split('|')
if len(parts) < 4:
continue

cluster_version = parts[3].strip()
cluster_name = parts[2].strip()

if cluster_version:
version_summary[cluster_version] = version_summary.get(cluster_version, 0) + 1
total_clusters += 1

region = "us-" in cluster_name
if region:
region_summary[region] = region_summary.get(region, 0) + 1

# Write summary to output file
with open(gke_output_file, "a") as file:
file.write(f"nClusters by Kubernetes Version:n")
for version, count in version_summary.items():
file.write(f"{count} clusters running Kubernetes version {version}n")

file.write(f"nClusters by Region:n")
for region, count in region_summary.items():
file.write(f"{count} clusters in region {region}n")

file.write(f"nSummary:n")
file.write(f"Total projects inspected: {project_count}n")
file.write(f"Total clusters found: {total_clusters}n")
file.write(f"Script runtime: {time_taken_minutes} minutesn")

print(f"Script finished. Output saved to {gke_output_file}")
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.