mirror of
https://github.com/Rainyy21/k3s-health-dashboard.git
synced 2026-10-10 23:00:19 -04:00
32 lines
879 B
Python
32 lines
879 B
Python
from fastapi import APIRouter
|
|
from app.k8s_client import get_core_client
|
|
|
|
router = APIRouter(prefix="/nodes", tags=["nodes"])
|
|
|
|
|
|
@router.get("/")
|
|
def list_node():
|
|
v1 = get_core_client()
|
|
nodes = v1.list_node().items
|
|
result = []
|
|
for n in nodes:
|
|
conditions = {c.type: c.status for c in n.status.conditions}
|
|
result.append(
|
|
{
|
|
"name": n.metadata.name,
|
|
"ready": conditions.get("Ready") == "True",
|
|
"cpu_capacity": n.status.capacity.get("cpu"),
|
|
"memory_capacity": n.status.capacity.get("memory"),
|
|
"labels": n.metadata.labels,
|
|
"age_days": _age(n.metadata.creation_timestamp),
|
|
}
|
|
)
|
|
return result
|
|
|
|
|
|
def _age(ts) -> int:
|
|
from datetime import datetime, timezone
|
|
|
|
now = datetime.now(timezone.utc)
|
|
return (now - ts).days
|