import os import sys import boto3 import yaml import base64 from kubernetes import client, config from kubernetes.client.rest import ApiException as K8sApiException def process_cluster(shootName): namespaces = apiClient.list_namespace() for ns in namespaces.items: services = apiClient.list_namespaced_service(ns.metadata.name) for svc in services.items: svcName = ns.metadata.name + '/' + svc.metadata.name if svc.spec.type != "LoadBalancer": print("skip service: ", svcName) continue key = shootName + '--' + svcName print("process elb: ", key) if key in lbWithFullName: print("find elb: ", key, lbWithFullName[key]) lbExisted.append(lbWithFullName[key]) regions = ['us-west-2', 'us-east-1', 'ap-northeast-1', 'ap-southeast-1', 'ap-northeast-2'] for region in regions: print('------------------------------------------------------------------------------') print("precess region", region) ec2client = boto3.client('elb', region_name=region) response = ec2client.describe_load_balancers() loadbalancer = [] for lb in response["LoadBalancerDescriptions"]: loadbalancer.append(lb['LoadBalancerName']) print("leaked lb:", len(loadbalancer)) print("leaked lb:", loadbalancer) lbWithShoot = {} lbWithService = {} lbWithFullName = {} notShootElb = {} noTagsELb = [] for lb in loadbalancer: tags = ec2client.describe_tags( LoadBalancerNames= [lb], ) tags = tags['TagDescriptions'] if len(tags) == 0: noTagsELb.append(lb) continue isShoot = True for tag in tags[0]['Tags']: if "kubernetes.io/cluster" in tag['Key']: if 'shoot--' not in tag['Key']: isShoot = False value = tag['Key'].replace('kubernetes.io/cluster/', '') notShootElb[value] = lb break if not isShoot: noTagsELb.append(lb) continue svcName = '' shootName = '' for tag in tags[0]['Tags']: if tag['Key'] == "kubernetes.io/service-name": svcName = tag['Value'] if "kubernetes.io/cluster/" in tag['Key']: value = tag['Key'].replace('kubernetes.io/cluster/', '') shootName = value if "KubernetesCluster" in tag['Key']: shootName = tag['Value'] lbWithFullName[shootName + '--' + svcName] = lb print(lbWithFullName) print(len(lbWithFullName)) print("-----------------------------------------------------------------------------") print('not shoot loadbalancer', len(noTagsELb), noTagsELb) print("-----------------------------------------------------------------------------") print(lbWithService) lbExisted = [] lbNotExisted = [] kubeconfig = ["/home/gardener-dev", "/home/gardener-staging", "/home/gardener-stability"] for kc in kubeconfig: config.kube_config.load_kube_config(config_file=kc) apiClient = client.CoreV1Api() namespaces = apiClient.list_namespace() shootClient = client.CustomObjectsApi() for ns in namespaces.items: config.kube_config.load_kube_config(config_file=kc) apiClient = client.CoreV1Api() namespaces = apiClient.list_namespace() shootClient = client.CustomObjectsApi() shoots = shootClient.list_namespaced_custom_object( group="core.gardener.cloud", version="v1beta1", namespace=ns.metadata.name, plural="shoots", ) ns = ns.metadata.name.replace("garden-","") shootName = '' for shoot in shoots['items']: cloudType = shoot['spec']['cloudProfileName'] shootRegion = shoot['spec']['region'] name = shoot["metadata"]["name"] print("process shoot: ", name) if cloudType == "aws": shootName = "shoot--" + ns + "--" + name else: continue if shootRegion != region: continue config.kube_config.load_kube_config(config_file=kc) apiClient = client.CoreV1Api() shootClient = client.CustomObjectsApi() try: shootKubeconfigSecretName = name + ".kubeconfig" print("fetch shoot kubeconfig: ", shootName, ns, shootKubeconfigSecretName) shootKubeconfig = apiClient.read_namespaced_secret(shootKubeconfigSecretName, "garden-" + ns) shootKubeconfig = shootKubeconfig.data["kubeconfig"] shootKubeconfig= base64.b64decode(shootKubeconfig) shootKubeconfigFile= open(shootName , "w") yaml.dump(yaml.load(shootKubeconfig), shootKubeconfigFile) shootKubeconfigFile.close() config.kube_config.load_kube_config(config_file=shootName) apiClient = client.CoreV1Api() process_cluster(shootName) except Exception as ex: print("shoot ", shootName, 'is Unheathy') for lb in lbWithFullName.keys(): if shootName in lb: lbNotExisted.append(lbWithFullName[lb]) for lb in lbWithFullName.values(): if lb not in lbExisted: lbNotExisted.append(lb) lbNotExisted = list(set(lbNotExisted)) print("region:", region, lbNotExisted) print("region:", region, len(lbNotExisted))