update(k8s): calculate namespace usage

This commit is contained in:
Aleksandr Tcitlionok
2024-12-05 06:51:57 +00:00
parent b9e4fdbf7a
commit fb4b63270b

View File

@@ -1,33 +1,60 @@
from fastapi import APIRouter from fastapi import APIRouter
from kubernetes import client, config from kubernetes import client, config
import math
router = APIRouter() router = APIRouter()
# Initialize Kubernetes client def fetch_k8s_data_with_usage():
def init_k8s_client():
try:
config.load_incluster_config() config.load_incluster_config()
except:
config.load_kube_config() # local testing
@router.get("/k8s/data")
def get_k8s_data():
init_k8s_client()
v1 = client.CoreV1Api() v1 = client.CoreV1Api()
metrics_client = client.CustomObjectsApi()
# Fetch nodes # Fetch nodes and namespaces
nodes = v1.list_node() nodes = [{
node_data = []
for node in nodes.items:
node_data.append({
"node_name": node.metadata.name, "node_name": node.metadata.name,
"cpu": node.status.capacity.get("cpu"), "cpu": node.status.capacity.get("cpu"),
"memory": node.status.capacity.get("memory"), "memory": node.status.capacity.get("memory"),
"pods_allocatable": node.status.allocatable.get("pods"), "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]
namespaces = v1.list_namespace()
namespace_data = [ns.metadata.name for ns in namespaces.items]
return {"nodes": node_data, "namespaces": namespace_data} # 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():
return fetch_k8s_data_with_usage()