Merge pull request 'feat: get_clusters() for the whole cluster, node= refused for another node' (#39) from feat/cluster-inventory into main
driver / lint (push) Successful in 13s
driver / standard (push) Successful in 15s
driver / test (3.12) (push) Failing after 6s
driver / lowest (push) Successful in 39s
driver / types (push) Successful in 1m4s
driver / test (3.13) (push) Successful in 1m6s
driver / build (push) Successful in 26s
driver / security (push) Successful in 1m10s
CI / driver (push) Failing after 2m8s
driver / publish (push) Skipped
driver / lint (push) Successful in 13s
driver / standard (push) Successful in 15s
driver / test (3.12) (push) Failing after 6s
driver / lowest (push) Successful in 39s
driver / types (push) Successful in 1m4s
driver / test (3.13) (push) Successful in 1m6s
driver / build (push) Successful in 26s
driver / security (push) Successful in 1m10s
CI / driver (push) Failing after 2m8s
driver / publish (push) Skipped
This commit was merged in pull request #39.
This commit is contained in:
commit
cb29a42c31
8 files changed
+858
-45
No files matched your search
@@ -12,6 +12,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6).
|
||||
|
||||
### Added
|
||||
- `get_clusters()`: the cluster the node belongs to, always exactly one, read
|
||||
from `/cluster/status`, `/cluster/resources`, each online node's status and
|
||||
network, SDN vnets/zones and the HA manager. Every member is listed, an
|
||||
offline one with `status` `"offline"` and no capacity; `maintenance` is None
|
||||
when the HA manager cannot be read. Storages are grouped by name with the
|
||||
nodes they are available on (size: one node's figure for a shared storage,
|
||||
the sum over its nodes for a local one); bridges are merged across nodes,
|
||||
VLAN aware only if they are on every node; a vnet is on its zone's nodes. A
|
||||
standalone node is a cluster of one without a name. `create_vm_from_cloud_init()`
|
||||
takes `node` and raises ValueError, before creating anything, for a node
|
||||
other than its own (NetOrk/netork#484, NetOrk/netork#1822). Needs the cluster
|
||||
models and `get_clusters()` contract of napalm-device-types.
|
||||
- `get_network_targets()` names each bridge's physical NICs in `uplinks`, read
|
||||
from `bridge_ports`/`ovs_ports`, one group per bridge port: a bond's slaves
|
||||
together, a NIC alone, without internal ports (`OVSIntPort`) and VLAN
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
"""HypervisorDriver.get_clusters() for Proxmox VE: the cluster this node belongs to.
|
||||
|
||||
Every PVE node can read its whole cluster, so the getter always reports exactly
|
||||
one cluster, the one the connected node is a member of (a standalone node is a
|
||||
cluster of one, without a name). Read from:
|
||||
|
||||
- ``GET /cluster/status``: the cluster's name and quorum, and every member with
|
||||
``online`` and ``local``;
|
||||
- ``GET /cluster/resources``: per node ``maxcpu``, ``cpu``, ``maxmem``, ``mem``;
|
||||
per node and storage one row with ``plugintype``, ``shared``, ``content``,
|
||||
``maxdisk``, ``disk`` and ``status``;
|
||||
- ``GET /nodes/{node}/status`` of each online node: ``cpuinfo``;
|
||||
- ``GET /nodes/{node}/network`` of each online node: its bridges;
|
||||
- ``GET /cluster/sdn/vnets`` and ``/cluster/sdn/zones``: the vnets, on the nodes
|
||||
of their zone;
|
||||
- ``GET /cluster/ha/status/manager_status``: which node is in maintenance.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Any, NamedTuple
|
||||
|
||||
from napalm_proxmox.network_targets import bridge_targets, vnet_target
|
||||
from napalm_proxmox.utils import cpu_flags
|
||||
|
||||
if TYPE_CHECKING:
|
||||
# Type-only: the cluster models are newer than the napalm_device_types floor
|
||||
# in pyproject.toml, and nothing here needs them at runtime.
|
||||
from napalm_device_types import (
|
||||
ClusterDict,
|
||||
ClusterNetworkDict,
|
||||
ClusterNodeDict,
|
||||
ClusterStorageDict,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_JsonDict = dict[str, Any]
|
||||
|
||||
|
||||
class _Member(NamedTuple):
|
||||
name: str
|
||||
online: bool
|
||||
local: bool
|
||||
|
||||
|
||||
class _Cpu(NamedTuple):
|
||||
cores: int | None = None
|
||||
model: str | None = None
|
||||
flags: list[str] | None = None
|
||||
|
||||
|
||||
def _int(value: Any) -> int | None:
|
||||
return None if value is None else int(value)
|
||||
|
||||
|
||||
def _float(value: Any) -> float | None:
|
||||
return None if value is None else float(value)
|
||||
|
||||
|
||||
def _flag(value: Any) -> bool:
|
||||
return bool(int(value or 0))
|
||||
|
||||
|
||||
def _list(value: Any) -> list[str]:
|
||||
return [part.strip() for part in str(value or "").split(",") if part.strip()]
|
||||
|
||||
|
||||
def _size(rows: list[_JsonDict], key: str, shared: bool) -> int | None:
|
||||
"""One storage's size over the nodes in *rows*: summed unless it is shared."""
|
||||
values = [row.get(key) for row in rows]
|
||||
if not values or any(value is None for value in values):
|
||||
return None
|
||||
sizes = [int(value) for value in values if value is not None]
|
||||
return sizes[0] if shared else sum(sizes)
|
||||
|
||||
|
||||
def _storage(name: str, rows: list[_JsonDict]) -> ClusterStorageDict:
|
||||
shared = any(_flag(row.get("shared")) for row in rows)
|
||||
available = [row for row in rows if row.get("status") == "available"]
|
||||
return {
|
||||
"name": name,
|
||||
"type": str(rows[0].get("plugintype") or ""),
|
||||
"shared": shared,
|
||||
"content": _list(rows[0].get("content")),
|
||||
"nodes": [str(row.get("node")) for row in available],
|
||||
"total": _size(available, "maxdisk", shared),
|
||||
"used": _size(available, "disk", shared),
|
||||
}
|
||||
|
||||
|
||||
def _storages(resources: list[_JsonDict]) -> list[ClusterStorageDict]:
|
||||
groups: dict[str, list[_JsonDict]] = {}
|
||||
for row in resources:
|
||||
if row.get("type") == "storage" and row.get("storage"):
|
||||
groups.setdefault(str(row["storage"]), []).append(row)
|
||||
return [_storage(name, rows) for name, rows in groups.items()]
|
||||
|
||||
|
||||
def _merge_bridges(per_node: list[tuple[str, list[_JsonDict]]]) -> list[ClusterNetworkDict]:
|
||||
merged: dict[str, ClusterNetworkDict] = {}
|
||||
for node, network in per_node:
|
||||
for target in bridge_targets(network):
|
||||
entry = merged.setdefault(
|
||||
target["name"],
|
||||
{"name": target["name"], "kind": "bridge", "vlan_aware": True, "nodes": []},
|
||||
)
|
||||
entry["vlan_aware"] = entry["vlan_aware"] and target["vlan_aware"]
|
||||
entry["nodes"].append(node)
|
||||
return list(merged.values())
|
||||
|
||||
|
||||
class ProxmoxClusterMixin:
|
||||
"""Relies on ``_api``/``_node_name`` from the driver and the SDN readers."""
|
||||
|
||||
_api: Any
|
||||
_node_name: str
|
||||
_get_sdn_vnets: Any
|
||||
_get_sdn_zones: Any
|
||||
|
||||
def get_clusters(self) -> list[ClusterDict]:
|
||||
"""
|
||||
Return the one cluster this node belongs to, its nodes, storages and networks.
|
||||
|
||||
- ``id`` is always None: PVE gives a cluster no identity beyond its name.
|
||||
``name`` and ``quorate`` are None on a standalone node, which reports
|
||||
itself as a cluster of one.
|
||||
- Every member is listed. An offline node has ``status`` ``"offline"`` and
|
||||
no capacity values: its own API cannot be asked. ``local`` marks the
|
||||
node ``/cluster/status`` calls local, or else the node this driver
|
||||
talks to. ``maintenance`` comes from the HA manager; when that cannot
|
||||
be read (no HA stack, no permission) or does not track a node, it is
|
||||
None. If another node's status cannot be read although the cluster
|
||||
calls it online, its CPU fields are None and its bridges are not
|
||||
listed; the own node's must be readable.
|
||||
- A storage is one entry per name, with the nodes it is ``available``
|
||||
on. ``total``/``used``: a shared storage is the same space on every
|
||||
node, so it is one node's figure; a local storage of that name is a
|
||||
separate disk on each node, so the figures of its nodes add up — the
|
||||
room any single node has is less. None when no node reports it.
|
||||
- Networks: each bridge once, with every online node that has one of
|
||||
that name; ``vlan_aware`` only if it is on all of them. A vnet is on
|
||||
the nodes of its zone, all members when the zone names none.
|
||||
|
||||
Raises:
|
||||
Whatever the API raises for ``/cluster/status``, ``/cluster/resources``
|
||||
or the own node: a getter that cannot read does not report less.
|
||||
"""
|
||||
status = self._api.cluster.status.get() or []
|
||||
resources = self._api.cluster.resources.get() or []
|
||||
head = next((s for s in status if s.get("type") == "cluster"), None)
|
||||
members = self._members(status if head else [])
|
||||
return [
|
||||
{
|
||||
"id": None,
|
||||
"name": (head.get("name") or None) if head else None,
|
||||
"quorate": _flag(head.get("quorate")) if head else None,
|
||||
"nodes": self._cluster_nodes(members, resources),
|
||||
"storages": _storages(resources),
|
||||
"networks": self._networks(members),
|
||||
}
|
||||
]
|
||||
|
||||
def _cluster_nodes(
|
||||
self, members: list[_Member], resources: list[_JsonDict]
|
||||
) -> list[ClusterNodeDict]:
|
||||
rows = {r.get("node"): r for r in resources if r.get("type") == "node"}
|
||||
maintenance = self._maintenance()
|
||||
return [self._cluster_node(m, rows.get(m.name, {}), maintenance) for m in members]
|
||||
|
||||
def _networks(self, members: list[_Member]) -> list[ClusterNetworkDict]:
|
||||
online = [m for m in members if m.online]
|
||||
return [
|
||||
*_merge_bridges([(m.name, self._node_network(m)) for m in online]),
|
||||
*self._vnets([m.name for m in members]),
|
||||
]
|
||||
|
||||
def _members(self, status: list[_JsonDict]) -> list[_Member]:
|
||||
entries = [s for s in status if s.get("type") == "node" and s.get("name")]
|
||||
if not entries:
|
||||
return [_Member(self._node_name, online=True, local=True)]
|
||||
any_local = any(_flag(s.get("local")) for s in entries)
|
||||
return [
|
||||
_Member(
|
||||
str(s["name"]),
|
||||
online=_flag(s.get("online")),
|
||||
local=_flag(s.get("local")) if any_local else s["name"] == self._node_name,
|
||||
)
|
||||
for s in entries
|
||||
]
|
||||
|
||||
def _maintenance(self) -> dict[str, bool] | None:
|
||||
try:
|
||||
status = self._api.cluster.ha.status.manager_status.get() or {}
|
||||
except Exception as exc:
|
||||
logger.debug("Cannot read the HA manager status: %s", exc)
|
||||
return None
|
||||
node_status = (status.get("manager_status") or {}).get("node_status") or {}
|
||||
return {str(node): state == "maintenance" for node, state in node_status.items()}
|
||||
|
||||
def _read_node(self, member: _Member, path: str) -> Any:
|
||||
"""``GET /nodes/{node}/{path}``; None for another node that does not answer."""
|
||||
try:
|
||||
return getattr(self._api.nodes(member.name), path).get()
|
||||
except Exception as exc:
|
||||
if member.local:
|
||||
raise
|
||||
logger.warning("Cannot read %s of cluster node %s: %s", path, member.name, exc)
|
||||
return None
|
||||
|
||||
def _node_network(self, member: _Member) -> list[_JsonDict]:
|
||||
return self._read_node(member, "network") or []
|
||||
|
||||
def _cpu(self, member: _Member) -> _Cpu:
|
||||
status = self._read_node(member, "status")
|
||||
if status is None:
|
||||
return _Cpu()
|
||||
info = status.get("cpuinfo") or {}
|
||||
cores, sockets = info.get("cores"), info.get("sockets")
|
||||
return _Cpu(
|
||||
cores=int(cores) * int(sockets) if cores and sockets else None,
|
||||
model=info.get("model") or None,
|
||||
flags=cpu_flags(status),
|
||||
)
|
||||
|
||||
def _cluster_node(
|
||||
self, member: _Member, row: _JsonDict, maintenance: dict[str, bool] | None
|
||||
) -> ClusterNodeDict:
|
||||
if not member.online:
|
||||
row, cpu = {}, _Cpu()
|
||||
else:
|
||||
cpu = self._cpu(member)
|
||||
return {
|
||||
"name": member.name,
|
||||
"status": "online" if member.online else "offline",
|
||||
"local": member.local,
|
||||
"maintenance": None if maintenance is None else maintenance.get(member.name),
|
||||
"cpu_threads": _int(row.get("maxcpu")),
|
||||
"cpu_cores": cpu.cores,
|
||||
"cpu_model": cpu.model,
|
||||
"cpu_flags": cpu.flags,
|
||||
"cpu_usage": _float(row.get("cpu")),
|
||||
"memory_total": _int(row.get("maxmem")),
|
||||
"memory_used": _int(row.get("mem")),
|
||||
}
|
||||
|
||||
def _vnets(self, members: list[str]) -> list[ClusterNetworkDict]:
|
||||
vnets = self._get_sdn_vnets()
|
||||
zones = {z.get("zone"): z for z in self._get_sdn_zones()} if vnets else {}
|
||||
networks: list[ClusterNetworkDict] = []
|
||||
for vnet in vnets:
|
||||
target, zone = vnet_target(vnet), zones.get(vnet.get("zone"))
|
||||
if target is None or zone is None:
|
||||
continue
|
||||
only = set(_list(zone.get("nodes")))
|
||||
networks.append(
|
||||
{
|
||||
"name": target["name"],
|
||||
"kind": "vnet",
|
||||
"vlan_aware": False,
|
||||
"fixed_vlan_tag": target.get("fixed_vlan_tag"),
|
||||
"nodes": [n for n in members if not only or n in only],
|
||||
}
|
||||
)
|
||||
return networks
|
||||
@@ -55,6 +55,7 @@ try:
|
||||
except ImportError as exc:
|
||||
raise ImportError("proxmoxer is required: pip install proxmoxer") from exc
|
||||
|
||||
from napalm_proxmox.cluster_mixin import ProxmoxClusterMixin
|
||||
from napalm_proxmox.config_mixin import ProxmoxConfigMixin
|
||||
from napalm_proxmox.interfaces_mixin import ProxmoxInterfaceMixin
|
||||
from napalm_proxmox.lldp_mixin import ProxmoxLLDPMixin
|
||||
@@ -87,6 +88,7 @@ class ProxmoxDriver(
|
||||
ProxmoxVMContractMixin,
|
||||
ProxmoxVMSnapshotMixin,
|
||||
ProxmoxVMProvisionMixin,
|
||||
ProxmoxClusterMixin,
|
||||
ProxmoxRoutingMixin,
|
||||
ProxmoxSystemMixin,
|
||||
KernelFactsMixin,
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""What a VM's NIC may attach to on a node (``NetworkTargetDict``).
|
||||
|
||||
A Linux or OVS bridge from the node's network config (``GET /nodes/{node}/network``),
|
||||
or an SDN vnet (``GET /cluster/sdn/vnets``). Physical NICs, bonds and other
|
||||
interface types are never valid ``NICConfigDict.bridge`` values on Proxmox.
|
||||
Shared by ``get_network_targets()`` (the own node) and ``get_clusters()`` (every
|
||||
online node of the cluster).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from napalm_device_types.models import NetworkTargetDict
|
||||
|
||||
from napalm_proxmox.uplinks import bridge_uplinks
|
||||
|
||||
_BRIDGE_TYPES = frozenset({"bridge", "OVSBridge"})
|
||||
|
||||
|
||||
def bridge_targets(network: list[dict[str, Any]]) -> list[NetworkTargetDict]:
|
||||
"""The bridges in one node's *network* config, with their uplinks where known."""
|
||||
targets: list[NetworkTargetDict] = []
|
||||
for iface in network:
|
||||
iface_type = iface.get("type")
|
||||
name = iface.get("iface", "")
|
||||
if not name or iface_type not in _BRIDGE_TYPES:
|
||||
continue
|
||||
# OVS bridges tag per-port regardless of a dedicated "VLAN aware" setting.
|
||||
vlan_aware = iface_type == "OVSBridge" or bool(int(iface.get("bridge_vlan_aware", 0) or 0))
|
||||
target: NetworkTargetDict = {"name": name, "kind": "bridge", "vlan_aware": vlan_aware}
|
||||
uplinks = bridge_uplinks(iface, network)
|
||||
if uplinks is not None:
|
||||
target["uplinks"] = uplinks
|
||||
targets.append(target)
|
||||
return targets
|
||||
|
||||
|
||||
def vnet_target(vnet: dict[str, Any]) -> NetworkTargetDict | None:
|
||||
"""An SDN *vnet* as a target, or None when it has no name."""
|
||||
name = vnet.get("vnet", "")
|
||||
if not name:
|
||||
return None
|
||||
# A vnet's VLAN is already fixed by its zone/tag — no separate vlan_tag applies.
|
||||
tag = vnet.get("tag")
|
||||
return {
|
||||
"name": name,
|
||||
"kind": "vnet",
|
||||
"vlan_aware": False,
|
||||
"fixed_vlan_tag": int(tag) if tag is not None else None,
|
||||
}
|
||||
@@ -120,6 +120,12 @@ def is_ovs_interface(iface_data: dict[str, Any]) -> bool:
|
||||
return iface_data.get("type", "") in OVS_TYPES
|
||||
|
||||
|
||||
def cpu_flags(node_status: dict[str, Any]) -> list[str] | None:
|
||||
"""The CPU flags in a ``GET /nodes/{node}/status`` answer, None when it names none."""
|
||||
flags = (node_status.get("cpuinfo") or {}).get("flags")
|
||||
return None if flags is None else str(flags).split()
|
||||
|
||||
|
||||
def is_sdn_vnet(name: str) -> bool:
|
||||
return bool(re.fullmatch(r"vnet\d+", name))
|
||||
|
||||
|
||||
@@ -25,7 +25,8 @@ from napalm_device_types.provisioning import (
|
||||
split_compression,
|
||||
)
|
||||
|
||||
from napalm_proxmox.uplinks import bridge_uplinks
|
||||
from napalm_proxmox.network_targets import bridge_targets, vnet_target
|
||||
from napalm_proxmox.utils import cpu_flags
|
||||
|
||||
if TYPE_CHECKING:
|
||||
# Type-only: VMCpuTypeDict is newer than the napalm_device_types floor in
|
||||
@@ -457,8 +458,7 @@ class ProxmoxVMProvisionMixin:
|
||||
return self._cpu_types_for(self._node_cpu_flags())
|
||||
|
||||
def _node_cpu_flags(self) -> set[str]:
|
||||
status = self._node_api().status.get() or {}
|
||||
return set(str((status.get("cpuinfo") or {}).get("flags", "")).split())
|
||||
return set(cpu_flags(self._node_api().status.get() or {}) or [])
|
||||
|
||||
@staticmethod
|
||||
def _cpu_types_for(node_flags: set[str]) -> list[VMCpuTypeDict]:
|
||||
@@ -533,6 +533,19 @@ class ProxmoxVMProvisionMixin:
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
def _check_placement(self, guest_os: str, node: str | None) -> None:
|
||||
"""Refuse a guest this driver cannot provision, or a node other than its own."""
|
||||
if guest_os not in self.GUEST_AGENTS:
|
||||
raise ValueError(
|
||||
f"Cannot provision a {guest_os!r} guest; this driver provisions "
|
||||
f"{', '.join(sorted(self.GUEST_AGENTS))}"
|
||||
)
|
||||
if node is not None and node != self._node_name:
|
||||
raise ValueError(
|
||||
f"Cannot create a VM on node {node!r} through {self._node_name!r}; "
|
||||
f"connect to {node!r} itself"
|
||||
)
|
||||
|
||||
def create_vm_from_cloud_init(
|
||||
self,
|
||||
name: str,
|
||||
@@ -548,6 +561,7 @@ class ProxmoxVMProvisionMixin:
|
||||
storage: str | None = None,
|
||||
cpu_type: str | None = None,
|
||||
guest_os: str = "linux",
|
||||
node: str | None = None,
|
||||
download_timeout: int = 300,
|
||||
timeout: int = 180,
|
||||
) -> VMProvisionResultDict:
|
||||
@@ -585,6 +599,9 @@ class ProxmoxVMProvisionMixin:
|
||||
OS type and how the agent is attached; a guest other than Linux
|
||||
also gets a network-config v2 snippet of its own, because
|
||||
FreeBSD's nuageinit skips runcmd on the v1 Proxmox writes.
|
||||
node: the cluster node to create on; only this driver's own node
|
||||
(None = that node). netOrk creates on another node through
|
||||
that node's own device.
|
||||
download_timeout: max seconds for the image download (skipped if cached)
|
||||
timeout: max seconds for the remaining provisioning steps
|
||||
|
||||
@@ -594,14 +611,10 @@ class ProxmoxVMProvisionMixin:
|
||||
Raises:
|
||||
RuntimeError: provisioning failure (download, import, config, timeout, etc.)
|
||||
ValueError: invalid storage or configuration, or a cpu_type that is
|
||||
unknown or that this node's CPU cannot run (raised before
|
||||
anything is created)
|
||||
unknown or that this node's CPU cannot run, or another node
|
||||
(raised before anything is created)
|
||||
"""
|
||||
if guest_os not in self.GUEST_AGENTS:
|
||||
raise ValueError(
|
||||
f"Cannot provision a {guest_os!r} guest; this driver provisions "
|
||||
f"{', '.join(sorted(self.GUEST_AGENTS))}"
|
||||
)
|
||||
self._check_placement(guest_os, node)
|
||||
try:
|
||||
_logger.info(f"Creating {guest_os} VM '{name}' from image {image_url}")
|
||||
cpu_model = self._resolve_cpu_type(cpu_type)
|
||||
@@ -950,39 +963,9 @@ class ProxmoxVMProvisionMixin:
|
||||
Excludes physical NICs, bonds, and other non-bridge interface types —
|
||||
those are never valid ``NICConfigDict.bridge`` values on Proxmox.
|
||||
"""
|
||||
targets: list[NetworkTargetDict] = []
|
||||
network = self._get_node_network()
|
||||
|
||||
for iface in network:
|
||||
iface_type = iface.get("type")
|
||||
name = iface.get("iface", "")
|
||||
if not name or iface_type not in ("bridge", "OVSBridge"):
|
||||
continue
|
||||
|
||||
# OVS bridges tag per-port regardless of a dedicated "VLAN aware" setting.
|
||||
vlan_aware = iface_type == "OVSBridge" or bool(
|
||||
int(iface.get("bridge_vlan_aware", 0) or 0)
|
||||
)
|
||||
target: NetworkTargetDict = {"name": name, "kind": "bridge", "vlan_aware": vlan_aware}
|
||||
uplinks = bridge_uplinks(iface, network)
|
||||
if uplinks is not None:
|
||||
target["uplinks"] = uplinks
|
||||
targets.append(target)
|
||||
|
||||
targets = bridge_targets(self._get_node_network())
|
||||
for vnet in self._get_sdn_vnets():
|
||||
name = vnet.get("vnet", "")
|
||||
if not name:
|
||||
continue
|
||||
# A vnet's VLAN is already fixed by its zone/tag — no separate vlan_tag applies.
|
||||
tag = vnet.get("tag")
|
||||
fixed_vlan_tag = int(tag) if tag is not None else None
|
||||
targets.append(
|
||||
{
|
||||
"name": name,
|
||||
"kind": "vnet",
|
||||
"vlan_aware": False,
|
||||
"fixed_vlan_tag": fixed_vlan_tag,
|
||||
}
|
||||
)
|
||||
|
||||
target = vnet_target(vnet)
|
||||
if target is not None:
|
||||
targets.append(target)
|
||||
return targets
|
||||
@@ -22,7 +22,7 @@
|
||||
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin._get_vm_disk_and_boot": 16,
|
||||
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vm_interfaces": 37,
|
||||
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vms": 21,
|
||||
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.create_vm_from_cloud_init": 26,
|
||||
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.create_vm_from_cloud_init": 25,
|
||||
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.get_vm_status": 16
|
||||
},
|
||||
"function_lines": {
|
||||
|
||||
@@ -0,0 +1,493 @@
|
||||
"""get_clusters(): the cluster a node belongs to, its nodes, storages and networks.
|
||||
|
||||
A Proxmox node can read its whole cluster: ``/cluster/status`` names the members
|
||||
and the quorum, ``/cluster/resources`` their capacity and every storage per
|
||||
node, ``/nodes/{node}/status`` the CPU, ``/nodes/{node}/network`` the bridges.
|
||||
netOrk uses it to tell where a new VM may go (NetOrk/netork#484, #1822). The
|
||||
payloads below follow PVE 8.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from napalm_proxmox.driver import ProxmoxDriver
|
||||
from tests.conftest import _build_mock_api
|
||||
|
||||
GIB = 1024**3
|
||||
|
||||
CLUSTER_STATUS = [
|
||||
{"type": "cluster", "id": "cluster", "name": "lab", "nodes": 3, "quorate": 1, "version": 7},
|
||||
{
|
||||
"type": "node",
|
||||
"id": "node/pve1",
|
||||
"name": "pve1",
|
||||
"nodeid": 1,
|
||||
"ip": "10.0.0.1",
|
||||
"local": 1,
|
||||
"online": 1,
|
||||
"level": "",
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"id": "node/pve2",
|
||||
"name": "pve2",
|
||||
"nodeid": 2,
|
||||
"ip": "10.0.0.2",
|
||||
"local": 0,
|
||||
"online": 1,
|
||||
"level": "",
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"id": "node/pve3",
|
||||
"name": "pve3",
|
||||
"nodeid": 3,
|
||||
"ip": "10.0.0.3",
|
||||
"local": 0,
|
||||
"online": 0,
|
||||
"level": "",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def _node_row(node: str, maxcpu: int, cpu: float, maxmem: int, mem: int) -> dict[str, Any]:
|
||||
return {
|
||||
"id": f"node/{node}",
|
||||
"type": "node",
|
||||
"node": node,
|
||||
"status": "online",
|
||||
"cpu": cpu,
|
||||
"maxcpu": maxcpu,
|
||||
"mem": mem,
|
||||
"maxmem": maxmem,
|
||||
"disk": 5 * GIB,
|
||||
"maxdisk": 100 * GIB,
|
||||
"uptime": 86400,
|
||||
"level": "",
|
||||
"cgroup-mode": 2,
|
||||
}
|
||||
|
||||
|
||||
def _storage_row(
|
||||
node: str,
|
||||
storage: str,
|
||||
plugintype: str,
|
||||
shared: int,
|
||||
content: str,
|
||||
maxdisk: int,
|
||||
disk: int,
|
||||
status: str = "available",
|
||||
) -> dict[str, Any]:
|
||||
return {
|
||||
"id": f"storage/{node}/{storage}",
|
||||
"type": "storage",
|
||||
"node": node,
|
||||
"storage": storage,
|
||||
"plugintype": plugintype,
|
||||
"shared": shared,
|
||||
"content": content,
|
||||
"maxdisk": maxdisk,
|
||||
"disk": disk,
|
||||
"status": status,
|
||||
}
|
||||
|
||||
|
||||
CLUSTER_RESOURCES = [
|
||||
_node_row("pve1", 32, 0.25, 128 * GIB, 64 * GIB),
|
||||
_node_row("pve2", 16, 0.5, 64 * GIB, 16 * GIB),
|
||||
# An offline node keeps a row, with what PVE last knew of it.
|
||||
{"id": "node/pve3", "type": "node", "node": "pve3", "status": "offline", "maxcpu": 8},
|
||||
_storage_row("pve1", "local-lvm", "lvmthin", 0, "rootdir,images", 400 * GIB, 100 * GIB),
|
||||
_storage_row("pve2", "local-lvm", "lvmthin", 0, "rootdir,images", 200 * GIB, 50 * GIB),
|
||||
_storage_row("pve3", "local-lvm", "lvmthin", 0, "rootdir,images", 0, 0, status="unknown"),
|
||||
_storage_row("pve1", "ceph-vm", "rbd", 1, "images,rootdir", 3000 * GIB, 1000 * GIB),
|
||||
_storage_row("pve2", "ceph-vm", "rbd", 1, "images,rootdir", 3000 * GIB, 1000 * GIB),
|
||||
_storage_row("pve1", "nfs-iso", "nfs", 1, "iso,vztmpl,snippets", 1000 * GIB, 10 * GIB),
|
||||
# The NFS server cannot be reached from pve2.
|
||||
_storage_row("pve2", "nfs-iso", "nfs", 1, "iso,vztmpl,snippets", 0, 0, status="unavailable"),
|
||||
{"id": "qemu/100", "type": "qemu", "vmid": 100, "node": "pve1", "status": "running"},
|
||||
{"id": "sdn/pve1/localnetwork", "type": "sdn", "sdn": "localnetwork", "node": "pve1"},
|
||||
]
|
||||
|
||||
|
||||
def _cpuinfo(cores: int, sockets: int, model: str, flags: str) -> dict[str, Any]:
|
||||
return {
|
||||
"cpuinfo": {
|
||||
"cores": cores,
|
||||
"sockets": sockets,
|
||||
"cpus": cores * sockets * 2,
|
||||
"model": model,
|
||||
"flags": flags,
|
||||
"mhz": "2100.000",
|
||||
"hvm": "1",
|
||||
"user_hz": 100,
|
||||
},
|
||||
"memory": {"total": 128 * GIB, "used": 64 * GIB, "free": 64 * GIB},
|
||||
"uptime": 86400,
|
||||
"pveversion": "pve-manager/8.2.4/faa83925c9641325",
|
||||
}
|
||||
|
||||
|
||||
NODE_STATUS = {
|
||||
"pve1": _cpuinfo(8, 2, "Intel(R) Xeon(R) Silver 4110 CPU @ 2.10GHz", "fpu vme sse4_2 avx2"),
|
||||
"pve2": _cpuinfo(8, 1, "AMD EPYC 7302P 16-Core Processor", "fpu vme sse4_2"),
|
||||
}
|
||||
|
||||
NODE_NETWORK = {
|
||||
"pve1": [
|
||||
{"iface": "eno1", "type": "eth", "active": 1},
|
||||
{
|
||||
"iface": "vmbr0",
|
||||
"type": "bridge",
|
||||
"active": 1,
|
||||
"bridge_ports": "eno1",
|
||||
"bridge_vlan_aware": 1,
|
||||
},
|
||||
{"iface": "vmbr9", "type": "bridge", "active": 1, "bridge_ports": "none"},
|
||||
],
|
||||
"pve2": [
|
||||
{"iface": "eno1", "type": "eth", "active": 1},
|
||||
# Same name, but not VLAN aware on this node.
|
||||
{"iface": "vmbr0", "type": "bridge", "active": 1, "bridge_ports": "eno1"},
|
||||
{"iface": "vmbr1", "type": "OVSBridge", "active": 1, "ovs_ports": ""},
|
||||
],
|
||||
}
|
||||
|
||||
SDN_ZONES = [
|
||||
{"zone": "edge", "type": "vlan", "bridge": "vmbr0", "nodes": "pve2", "digest": "x"},
|
||||
{"zone": "fabric", "type": "vxlan", "peers": "10.0.0.1,10.0.0.2", "digest": "x"},
|
||||
]
|
||||
|
||||
SDN_VNETS = [
|
||||
{"vnet": "dmz", "zone": "edge", "tag": 30, "type": "vnet", "digest": "x"},
|
||||
{"vnet": "overlay", "zone": "fabric", "tag": 100000, "type": "vnet", "digest": "x"},
|
||||
]
|
||||
|
||||
HA_MANAGER_STATUS = {
|
||||
"manager_status": {
|
||||
"master_node": "pve1",
|
||||
"node_status": {"pve1": "online", "pve2": "maintenance", "pve3": "unknown"},
|
||||
"service_status": {},
|
||||
"timestamp": 1730000000,
|
||||
},
|
||||
"quorum": {"node": "pve1", "quorate": "1"},
|
||||
}
|
||||
|
||||
|
||||
def _api(
|
||||
cluster_status: list[dict[str, Any]] | None = None,
|
||||
resources: list[dict[str, Any]] | None = None,
|
||||
node_status: dict[str, Any] | None = None,
|
||||
node_network: dict[str, Any] | None = None,
|
||||
zones: list[dict[str, Any]] | None = None,
|
||||
vnets: list[dict[str, Any]] | None = None,
|
||||
ha: dict[str, Any] | Exception | None = None,
|
||||
) -> MagicMock:
|
||||
api = _build_mock_api(cluster_status=cluster_status or CLUSTER_STATUS)
|
||||
statuses = NODE_STATUS if node_status is None else node_status
|
||||
networks = NODE_NETWORK if node_network is None else node_network
|
||||
nodes: dict[str, MagicMock] = {}
|
||||
|
||||
def node_res(name: str) -> MagicMock:
|
||||
if name not in nodes:
|
||||
res = MagicMock()
|
||||
if name in statuses:
|
||||
res.status.get.return_value = statuses[name]
|
||||
else:
|
||||
res.status.get.side_effect = ConnectionError(f"{name} unreachable")
|
||||
res.network.get.return_value = networks.get(name, [])
|
||||
nodes[name] = res
|
||||
return nodes[name]
|
||||
|
||||
api.nodes.side_effect = node_res
|
||||
api.nodes.return_value = None
|
||||
api.cluster.resources.get.return_value = CLUSTER_RESOURCES if resources is None else resources
|
||||
api.cluster.sdn.zones.get.return_value = SDN_ZONES if zones is None else zones
|
||||
api.cluster.sdn.vnets.get.return_value = SDN_VNETS if vnets is None else vnets
|
||||
manager = api.cluster.ha.status.manager_status.get
|
||||
if isinstance(ha, Exception):
|
||||
manager.side_effect = ha
|
||||
else:
|
||||
manager.return_value = HA_MANAGER_STATUS if ha is None else ha
|
||||
return api
|
||||
|
||||
|
||||
def _driver(api: MagicMock, node: str = "pve1") -> ProxmoxDriver:
|
||||
drv = ProxmoxDriver(
|
||||
hostname=f"{node}.example.com",
|
||||
username="root",
|
||||
password="secret",
|
||||
optional_args={"verify_ssl": False, "node": node},
|
||||
)
|
||||
with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=api):
|
||||
drv.open()
|
||||
return drv
|
||||
|
||||
|
||||
def _cluster(**kwargs: Any) -> dict[str, Any]:
|
||||
clusters = _driver(_api(**kwargs)).get_clusters()
|
||||
assert len(clusters) == 1
|
||||
return dict(clusters[0])
|
||||
|
||||
|
||||
def _by_name(items: list[Any]) -> dict[str, Any]:
|
||||
return {item["name"]: item for item in items}
|
||||
|
||||
|
||||
class TestCluster:
|
||||
def test_name_and_quorum(self):
|
||||
cluster = _cluster()
|
||||
assert cluster["id"] is None
|
||||
assert cluster["name"] == "lab"
|
||||
assert cluster["quorate"] is True
|
||||
|
||||
def test_lost_quorum(self):
|
||||
status = [dict(CLUSTER_STATUS[0], quorate=0), *CLUSTER_STATUS[1:]]
|
||||
assert _cluster(cluster_status=status)["quorate"] is False
|
||||
|
||||
def test_every_member_listed_in_cluster_order(self):
|
||||
assert [n["name"] for n in _cluster()["nodes"]] == ["pve1", "pve2", "pve3"]
|
||||
|
||||
|
||||
class TestNodes:
|
||||
def test_online_node(self):
|
||||
pve1 = _by_name(_cluster()["nodes"])["pve1"]
|
||||
assert pve1 == {
|
||||
"name": "pve1",
|
||||
"status": "online",
|
||||
"local": True,
|
||||
"maintenance": False,
|
||||
"cpu_threads": 32,
|
||||
"cpu_cores": 16,
|
||||
"cpu_model": "Intel(R) Xeon(R) Silver 4110 CPU @ 2.10GHz",
|
||||
"cpu_flags": ["fpu", "vme", "sse4_2", "avx2"],
|
||||
"cpu_usage": 0.25,
|
||||
"memory_total": 128 * GIB,
|
||||
"memory_used": 64 * GIB,
|
||||
}
|
||||
|
||||
def test_other_member_is_not_local(self):
|
||||
pve2 = _by_name(_cluster()["nodes"])["pve2"]
|
||||
assert pve2["local"] is False
|
||||
assert pve2["cpu_cores"] == 8
|
||||
assert pve2["cpu_threads"] == 16
|
||||
|
||||
def test_offline_node_is_listed_without_capacity(self):
|
||||
api = _api()
|
||||
pve3 = _by_name(_driver(api).get_clusters()[0]["nodes"])["pve3"]
|
||||
assert pve3["status"] == "offline"
|
||||
assert pve3["local"] is False
|
||||
for key in (
|
||||
"cpu_threads",
|
||||
"cpu_cores",
|
||||
"cpu_model",
|
||||
"cpu_flags",
|
||||
"cpu_usage",
|
||||
"memory_total",
|
||||
"memory_used",
|
||||
):
|
||||
assert pve3[key] is None, key
|
||||
# An offline node is not asked: the request would only time out.
|
||||
assert all(call.args != ("pve3",) for call in api.nodes.call_args_list)
|
||||
|
||||
def test_local_falls_back_to_the_resolved_node(self):
|
||||
status = [dict(s, local=0) if s["type"] == "node" else s for s in CLUSTER_STATUS]
|
||||
nodes = _by_name(
|
||||
_driver(_api(cluster_status=status), node="pve2").get_clusters()[0]["nodes"]
|
||||
)
|
||||
assert nodes["pve2"]["local"] is True
|
||||
assert nodes["pve1"]["local"] is False
|
||||
|
||||
|
||||
class TestStandalone:
|
||||
STATUS = [
|
||||
{
|
||||
"type": "node",
|
||||
"id": "node/pve1",
|
||||
"name": "pve1",
|
||||
"nodeid": 0,
|
||||
"ip": "10.0.0.1",
|
||||
"local": 1,
|
||||
"online": 1,
|
||||
"level": "",
|
||||
}
|
||||
]
|
||||
RESOURCES = [
|
||||
_node_row("pve1", 32, 0.25, 128 * GIB, 64 * GIB),
|
||||
_storage_row("pve1", "local", "dir", 0, "iso,vztmpl,backup", 100 * GIB, 5 * GIB),
|
||||
]
|
||||
|
||||
def test_a_cluster_of_one_without_name(self):
|
||||
cluster = _cluster(cluster_status=self.STATUS, resources=self.RESOURCES, ha=RuntimeError())
|
||||
assert cluster["id"] is None
|
||||
assert cluster["name"] is None
|
||||
assert cluster["quorate"] is None
|
||||
[node] = cluster["nodes"]
|
||||
assert node["name"] == "pve1"
|
||||
assert node["status"] == "online"
|
||||
assert node["local"] is True
|
||||
assert node["cpu_cores"] == 16
|
||||
assert node["memory_total"] == 128 * GIB
|
||||
assert _by_name(cluster["storages"])["local"]["nodes"] == ["pve1"]
|
||||
|
||||
|
||||
class TestStorages:
|
||||
def test_local_storage_grouped_over_its_nodes(self):
|
||||
lvm = _by_name(_cluster()["storages"])["local-lvm"]
|
||||
assert lvm == {
|
||||
"name": "local-lvm",
|
||||
"type": "lvmthin",
|
||||
"shared": False,
|
||||
"content": ["rootdir", "images"],
|
||||
# pve3 is offline: its storage is "unknown", not available.
|
||||
"nodes": ["pve1", "pve2"],
|
||||
# Separate disks on each node: their sizes add up.
|
||||
"total": 600 * GIB,
|
||||
"used": 150 * GIB,
|
||||
}
|
||||
|
||||
def test_shared_storage_counted_once(self):
|
||||
ceph = _by_name(_cluster()["storages"])["ceph-vm"]
|
||||
assert ceph["shared"] is True
|
||||
assert ceph["type"] == "rbd"
|
||||
assert ceph["nodes"] == ["pve1", "pve2"]
|
||||
assert ceph["total"] == 3000 * GIB
|
||||
assert ceph["used"] == 1000 * GIB
|
||||
|
||||
def test_storage_unavailable_on_a_node_leaves_it_out(self):
|
||||
nfs = _by_name(_cluster()["storages"])["nfs-iso"]
|
||||
assert nfs["nodes"] == ["pve1"]
|
||||
assert nfs["content"] == ["iso", "vztmpl", "snippets"]
|
||||
assert nfs["total"] == 1000 * GIB
|
||||
|
||||
def test_storage_available_nowhere_has_no_size(self):
|
||||
resources = [
|
||||
*CLUSTER_RESOURCES[:3],
|
||||
_storage_row("pve1", "nas", "cifs", 1, "backup", 0, 0, status="unavailable"),
|
||||
]
|
||||
nas = _by_name(_cluster(resources=resources)["storages"])["nas"]
|
||||
assert nas["nodes"] == []
|
||||
assert nas["total"] is None
|
||||
assert nas["used"] is None
|
||||
|
||||
|
||||
class TestNetworks:
|
||||
def test_bridge_merged_across_nodes_vlan_aware_only_if_everywhere(self):
|
||||
vmbr0 = _by_name(_cluster()["networks"])["vmbr0"]
|
||||
assert vmbr0 == {
|
||||
"name": "vmbr0",
|
||||
"kind": "bridge",
|
||||
"vlan_aware": False,
|
||||
"nodes": ["pve1", "pve2"],
|
||||
}
|
||||
|
||||
def test_bridge_on_one_node(self):
|
||||
networks = _by_name(_cluster()["networks"])
|
||||
assert networks["vmbr9"]["nodes"] == ["pve1"]
|
||||
assert networks["vmbr1"] == {
|
||||
"name": "vmbr1",
|
||||
"kind": "bridge",
|
||||
"vlan_aware": True,
|
||||
"nodes": ["pve2"],
|
||||
}
|
||||
|
||||
def test_vlan_aware_on_every_node(self):
|
||||
network = {
|
||||
n: [dict(i, bridge_vlan_aware=1) for i in NODE_NETWORK["pve1"]] for n in NODE_NETWORK
|
||||
}
|
||||
assert _by_name(_cluster(node_network=network)["networks"])["vmbr0"]["vlan_aware"] is True
|
||||
|
||||
def test_vnet_on_its_zones_nodes(self):
|
||||
dmz = _by_name(_cluster()["networks"])["dmz"]
|
||||
assert dmz == {
|
||||
"name": "dmz",
|
||||
"kind": "vnet",
|
||||
"vlan_aware": False,
|
||||
"fixed_vlan_tag": 30,
|
||||
"nodes": ["pve2"],
|
||||
}
|
||||
|
||||
def test_vnet_of_a_zone_without_nodes_is_everywhere(self):
|
||||
overlay = _by_name(_cluster()["networks"])["overlay"]
|
||||
assert overlay["nodes"] == ["pve1", "pve2", "pve3"]
|
||||
assert overlay["fixed_vlan_tag"] == 100000
|
||||
|
||||
def test_get_network_targets_unchanged_for_the_own_node(self):
|
||||
targets = _by_name(_driver(_api()).get_network_targets())
|
||||
assert targets["vmbr0"] == {
|
||||
"name": "vmbr0",
|
||||
"kind": "bridge",
|
||||
"vlan_aware": True,
|
||||
"uplinks": [["eno1"]],
|
||||
}
|
||||
assert targets["dmz"]["fixed_vlan_tag"] == 30
|
||||
|
||||
|
||||
class TestMaintenance:
|
||||
def test_ha_reports_maintenance(self):
|
||||
nodes = _by_name(_cluster()["nodes"])
|
||||
assert nodes["pve1"]["maintenance"] is False
|
||||
assert nodes["pve2"]["maintenance"] is True
|
||||
|
||||
def test_ha_unreadable_leaves_maintenance_unknown(self):
|
||||
nodes = _cluster(ha=RuntimeError("403 Forbidden: Permission check failed"))["nodes"]
|
||||
assert [n["maintenance"] for n in nodes] == [None, None, None]
|
||||
|
||||
def test_node_the_ha_manager_does_not_track_is_unknown(self):
|
||||
ha = {"manager_status": {"node_status": {"pve1": "online"}}, "quorum": {}}
|
||||
nodes = _by_name(_cluster(ha=ha)["nodes"])
|
||||
assert nodes["pve1"]["maintenance"] is False
|
||||
assert nodes["pve2"]["maintenance"] is None
|
||||
|
||||
|
||||
class TestReadFailures:
|
||||
def test_cluster_resources_unreadable_raises(self):
|
||||
api = _api()
|
||||
api.cluster.resources.get.side_effect = ConnectionError("pveproxy down")
|
||||
with pytest.raises(ConnectionError):
|
||||
_driver(api).get_clusters()
|
||||
|
||||
def test_cluster_status_unreadable_raises(self):
|
||||
api = _api()
|
||||
drv = _driver(api)
|
||||
api.cluster.status.get.side_effect = ConnectionError("pveproxy down")
|
||||
with pytest.raises(ConnectionError):
|
||||
drv.get_clusters()
|
||||
|
||||
def test_own_node_status_unreadable_raises(self):
|
||||
status = {"pve2": NODE_STATUS["pve2"]}
|
||||
with pytest.raises(ConnectionError):
|
||||
_driver(_api(node_status=status)).get_clusters()
|
||||
|
||||
def test_other_node_status_unreadable_leaves_its_cpu_unknown(self):
|
||||
status = {"pve1": NODE_STATUS["pve1"]}
|
||||
pve2 = _by_name(_cluster(node_status=status)["nodes"])["pve2"]
|
||||
assert pve2["status"] == "online"
|
||||
assert pve2["cpu_cores"] is None
|
||||
assert pve2["cpu_model"] is None
|
||||
assert pve2["cpu_flags"] is None
|
||||
# What /cluster/resources says still stands.
|
||||
assert pve2["cpu_threads"] == 16
|
||||
|
||||
|
||||
class TestCreateOnAnotherNode:
|
||||
def test_other_node_raises_before_anything_is_created(self):
|
||||
api = _api()
|
||||
drv = _driver(api)
|
||||
with pytest.raises(ValueError, match="pve2"):
|
||||
drv.create_vm_from_cloud_init(
|
||||
"vm1",
|
||||
image_url="https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2",
|
||||
cpu=2,
|
||||
memory=2048,
|
||||
nics=[{"bridge": "vmbr0"}],
|
||||
cloud_init_config={},
|
||||
node="pve2",
|
||||
)
|
||||
api.cluster.nextid.get.assert_not_called()
|
||||
for res in [api.nodes(n) for n in ("pve1", "pve2")]:
|
||||
assert not [c for c in res.mock_calls if "post" in c[0]]
|
||||
Reference in new issue
Block a user