from fastapi import APIRouter from kubernetes import client, config router = APIRouter() def fetch_k8s_data_with_usage(): config.load_incluster_config() v1 = client.CoreV1Api() metrics_client = client.CustomObjectsApi() # Fetch nodes nodes = [] for node in v1.list_node().items: # Extract storage (ephemeral-storage) and instance type ephemeral_storage = node.status.capacity.get("ephemeral-storage", "0") instance_type = node.metadata.labels.get("beta.kubernetes.io/instance-type", "N/A") nodes.append({ "node_name": node.metadata.name, "cpu": node.status.capacity.get("cpu"), "memory": round(convert_memory_to_mib(node.status.capacity.get("memory")), 2), # Convert to MiB "storage": f"{round(convert_memory_to_gb(ephemeral_storage), 2)} GB", "instance_type": instance_type, "pods_allocatable": node.status.allocatable.get("pods"), }) # Fetch namespaces namespaces = [ns.metadata.name for ns in v1.list_namespace().items] # Fetch pod metrics and calculate namespace resource usage namespace_usage = {} pod_metrics = metrics_client.list_cluster_custom_object( group="metrics.k8s.io", version="v1beta1", plural="pods" ) for pod in pod_metrics["items"]: pod_namespace = pod["metadata"]["namespace"] if pod_namespace not in namespace_usage: namespace_usage[pod_namespace] = {"cpu": 0, "memory": 0} for container in pod["containers"]: cpu_usage = container["usage"]["cpu"] memory_usage = container["usage"]["memory"] # Convert CPU to cores and memory to MiB namespace_usage[pod_namespace]["cpu"] += convert_cpu_to_cores(cpu_usage) namespace_usage[pod_namespace]["memory"] += convert_memory_to_mib(memory_usage) # Round and format usage for readability namespace_usage = { ns: { "cpu": round(usage["cpu"], 4), # Round to 4 decimal places "memory": round(usage["memory"], 2), # Memory in MiB } for ns, usage in namespace_usage.items() } return {"nodes": nodes, "namespaces": namespaces, "namespace_usage": namespace_usage} def convert_memory_to_gb(memory): """ Convert memory to GB (gigabytes) for ephemeral-storage. """ if "Ki" in memory: return int(memory.replace("Ki", "")) / (1024 ** 2) elif "Mi" in memory: return int(memory.replace("Mi", "")) / 1024 elif "Gi" in memory: return int(memory.replace("Gi", "")) return float(memory) def convert_cpu_to_cores(cpu): if "n" in cpu: return round(int(cpu.replace("n", "")) / 1e9, 4) elif "u" in cpu: return round(int(cpu.replace("u", "")) / 1e6, 4) elif "m" in cpu: return round(int(cpu.replace("m", "")) / 1000, 4) return float(cpu) def convert_memory_to_mib(memory): if "Ki" in memory: return int(memory.replace("Ki", "")) / 1024 elif "Mi" in memory: return int(memory.replace("Mi", "")) elif "Gi" in memory: return int(memory.replace("Gi", "")) * 1024 return float(memory) @router.get("/k8s/data") def get_k8s_data(): return fetch_k8s_data_with_usage()