feat: get_clusters() and create_vm_from_cloud_init(node=)
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Failing after 1m2s
driver / test (3.12) (pull_request) Failing after 57s
driver / test (3.13) (pull_request) Failing after 52s
driver / lowest (pull_request) Failing after 25s
driver / build (pull_request) Successful in 24s
driver / security (pull_request) Successful in 57s
CI / driver (pull_request) Failing after 2m21s
driver / publish (pull_request) Skipped

vCenter reports each cluster compute resource, and each standalone host
it runs as a cluster of one, identified by "<instanceUuid>/<moref>"; an
ESXi host reports itself. Nodes carry state, maintenance, CPU and memory;
datastores and port groups carry the hosts that see them.

create_vm_from_cloud_init(node=) places the VM on that host and refuses
one that does not exist or cannot take it, before anything is created.
Placement now runs before the image download, so a refused host fails at
once instead of after the download.

Refs NetOrk/netork#484, NetOrk/netork#1822
This commit is contained in:
Christian Manivong committed 2026-11-01 16:38:38 +01:00
1 parent 1bf8de4c23
commit 52c802ed41
14 files changed
+812 -28

No files matched your search

+55 -15
View File
@@ -2,9 +2,10 @@
A host qualifies when it is connected, not in maintenance mode, and sees both
a usable datastore and every requested network. Among those, the pair with
the most free space wins -- or the named datastore, when one is given. The
resource pool comes from the host's compute resource and the VM folder from
the datacenter above it; a standalone ESXi host has the same shape
the most free space wins -- or the named datastore, when one is given; a
named host narrows the choice to that host and is refused if it does not
qualify. The resource pool comes from the host's compute resource and the VM
folder from the datacenter above it; a standalone ESXi host has the same shape
(``ha-compute-res`` under ``ha-datacenter``), so one path serves both.
"""
@@ -16,6 +17,10 @@ from typing import Any
Row = dict[str, Any]
class NodeRefused(ValueError):
"""The host named for a new VM does not exist or cannot take it."""
@dataclass
class Inventory:
hosts: list[Row]
@@ -71,15 +76,35 @@ def _datacenter(inv: Inventory, compute_resource: str) -> Row:
return datacenters[node]
def choose_placement(inv: Inventory, storage: str | None, networks: list[str]) -> Placement:
usable = _usable_datastores(inv, storage)
names = {n["_moref"]: n.get("name", "") for n in inv.networks}
def _refusal(host: Row) -> str | None:
"""Why ``host`` cannot take a new VM, or None if it can."""
if host.get("runtime.connectionState") != "connected":
return "is not connected"
if host.get("runtime.inMaintenanceMode"):
return "is in maintenance mode"
return None
def _candidates(inv: Inventory, host_name: str | None) -> list[Row]:
"""The hosts placement may choose from: every eligible one, or the named one."""
hosts = sorted(inv.hosts, key=lambda h: h.get("name", ""))
if host_name is None:
return [h for h in hosts if _refusal(h) is None]
named = [h for h in hosts if h.get("name") == host_name]
if not named:
raise NodeRefused(f"There is no host named {host_name!r}")
reason = _refusal(named[0])
if reason:
raise NodeRefused(f"Host {host_name} {reason}")
return named[:1]
def _best(
hosts: list[Row], usable: dict[str, Row], names: dict[str, str], networks: list[str]
) -> tuple[Row, Row, list[str]] | None:
"""The host/datastore pair with the most free space, and the host's networks."""
best: tuple[int, Row, Row, list[str]] | None = None
for host in sorted(inv.hosts, key=lambda h: h.get("name", "")):
if host.get("runtime.connectionState") != "connected" or host.get(
"runtime.inMaintenanceMode"
):
continue
for host in hosts:
host_nets = _host_networks(host, names, networks)
if host_nets is None:
continue
@@ -88,11 +113,26 @@ def choose_placement(inv: Inventory, storage: str | None, networks: list[str]) -
free = int((ds or {}).get("summary", {}).get("freeSpace", 0))
if ds is not None and (best is None or free > best[0]):
best = (free, host, ds, host_nets)
return best[1:] if best else None
def choose_placement(
inv: Inventory, storage: str | None, networks: list[str], *, host_name: str | None = None
) -> Placement:
"""Where a VM on ``storage`` with ``networks`` goes; ``host_name`` pins the host.
Raises NodeRefused (a ValueError) when the named host does not exist or
cannot take the VM, and ValueError when no host can.
"""
usable = _usable_datastores(inv, storage)
names = {n["_moref"]: n.get("name", "") for n in inv.networks}
best = _best(_candidates(inv, host_name), usable, names, networks)
wanted = f"datastore {storage or '(any)'} and networks {networks}"
if best is None and host_name is not None:
raise NodeRefused(f"Host {host_name} does not see {wanted}")
if best is None:
raise ValueError(
f"There is no host that sees datastore {storage or '(any)'} and networks {networks}"
)
_, host, ds, host_nets = best
raise ValueError(f"There is no host that sees {wanted}")
host, ds, host_nets = best
compute = next(c for c in inv.compute_resources if c["_moref"] == host.get("parent"))
dc = _datacenter(inv, compute["_moref"])
return Placement(