import datetime import json import time import requests import os import subprocess def fetch_dev_infra_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 fetch_staging_infra_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_clusters( dev ): clusterResult = {} rawData = None if dev == 'nightly': rawData = fetch_dev_infra_clusters_json() else: rawData = fetch_staging_infra_clusters_json() clusters = json.loads(rawData) for clusters in clusters['items']: name = clusters['metadata']['name'] nameSeps = name.split('-') clusterId = nameSeps[ len(nameSeps) - 1 ] namespace = clusters['metadata']['namespace'] namespaceSeps = namespace.split('-') projectId = namespaceSeps[ len(namespaceSeps) - 1 ] if projectId == '1': continue if dev in namespace: if projectId not in clusterResult: clusterResult[projectId] = [] clusterResult[projectId].append(clusterId) return clusterResult if "__main__" == __name__: nightlyClusterResult = fetch_clusters('nightly') print(nightlyClusterResult) stagingClusterResult = fetch_clusters('staging') print(stagingClusterResult)