#### # detect the leaked gcp disk on dev environments # it will be added into gcloud function later ### import json import subprocess import os import argparse import yaml from kubernetes import client, config import base64 import time import csv from datetime import datetime result = subprocess.run(['gcloud', 'compute', 'disks', 'list','--format', 'json'], stdout=subprocess.PIPE) disksDict = json.loads(result.stdout) gkeClusters = { "prod-seed-asia-northeast1-85509b9": "asia-northeast1", "prod-seed-asia-southeast1-f7a3983": "asia-southeast1", "prod-seed-us-central1-e5cf570": "us-central1", "prod-seed-us-west1-d5ae263": "us-west1", } outfile='disks.csv' csvFile = open(outfile, 'w', newline='') fieldnames = ['id', 'name', 'zone', 'selfLink', "sizeGb", "creationTimestamp", 'lastAttachTimestamp'] writer = csv.DictWriter(csvFile, fieldnames=fieldnames) writer.writeheader() allPVs = [] def process_gke_clusters(): for gke in gkeClusters: os.system("gcloud container clusters get-credentials {} --region {}".format(gke, gkeClusters[gke])) config.kube_config.load_kube_config() apiClient = client.CoreV1Api() pvs = apiClient.list_persistent_volume() for pv in pvs.items: print("process gke {} pv {}".format(gke, pv.metadata.name)) allPVs.append(pv.metadata.name) gardenerConfig = ["gardener-dev", "gardener-staging", "gardener-stability"] def process_all_shoot_clusters(): for gke in gkeClusters: os.system("gcloud container clusters get-credentials {} --region {}".format(gke, gkeClusters[gke])) config.kube_config.load_kube_config() apiClient = client.CoreV1Api() namespaces = apiClient.list_namespace() for ns in namespaces.items: os.system("gcloud container clusters get-credentials {} --region {}".format(gke, gkeClusters[gke])) config.kube_config.load_kube_config() apiClient = client.CoreV1Api() if 'shoot--' not in ns.metadata.name: continue print("process shoot {}".format(ns.metadata.name)) try: shootKubeconfig = apiClient.read_namespaced_secret('kubecfg', ns.metadata.name) shootKubeconfig = shootKubeconfig.data["kubeconfig"] shootKubeconfig= base64.b64decode(shootKubeconfig) shootKubeconfigFile= open(ns.metadata.name , "w") yaml.dump(yaml.load(shootKubeconfig), shootKubeconfigFile) shootKubeconfigFile.close() config.kube_config.load_kube_config(config_file=ns.metadata.name) apiClient = client.CoreV1Api() pvs = apiClient.list_persistent_volume() for pv in pvs.items: print("process shoot {} pv {}".format(ns.metadata.name, pv.metadata.name)) allPVs.append(pv.metadata.name) except: print("bypass shoot {}".format(ns.metadata.name)) process_gke_clusters() process_all_shoot_clusters() for instance in allPVs: print(instance) for disk in disksDict: try: foundIt = False targetPVName = "" if 'description' in disk: description = json.loads(disk['description']) if 'kubernetes.io/created-for/pv/name' in description: targetPVName = description['kubernetes.io/created-for/pv/name'] if targetPVName != "": for pv in allPVs: if pv == targetPVName: foundIt = True if targetPVName != "" and not foundIt: writer.writerow({ 'id': disk['id'], 'name': disk['name'], 'zone': disk['zone'], 'selfLink': disk['selfLink'], "sizeGb": disk["sizeGb"], "creationTimestamp": disk["creationTimestamp"], 'lastAttachTimestamp': disk['lastAttachTimestamp']}) except: pass csvFile.close()