mirror of
https://github.com/Rainyy21/k3s-health-dashboard.git
synced 2026-10-11 02:40:19 -04:00
24 lines
720 B
Python
24 lines
720 B
Python
from fastapi import APIRouter
|
|
from k8s_client import get_custom_client
|
|
|
|
router = APIRouter(prefix="/metrics", tags=["metrics"])
|
|
|
|
|
|
@router.get("/nodes")
|
|
def node_metrics():
|
|
api = get_custom_client()
|
|
try:
|
|
data = api.list_cluster_custom_object(
|
|
group="metrics.k8s.io", version="v1beta1", plural="nodes"
|
|
)
|
|
return [
|
|
{
|
|
"name": item["metadata"]["name"],
|
|
"cpu_usage": item["usage"]["cpu"], # e.g. "250m"
|
|
"memory_usage": item["usage"]["memory"], # e.g. "512Mi"
|
|
}
|
|
for item in data["items"]
|
|
]
|
|
except Exception as e:
|
|
return {"error": str(e), "hint": "Is metrics-server installed?"}
|