From 03a0c10c640b7b04eaa20bcb7b3ee815b4a3ce31 Mon Sep 17 00:00:00 2001 From: Aleksandr Tcitlionok <803797+terghalin@users.noreply.github.com> Date: Thu, 5 Dec 2024 07:28:53 +0000 Subject: [PATCH] fix(k8s): convert kib to mib --- app/routes/k8s.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/routes/k8s.py b/app/routes/k8s.py index 7435d1c..4024c95 100644 --- a/app/routes/k8s.py +++ b/app/routes/k8s.py @@ -1,6 +1,5 @@ from fastapi import APIRouter from kubernetes import client, config -import math router = APIRouter() @@ -13,7 +12,7 @@ def fetch_k8s_data_with_usage(): nodes = [{ "node_name": node.metadata.name, "cpu": node.status.capacity.get("cpu"), - "memory": node.status.capacity.get("memory"), + "memory": round(convert_memory_to_mib(node.status.capacity.get("memory")), 2), # Convert to MiB "pods_allocatable": node.status.allocatable.get("pods"), } for node in v1.list_node().items] @@ -65,6 +64,10 @@ def convert_cpu_to_cores(cpu): return float(cpu) # Already in cores def convert_memory_to_mib(memory): + """ + Convert memory to MiB (mebibytes). + Handles units: Ki (kibibytes), Mi (mebibytes), Gi (gibibytes). + """ if "Ki" in memory: return int(memory.replace("Ki", "")) / 1024 elif "Mi" in memory: @@ -75,4 +78,8 @@ def convert_memory_to_mib(memory): @router.get("/k8s/data") def get_k8s_data(): + """ + API endpoint to fetch Kubernetes data including nodes, namespaces, + and namespace resource usage. + """ return fetch_k8s_data_with_usage()