import json kube_pod_container_resource_requests = json.load( open('kube_pod_container_resource_requests_asset_snapshot.json', 'r')) kube_node_status_allocatable = json.load( open('kube_node_status_allocatable_asset_snapshot.json', 'r')) resource_map = [] node_allocated_resources = {} # parse kube_pod_container_resource_requests data for metric in kube_pod_container_resource_requests['data']['result']: if metric['metric']['resource'] not in ['cpu', 'memory']: continue try: resource = { 'node': metric['metric']['node'], 'namespace': metric['metric']['namespace'], 'pod': metric['metric']['pod'], 'resource': metric['metric']['resource'], 'value': metric['value'][1] } except Exception as e: print(e) continue if "shoot/" in metric['metric']['cluster'] or '/' not in metric['metric']['cluster']: cluster = metric['metric']['cluster'] else: cluster = "{env}/{cluster}".format(env=(metric['metric']['prometheus'].split( '-')[0]).replace('monitoring/', ''), cluster=metric['metric']['cluster']) resource['cluster'] = cluster if metric['metric']['resource'] == 'memory': resource['value'] = float(metric['value'][1]) / 1024 / 1024 / 1024 resource_map.append(resource) if cluster not in node_allocated_resources: node_allocated_resources[cluster] = {} if metric['metric']['node'] not in node_allocated_resources[cluster]: node_allocated_resources[cluster][metric['metric']['node']] = {} if metric['metric']['resource'] not in node_allocated_resources[cluster][metric['metric']['node']]: node_allocated_resources[cluster][metric['metric'] ['node']][metric['metric']['resource']] = 0 if metric['metric']['resource'] == 'memory': node_allocated_resources[cluster][metric['metric']['node'] ][metric['metric']['resource']] += float(metric['value'][1]) / 1024 / 1024 / 1024 else: node_allocated_resources[cluster][metric['metric']['node'] ][metric['metric']['resource']] += float(metric['value'][1]) # find unallocated resources for metric in kube_node_status_allocatable['data']['result']: if metric['metric']['resource'] not in ['cpu', 'memory']: continue node = metric['metric']['node'] resource_type = metric['metric']['resource'] allocatable = float(metric['value'][1]) if resource_type == 'memory': allocatable = allocatable / 1024 / 1024 / 1024 if "shoot/" in metric['metric']['cluster'] or '/' not in metric['metric']['cluster']: cluster = metric['metric']['cluster'] else: cluster = "{env}/{cluster}".format(env=(metric['metric']['prometheus'].split( '-')[0]).replace('monitoring/', ''), cluster=metric['metric']['cluster']) if cluster in node_allocated_resources and node in node_allocated_resources[cluster]: allocated = node_allocated_resources[cluster][node][resource_type] if allocatable > allocated: unallocated = allocatable - allocated # append unallocated resources as a resource named "unallocated" to resource_map resource_map.append({ 'cluster': cluster, 'node': node, 'resource': resource_type, 'namespace': 'unallocated', 'pod': 'unallocated', 'value': unallocated, }) # print(f'Node {node} has {unallocated} {resource_type} unallocated.') # else: # print(f'Node {node} fully allocated for resource {resource_type}.') # else: # print( # f'No allocation data found for node {node} in cluster {cluster}.') json.dump(resource_map, open('nodes_unallocated_resources_analyzed_result.json', 'w'))