fix(k8s): convert kib to mib
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user