import json import time import requests import subprocess pendingDeleteClusters = [] def fetch_staging_clusters_json(): cmd = "KUBECONFIG=staging-infra-apiserver /usr/local/bin/kubectl get clusters -A -o json" inputDomain = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) jsonS = json.dumps(inputDomain.communicate()[0].decode('utf-8')) return json.loads(jsonS) def fetch_dev_clusters_json(): cmd = "KUBECONFIG=dev-infra-apiserver /usr/local/bin/kubectl get clusters -A -o json" inputDomain = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) jsonS = json.dumps(inputDomain.communicate()[0].decode('utf-8')) return json.loads(jsonS) def process_infra_clusters(): rawData = fetch_dev_clusters_json() clusters = json.loads(rawData) for cluster in clusters['items']: if 'status' not in cluster: continue if 'phase' not in cluster['status']: continue status = cluster['status']['phase'] name = cluster['metadata']['name'] namespace = cluster['metadata']['namespace'] if status == 'PendingDelete': pendingDeleteClusters.append( { 'name': name, "namespace": namespace } ) rawData = fetch_staging_clusters_json() clusters = json.loads(rawData) for cluster in clusters['items']: if 'status' not in cluster: continue if 'phase' not in cluster['status']: continue status = cluster['status']['phase'] name = cluster['metadata']['name'] namespace = cluster['metadata']['namespace'] if status == 'PendingDelete': pendingDeleteClusters.append( { 'name': name, "cluster_id": namespace } ) return pendingDeleteClusters