|
| 1 | +from fastapi import APIRouter |
| 2 | +from internal_svc import SIGNATURE_METADATA |
| 3 | +from orchestrator.k8s import node_ip_mapping |
| 4 | +from api_server.v1.models import NodeList, Node, PoolStat, PoolsStat |
| 5 | +import grpc |
| 6 | +from internal import internal_pb2, internal_pb2_grpc |
| 7 | +from config import config |
| 8 | +import ipaddress |
| 9 | +import asyncio |
| 10 | + |
| 11 | +router = APIRouter() |
| 12 | + |
| 13 | + |
| 14 | +def get_node_pool_stat(node_ip): |
| 15 | + node_ip_str = node_ip |
| 16 | + metadata = [(SIGNATURE_METADATA, config.csi_driver.internal_signature)] |
| 17 | + node_ip = ipaddress.ip_address(node_ip_str) |
| 18 | + if node_ip.version == 6: |
| 19 | + node_ip_str = f"[{node_ip_str}]" |
| 20 | + channel = grpc.insecure_channel(f"{node_ip_str}:{config.csi_driver.internal_port}") |
| 21 | + stub = internal_pb2_grpc.InternalStub(channel) |
| 22 | + return stub.GetPoolsStats( |
| 23 | + internal_pb2.GetPoolsStatsRequest(), |
| 24 | + metadata=metadata, |
| 25 | + timeout=15, |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +async def build_node(node_name: str, node_ip: str) -> Node: |
| 30 | + pool_stats = await get_node_pool_stat(node_ip) |
| 31 | + |
| 32 | + return Node( |
| 33 | + name=node_name, |
| 34 | + ip=node_ip, |
| 35 | + pools_stat=PoolsStat( |
| 36 | + { |
| 37 | + name: PoolStat( |
| 38 | + reserved_capacity=pool.reserved_capacity, |
| 39 | + path=pool.path, |
| 40 | + reserved_capacity_mode=pool.reserved_capacity_mode, |
| 41 | + capacity=pool.capacity, |
| 42 | + ) |
| 43 | + for name, pool in pool_stats.stats.items() |
| 44 | + } |
| 45 | + ), |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@router.get("/") |
| 50 | +async def get_nodes() -> NodeList: |
| 51 | + node_list = [] |
| 52 | + node_ips = node_ip_mapping.get_all_nodes() |
| 53 | + tasks = [build_node(node_name, node_ip) for node_name, node_ip in node_ips.items()] |
| 54 | + node_list = await asyncio.gather(*tasks) |
| 55 | + return node_list |
0 commit comments