diff --git a/app/routes/k8s.py b/app/routes/k8s.py index 49449d9..b68b9c4 100644 --- a/app/routes/k8s.py +++ b/app/routes/k8s.py @@ -1,33 +1,60 @@ from fastapi import APIRouter from kubernetes import client, config +import math router = APIRouter() -# Initialize Kubernetes client -def init_k8s_client(): - try: - config.load_incluster_config() - except: - config.load_kube_config() # local testing +def fetch_k8s_data_with_usage(): + config.load_incluster_config() + v1 = client.CoreV1Api() + metrics_client = client.CustomObjectsApi() + + # Fetch nodes and namespaces + nodes = [{ + "node_name": node.metadata.name, + "cpu": node.status.capacity.get("cpu"), + "memory": node.status.capacity.get("memory"), + "pods_allocatable": node.status.allocatable.get("pods"), + } for node in v1.list_node().items] + + 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_namespaced_custom_object( + group="metrics.k8s.io", version="v1beta1", namespace="", 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 millicores and memory to MiB + namespace_usage[pod_namespace]["cpu"] += convert_cpu_to_millicores(cpu_usage) + namespace_usage[pod_namespace]["memory"] += convert_memory_to_mib(memory_usage) + + return {"nodes": nodes, "namespaces": namespaces, "namespace_usage": namespace_usage} + +def convert_cpu_to_millicores(cpu): + if "n" in cpu: + return int(cpu.replace("n", "")) / 1e6 + elif "m" in cpu: + return int(cpu.replace("m", "")) + return int(cpu) * 1000 + +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 int(memory) @router.get("/k8s/data") def get_k8s_data(): - init_k8s_client() - v1 = client.CoreV1Api() - - # Fetch nodes - nodes = v1.list_node() - node_data = [] - for node in nodes.items: - node_data.append({ - "node_name": node.metadata.name, - "cpu": node.status.capacity.get("cpu"), - "memory": node.status.capacity.get("memory"), - "pods_allocatable": node.status.allocatable.get("pods"), - }) - - # Fetch namespaces - namespaces = v1.list_namespace() - namespace_data = [ns.metadata.name for ns in namespaces.items] - - return {"nodes": node_data, "namespaces": namespace_data} + return fetch_k8s_data_with_usage()