# usage: KUBECONFIG=lab/kubeconfig python advisor/cleanup_networks/cleanup_networks.py import json import copy import boto3 import sys from kubernetes.client import api_client from kubernetes import config, dynamic from requests.packages import urllib3 from kubernetes.dynamic.exceptions import NotFoundError urllib3.disable_warnings() DELETE_BY_API = False cleanup_template = '''tenant: %s, project: %s kubectl get network,cluster,backup,networkpeer -n %s kubectl delete network %s -n %s --wait=0''' cleanup_shoot_template = '''kubectl annotate shoot -n %s %s confirmation.gardener.cloud/deletion=true kubectl delete shoot %s -n %s --wait=0 kubectl annotate shoot -n %s %s gardener.cloud/operation=reconcile ''' def delete_network_by_api(network): # apply delete request cli = dynamic.DynamicClient(api_client.ApiClient()) network_api = cli.resources.get( api_version='cloud.pingcap.com/v1', kind='Network') try: response = network_api.delete( name=network['network_name'], namespace=network['namespace']) print('ok, removing shoot_name: %s, namespace: %s' % (network['shoot_name'], network['shoot_namespace'])) print(response['status']['phase']) # check dependencies for resource in resources: if resource['product'] not in ['clb', 'nlb']: continue if network['shoot_name'] == resource['shoot_id'] and resource['component'] not in ['kube-apiserver', 'vpn-shoot']: if resource['product'] == 'clb': clbclient = boto3.client( 'elb', region_name='us-west-2') response = clbclient.delete_load_balancer( LoadBalancerName=resource['name'] ) print(response) if resource['product'] == 'nlb': nlbclient = boto3.client( 'elbv2', region_name='us-west-2') response = nlbclient.delete_load_balancer( LoadBalancerArn=resource['billing_key'] ) print(response) except NotFoundError as e: # print('not found') pass except Exception as e: print(response) if __name__ == '__main__': for arg in sys.argv: if arg == '--delete-by-api': DELETE_BY_API = True empty_networks = json.load(open( 'infra_api_empty_network_asset_snapshot_analysis_result.json', 'r')) resources = json.load(open('aws_asset_snapshot_analysis_result.json', 'r')) # cleanup network for network in empty_networks: # QA test infra https://pingcap.feishu.cn/wiki/wikcnwsv4vC0EUAVPSBqixMygIb if network['shoot_name'] in ['5ad62c29', '2c8a7d9a', 'a7c2b4dd']: continue if network['network_name'] == '': continue if DELETE_BY_API: if network['env'] in ['dev', 'staging', 'prod']: config.load_kube_config(context='%s-infra' % network['env']) delete_network_by_api(network) else: print(cleanup_template % (network['tenant'], network['project'], network['namespace'], network['network_name'], network['namespace'])) # check dependencies for resource in resources: if resource['product'] not in ['clb', 'nlb']: continue if network['shoot_name'] == resource['shoot_id'] and resource['component'] not in ['kube-apiserver', 'vpn-shoot']: print("exist clb, clb, component: %s" % resource['component']) if resource['product'] == 'clb': print( "aws elb delete-load-balancer --load-balancer-name %s" % resource['name']) if resource['product'] == 'nlb': print( "aws elbv2 delete-load-balancer --load-balancer-arn %s" % resource['billing_key']) print() # cleanup empty gardener for network in empty_networks: if network['network_name'] != '': continue print("shoot network not found, shoot_name: %s, namespace: %s" % (network['shoot_name'], network['shoot_namespace'])) print(cleanup_shoot_template % (network['shoot_namespace'], network['shoot_name'], network['shoot_name'], network['shoot_namespace'], network['shoot_namespace'], network['shoot_name']))