diff --git a/app/routes/export.py b/app/routes/export.py index ec42865..9bfd56f 100644 --- a/app/routes/export.py +++ b/app/routes/export.py @@ -1,14 +1,36 @@ from fastapi import APIRouter, Response from database import fetch_all +from kubernetes import client, config import yaml import json -from kubernetes import client, config +import logging router = APIRouter() -def fetch_k8s_data(): +# Helper functions for conversions +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 float(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 float(memory) + +# Fetch Kubernetes data with namespace resource usage +def fetch_k8s_data_with_usage(): config.load_incluster_config() v1 = client.CoreV1Api() + metrics_client = client.CustomObjectsApi() + + # Fetch nodes nodes = [{ "node_name": node.metadata.name, "cpu": node.status.capacity.get("cpu"), @@ -16,9 +38,39 @@ def fetch_k8s_data(): "pods_allocatable": node.status.allocatable.get("pods"), } for node in v1.list_node().items] + # Fetch namespaces namespaces = [ns.metadata.name for ns in v1.list_namespace().items] - return {"nodes": nodes, "namespaces": namespaces} + # 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 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) + + # Convert usage to serializable types + namespace_usage = { + ns: { + "cpu": round(usage["cpu"], 2), # Round to 2 decimal places for readability + "memory": round(usage["memory"], 2), + } + for ns, usage in namespace_usage.items() + } + + return {"nodes": nodes, "namespaces": namespaces, "namespace_usage": namespace_usage} + +# Export endpoint @router.get("/export") def export_data(format: str = "yaml"): data = { @@ -27,6 +79,8 @@ def export_data(format: str = "yaml"): "kubernetes": fetch_k8s_data_with_usage(), } + logging.info(f"Exporting data: {data}") + if format.lower() == "yaml": yaml_data = yaml.safe_dump(data, sort_keys=False) return Response(content=yaml_data, media_type="text/yaml")