feat: expose host capacity snapshots from engine

dev
admin 2 months ago
parent fe43fdbf9c
commit 3c305a552d

@ -114,6 +114,21 @@ class FakeVMService:
return {"networks": ["default"], "isos": ["AlmaLinux-10.iso"],
"storage_pool": "images"}
def host_capacity(self):
self.calls.append(("host_capacity",))
return {
"instances": {"total": 1 + len(self.other_vms), "running": 1},
"cpu": {"total": 16, "allocated": 2, "available": 14},
"memory_mib": {
"total": 32768, "allocated": 2048, "available": 30720,
},
"storage_bytes": {
"total": 100 * 1024**3,
"allocated": 10 * 1024**3,
"available": 90 * 1024**3,
},
}
def ensure_storage_pools(self):
self.calls.append(("ensure_storage_pools",))
return {"storage_pool": "panel-dir-kvm", "iso_pool": "panel-dir-iso"}
@ -756,6 +771,22 @@ def test_host_network_options_lists_only_unallocated_public_ips(web):
assert "8.8.8.3/29" not in result["available_ips"]
assert "8.8.8.4/29" not in result["available_ips"]
capacity_response = client.get("/api/v1/host/capacity")
assert capacity_response.status_code == 200, capacity_response.text
capacity = capacity_response.json()
assert capacity["health"] == "healthy"
assert isinstance(capacity["observed_at"], int)
assert capacity["cpu"] == {"total": 16, "allocated": 2, "available": 14}
assert capacity["public_ipv4"] == {
"configured": True,
"total": 6,
"reserved": 2,
"allocated": 2,
"available": 2,
}
assert "pool_cidr" not in capacity["public_ipv4"]
assert "available_ips" not in capacity["public_ipv4"]
def test_host_accepts_provider_28_and_gateway_address_form(web):
client, _, token, _service, manager, _ = web

@ -232,6 +232,33 @@ def call(client, method, path, actor=USER, value=None, raw=None, headers=None):
return client.request(method, path, content=body, headers=signed)
def test_host_capacity_is_admin_only_and_does_not_expose_host_secrets(boundary):
client, _engine, _store, _logs, _services = boundary
denied = call(client, "GET", "/internal/v1/hosts/hk/host/capacity", USER)
assert denied.status_code == 403
response = call(
client, "GET", "/internal/v1/hosts/hk/host/capacity", SUPER,
)
assert response.status_code == 200, response.text
payload = response.json()
assert payload["health"] == "healthy"
assert isinstance(payload["observed_at"], int)
assert payload["cpu"] == {"total": 16, "allocated": 2, "available": 14}
assert payload["public_ipv4"] == {
"configured": False,
"total": 0,
"reserved": 0,
"allocated": 0,
"available": 0,
}
for secret_value in (
"host.example.test", "/fake/config", "/fake/known_hosts", "SHA256:example",
):
assert secret_value not in response.text
def import_user_iso_for_test(client, tmp_path, name, *, digest="e" * 64):
download = tmp_path / (hashlib.sha256(name.encode()).hexdigest() + ".iso")
download.write_bytes(b"i" * (1024 * 1024))

@ -441,7 +441,7 @@ class FakeConnection:
raise FakeLibvirtError("domain not found")
def getInfo(self):
return ("x86_64", 0, 0, 0, 0, 0, 0, 0)
return self.backend.node_info
def storagePoolLookupByName(self, name):
try:
@ -581,6 +581,7 @@ class FakeBackend:
self.network_name = "default"
self.network_lookups = []
self.nwfilters = {"clean-traffic"}
self.node_info = ("x86_64", 0, 0, 0, 0, 0, 0, 0)
self.bridge_active = True
self.fail_define = False
self.fail_create = False
@ -1028,6 +1029,7 @@ class VMServiceTests(unittest.TestCase):
self.assertEqual(self.service.get_vm_state(vm["uuid"]), {
"uuid": vm["uuid"], "name": "alma-test", "state": "running",
})
metadata = self.service.list_vm_metadata([vm["uuid"]])
self.assertEqual(len(metadata), 1)
self.assertEqual(metadata[0]["uuid"], vm["uuid"])
@ -1075,6 +1077,27 @@ class VMServiceTests(unittest.TestCase):
self.assertEqual(len(self.backend.define_xmls), 1)
self.assertEqual(self.backend.opened, self.backend.closed)
def test_host_capacity_counts_all_domains_and_preserves_storage_bytes(self):
self.backend.node_info = (
"x86_64", 64 * 1024 * 1024, 16, 3000, 1, 1, 8, 2,
)
self.service.create_vm(self.spec(vcpu=4, memory_mib=8192))
capacity = self.service.host_capacity()
self.assertEqual(capacity["instances"], {"total": 1, "running": 1})
self.assertEqual(capacity["cpu"], {
"total": 16, "allocated": 4, "available": 12,
})
self.assertEqual(capacity["memory_mib"], {
"total": 65536, "allocated": 8192, "available": 57344,
})
self.assertEqual(capacity["storage_bytes"], {
"total": 100 * 1024**3,
"allocated": 10 * 1024**3,
"available": 90 * 1024**3,
})
def test_iso_create_accepts_preassigned_uuid_for_user_ownership(self):
requested_uuid = "14d9bb2f-65cd-4406-a377-90685c0690e4"
created = self.service.create_vm(self.spec(vm_uuid=requested_uuid))

