34 lines
899 B
Python
34 lines
899 B
Python
from fastapi import APIRouter
|
|
from kubernetes import client, config
|
|
|
|
router = APIRouter()
|
|
|
|
# Initialize Kubernetes client
|
|
def init_k8s_client():
|
|
try:
|
|
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()
|
|
|
|
# Fetch nodes
|
|
nodes = v1.list_node()
|
|
node_data = []
|
|
for node in nodes.items:
|
|
node_data.append({
|
|
"node_name": node.metadata.name,
|
|
"cpu": node.status.capacity.get("cpu"),
|
|
"memory": node.status.capacity.get("memory"),
|
|
"pods_allocatable": node.status.allocatable.get("pods"),
|
|
})
|
|
|
|
# Fetch namespaces
|
|
namespaces = v1.list_namespace()
|
|
namespace_data = [ns.metadata.name for ns in namespaces.items]
|
|
|
|
return {"nodes": node_data, "namespaces": namespace_data}
|