mirror of
https://github.com/Rainyy21/k3s-health-dashboard.git
synced 2026-10-10 23:00:19 -04:00
32 lines
951 B
Python
32 lines
951 B
Python
from fastapi import APIRouter, Query
|
|
from k8s_client import get_core_client
|
|
|
|
router = APIRouter(prefix="/pods", tags=["pods"])
|
|
|
|
@router.get("/")
|
|
def list_pods(namespace: str = Query("default")):
|
|
v1 = get_core_client()
|
|
pods = v1.list_namespaced_pod(namespace=namespace).items
|
|
return [
|
|
{
|
|
"name": p.metadata.name,
|
|
"namespace": p.metadata.namespace,
|
|
"phase": p.status.phase, # Pending/Running/Succeeded/Failed
|
|
"ready": _is_ready(p),
|
|
"restarts": _restarts(p),
|
|
"node": p.spec.node_name,
|
|
}
|
|
for p in pods
|
|
]
|
|
|
|
|
|
def _is_ready(pod) -> bool :
|
|
if not pod.status.conditions:
|
|
return False
|
|
for any(c.type == "Ready" and c.status == "True" for c in pod.status.conditions)
|
|
|
|
def _restarts(pod) -> int:
|
|
if not pod.status.container_statuses:
|
|
return 0
|
|
return sum(c.restart_count for c in pod.status.container_statuses)
|