@ -591,13 +591,13 @@ def create_app(
return f"{address}/{guest_prefix}"
raise VMError("公网地址池中没有可分配的 IP,请检查已用和保留地址", 409)
def public_network_options(profile: dict, service: VMService) -> dict:
"""Return a bounded, non-secret snapshot for the admin IP selector.
def public_network_inventory(profile: dict, service: VMService) -> dict:
"""Build one bounded public-pool inventory without exposing claims.
The list is advisory only. The network mutation endpoint still
performs its own validation and atomic claim immediately before
changing libvirt, so a stale selector can never allocate an address
twice.
The supported /24 through /30 pools contain at most 254 host
addresses, so materializing these sets is intentionally bounded. VM
metadata and durable claims are merged because an uncertain create
must keep its address unavailable even when no domain is visible yet.
"""
pool_value = profile.get("public_pool_cidr", "")
gateway_value = profile.get("public_gateway", "")
@ -616,9 +616,10 @@ def create_app(
"guest_prefix": None,
"gateway": "",
"dns_servers": [],
"available_ips": [],
"available_count": 0,
"message": "宿主机尚未配置公网地址池,暂时无法更换公网 IP。",
"usable_count": 0,
"reserved_count": 0,
"allocated_count": 0,
"available": [],
}
try:
network = ipaddress.IPv4Network(
@ -648,17 +649,20 @@ def create_app(
or address in (network.network_address, network.broadcast_address)
for address in reserved):
raise ValueError
blocked = {network.network_address, network.broadcast_address, gateway,
*dns, *reserved}
statically_blocked = {
network.network_address, network.broadcast_address, gateway,
*dns, *reserved,
}
try:
host_address = ipaddress.IPv4Address(profile.get("host", ""))
except (TypeError, ValueError):
host_address = None
if host_address is not None:
blocked.add(host_address)
statically_blocked.add(host_address)
except (KeyError, TypeError, ValueError) as exc:
raise VMError("宿主机公网地址池配置损坏,请管理员重新保存设置", 500) from exc
allocated: set[ipaddress.IPv4Address] = set()
for existing_vm in service.list_vms():
configured = existing_vm.get("public_ip_cidr")
if not isinstance(configured, str):
@ -668,7 +672,7 @@ def create_app(
except ValueError:
continue
if address in network:
blocked.add(address)
allocated.add(address)
scope_prefix = f"{host_scope(profile)}."
if data_store is not None:
@ -680,21 +684,69 @@ def create_app(
except ValueError:
continue
if address in network:
blocked.add(address)
allocated.add(address)
available = [f"{address}/{guest_prefix}" for address in network.hosts()
if address not in blocked]
usable = set(network.hosts())
reserved_in_pool = usable & statically_blocked
allocated_in_pool = (usable - reserved_in_pool) & allocated
available = sorted(usable - reserved_in_pool - allocated_in_pool)
return {
"configuration_state": "ready",
"pool_cidr": str(network),
"guest_prefix": guest_prefix,
"gateway": str(gateway),
"dns_servers": [str(value) for value in dns],
"available_ips": available,
"available_count": len(available),
"usable_count": len(usable),
"reserved_count": len(reserved_in_pool),
"allocated_count": len(allocated_in_pool),
"available": available,
}
def public_network_options(profile: dict, service: VMService) -> dict:
"""Return a bounded, non-secret snapshot for the admin IP selector.
The list is advisory only. The network mutation endpoint still
performs its own validation and atomic claim immediately before
changing libvirt, so a stale selector can never allocate an address
twice.
"""
inventory = public_network_inventory(profile, service)
if inventory["configuration_state"] == "not_configured":
return {
"configuration_state": "not_configured",
"pool_cidr": "",
"guest_prefix": None,
"gateway": "",
"dns_servers": [],
"available_ips": [],
"available_count": 0,
"message": "宿主机尚未配置公网地址池,暂时无法更换公网 IP。",
}
available_ips = [
f"{address}/{inventory['guest_prefix']}"
for address in inventory["available"]
]
return {
"configuration_state": "ready",
"pool_cidr": inventory["pool_cidr"],
"guest_prefix": inventory["guest_prefix"],
"gateway": inventory["gateway"],
"dns_servers": inventory["dns_servers"],
"available_ips": available_ips,
"available_count": len(available_ips),
"message": "目标地址仍会由引擎在提交时再次校验并原子占用。",
}
def public_ip_capacity(profile: dict, service: VMService) -> dict:
inventory = public_network_inventory(profile, service)
return {
"configured": inventory["configuration_state"] == "ready",
"total": inventory["usable_count"],
"reserved": inventory["reserved_count"],
"allocated": inventory["allocated_count"],
"available": len(inventory["available"]),
}
def claim_explicit_public_ip(profile: dict, public_ip_cidr: str,
vm_id: str) -> None:
if data_store is None:
@ -1605,6 +1657,19 @@ def create_app(
profile = current_profile()
return public_network_options(profile, current_service())
@app.get("/api/v1/host/capacity")
def host_capacity(_session: Session = Depends(require_admin)):
"""Return the scheduler's non-secret observation for this host."""
profile = current_profile()
service = new_service(profile)
capacity = service.host_capacity()
return {
"health": "healthy",
"observed_at": int(time.time()),
**capacity,
"public_ipv4": public_ip_capacity(profile, service),
}
@app.post("/api/v1/host/scan")
def scan_host(body: HostScanBody, _session: Session = Depends(require_admin_write)):
return manager.scan(body.host, body.port)

@ -59,7 +59,7 @@ JSON_LIMIT = 1024 * 1024
CLOUD_UPLOAD_IDLE_SECONDS = 60
CLOUD_UPLOAD_WRITE_BYTES = 1024 * 1024
READ_PATHS = (
r"host", r"host/network-options", r"resources", r"vm-templates", r"rescue-templates", r"user-isos", r"vms",
r"host", r"host/network-options", r"host/capacity", r"resources", r"vm-templates", r"rescue-templates", r"user-isos", r"vms",
r"vm-states", r"vm-metadata",
r"user-isos/[0-9a-f]{32}",
rf"vms/{VM_ID}(?:/state|/metrics|/traffic/daily|/rescue)?",

@ -860,6 +860,68 @@ class VMService:
"storage_available_gib": pool_info[3] // GIB,
}
def host_capacity(self) -> dict[str, Any]:
"""Return a non-secret schedulable-capacity observation.
CPU and memory allocation count every libvirt-visible domain, not just
panel-managed guests. This deliberately makes the scheduler
conservative when an operator also creates VMs outside the panel.
Storage values are the configured VM storage pool's native byte
counters so callers do not lose precision by rounding to GiB.
"""
with self._connect() as conn:
node = conn.getInfo()
if (not isinstance(node, (tuple, list)) or len(node) < 3
or isinstance(node[1], bool) or not isinstance(node[1], int)
or isinstance(node[2], bool) or not isinstance(node[2], int)
or node[1] < 0 or node[2] < 0):
raise VMError("宿主机容量信息无效", 502)
cpu_total = node[2]
memory_total_mib = node[1] // 1024
cpu_allocated = 0
memory_allocated_mib = 0
running = 0
domains = conn.listAllDomains(0)
for domain in domains:
info = domain.info()
if (not isinstance(info, (tuple, list)) or len(info) < 4
or isinstance(info[1], bool) or not isinstance(info[1], int)
or isinstance(info[3], bool) or not isinstance(info[3], int)
or info[1] < 0 or info[3] < 0):
raise VMError("宿主机虚拟机容量信息无效", 502)
memory_allocated_mib += info[1] // 1024
cpu_allocated += info[3]
if info[0] in {1, 2, 3, 7}:
running += 1
disk_pool = self._active_pool(conn, self.storage_pool)
pool_info = disk_pool.info()
if (not isinstance(pool_info, (tuple, list)) or len(pool_info) < 4
or any(isinstance(value, bool) or not isinstance(value, int)
or value < 0 for value in pool_info[1:4])):
raise VMError("宿主机存储容量信息无效", 502)
storage_total, storage_allocated, storage_available = pool_info[1:4]
return {
"instances": {"total": len(domains), "running": running},
"cpu": {
"total": cpu_total,
"allocated": cpu_allocated,
"available": max(0, cpu_total - cpu_allocated),
},
"memory_mib": {
"total": memory_total_mib,
"allocated": memory_allocated_mib,
"available": max(0, memory_total_mib - memory_allocated_mib),
},
"storage_bytes": {
"total": storage_total,
"allocated": storage_allocated,
"available": storage_available,
},
}
def _validate_common_create(self, spec: dict[str, Any]) -> dict[str, Any]:
if not isinstance(spec, dict):
raise VMError("虚拟机配置无效")

Loading…
Cancel
Save