fix: do not spread unplaced VMs once a VM of the cluster is pinned

As soon as any VM of a cluster (in any status) is assigned to one of its
hosts, the placement is treated as managed by hand: VMs without a host
are no longer spread over the hosts and only count towards the cluster
totals. Cluster panel and host tab say which case applies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-10-29 11:12:15 +01:00
1 parent afa15eb46a
commit 8aa8d3fe1e
6 files changed
+65 -23

No files matched your search

+3 -1
View File
@@ -475,7 +475,9 @@ VMs that are assigned only to a cluster, without a fixed host (*Device*), are
spread over the cluster hosts in proportion to their size: vCPUs by the number
of threads, memory by the host RAM. A host with twice the threads carries twice
the vCPUs of these VMs. If no host of the cluster has CPU & RAM recorded, the
load is split evenly. The cluster table and the CPU & RAM tab of each host
load is split evenly. As soon as a single VM of the cluster (in any status) is
assigned to one of its hosts, the placement counts as managed by hand: nothing
is spread, and the VMs without a host only count towards the cluster totals. The cluster table and the CPU & RAM tab of each host
include this share and mark it as such; the cluster totals count every VM once. VM memory is read in MB as NetBox stores it.
**CSV import.** **Hardware > CPU & RAM > Import** accepts `device` (name),
+13 -2
View File
@@ -122,6 +122,10 @@ class Capacity:
unplaced_vm_count: int = 0
# True for one host of a cluster: vcpus/vm_memory_gb include its share of the unplaced VMs.
host_share: bool = False
# Some VM is pinned to a host, so VMs without a host are not spread automatically.
manual_placement: bool = False
# VMs without a host were spread over the hosts.
distributed: bool = False
@property
def known_hosts(self):
@@ -200,12 +204,16 @@ def build_capacity(hosts, vms):
vms: iterable of (host key or None, vcpus, memory in MB, status).
VMs without a fixed host (only a cluster) are spread over the hosts in proportion to
their size: vCPUs by threads, memory by RAM.
their size: vCPUs by threads, memory by RAM. As soon as any VM of the cluster (in any
status) is pinned to one of its hosts, the placement is managed by hand and nothing is
spread; the unplaced VMs then only count towards the cluster totals.
"""
capacity = Capacity(hosts=list(hosts))
by_key = {host.key: host for host in capacity.hosts}
unplaced_vcpus = unplaced_memory_gb = 0
for host_key, vcpus, memory_mb, status in vms:
if host_key in by_key:
capacity.manual_placement = True
if status in INACTIVE_VM_STATUSES:
continue
vcpus = float(vcpus or 0)
@@ -223,7 +231,8 @@ def build_capacity(hosts, vms):
host.vcpus += vcpus
host.vm_memory_gb += memory_gb
if capacity.hosts and capacity.unplaced_vm_count:
if capacity.hosts and capacity.unplaced_vm_count and not capacity.manual_placement:
capacity.distributed = True
for host, weight in zip(capacity.hosts, _weights(capacity.hosts, "threads"), strict=True):
host.shared_vcpus = unplaced_vcpus * weight
for host, weight in zip(capacity.hosts, _weights(capacity.hosts, "memory_gb"), strict=True):
@@ -240,4 +249,6 @@ def host_capacity(cluster_capacity, host):
vm_memory_gb=host.total_vm_memory_gb,
unplaced_vm_count=cluster_capacity.unplaced_vm_count,
host_share=True,
manual_placement=cluster_capacity.manual_placement,
distributed=cluster_capacity.distributed,
)
@@ -45,8 +45,15 @@
</div>
</div>
<div class="card-footer small text-muted">
Gezählt werden alle VMs außer „Offline“ und „Außer Betrieb“. VMs ohne festen Host werden anteilig nach Größe
auf die Hosts verteilt: vCPUs nach Threads, RAM nach Host-RAM. Ist bei keinem Host CPU/RAM erfasst, zu gleichen Teilen.
Gezählt werden alle VMs außer „Offline“ und „Außer Betrieb“.
{% if capacity.distributed %}
VMs ohne festen Host sind anteilig nach Größe auf die Hosts verteilt: vCPUs nach Threads, RAM nach Host-RAM
(zu gleichen Teilen, wenn bei keinem Host CPU/RAM erfasst ist).
{% elif capacity.unplaced_vm_count %}
{{ capacity.unplaced_vm_count }} VM{{ capacity.unplaced_vm_count|pluralize:",s" }} ohne festen Host
{{ capacity.unplaced_vm_count|pluralize:"zählt,zählen" }} nur zur Cluster-Summe: Im Cluster sind VMs fest Hosts
zugewiesen, deshalb wird nicht automatisch verteilt.
{% endif %}
</div>
{% endif %}
</div>
@@ -64,7 +64,7 @@
{% else %}
<div class="card-body text-muted">Keine VMs mit diesem Gerät als Host.</div>
{% endif %}
{% if capacity.host_share and capacity.unplaced_vm_count %}
{% if capacity.distributed and capacity.unplaced_vm_count %}
<div class="card-footer small text-muted">
Die Liste zeigt nur VMs, deren Gerät dieser Host ist. {{ capacity.unplaced_vm_count }} weitere VM{{ capacity.unplaced_vm_count|pluralize:",s" }}
des Clusters ohne festen Host {{ capacity.unplaced_vm_count|pluralize:"ist,sind" }} anteilig in der Auslastung enthalten.
@@ -14,12 +14,18 @@
<th scope="row">VMs</th>
<td>
{{ capacity.vm_count }} VM{{ capacity.vm_count|pluralize:",s" }} · {{ capacity.vcpus|floatformat:"-1" }} vCPUs · {{ vm_memory }} RAM
{% if capacity.host_share and capacity.unplaced_vm_count %}
{% if capacity.unplaced_vm_count %}
<div class="small text-muted">
inkl. anteiliger Last aus {{ capacity.unplaced_vm_count }} Cluster-VM{{ capacity.unplaced_vm_count|pluralize:",s" }} ohne festen Host
{% if capacity.host_share and capacity.distributed %}
inkl. anteiliger Last aus {{ capacity.unplaced_vm_count }} Cluster-VM{{ capacity.unplaced_vm_count|pluralize:",s" }} ohne festen Host
{% elif capacity.host_share %}
{{ capacity.unplaced_vm_count }} Cluster-VM{{ capacity.unplaced_vm_count|pluralize:",s" }} ohne festen Host nicht enthalten, weil im Cluster VMs fest Hosts zugewiesen sind
{% elif capacity.distributed %}
davon {{ capacity.unplaced_vm_count }} ohne festen Host, anteilig nach Größe auf die Hosts verteilt
{% else %}
davon {{ capacity.unplaced_vm_count }} ohne festen Host, nicht auf die Hosts verteilt (es sind VMs fest zugewiesen)
{% endif %}
</div>
{% elif capacity.unplaced_vm_count %}
<div class="small text-muted">davon {{ capacity.unplaced_vm_count }} ohne festen Host, anteilig nach Größe auf die Hosts verteilt</div>
{% endif %}
</td>
</tr>
+29 -13
View File
@@ -124,19 +124,35 @@ class UnplacedVMTest(SimpleTestCase):
hardware.Host("small", cores=16, threads=32, memory_gb=256, key=1),
hardware.Host("big", cores=48, threads=96, memory_gb=768, key=2),
]
vms = [(None, 16, 100 * 1024, "active"), (1, 4, 8 * 1024, "active")]
vms = [(None, 16, 100 * 1024, "active"), (None, 4, 8 * 1024, "active")]
capacity = hardware.build_capacity(hosts, vms)
small, big = capacity.hosts
self.assertEqual(capacity.unplaced_vm_count, 1)
self.assertAlmostEqual(small.shared_vcpus, 4)
self.assertAlmostEqual(big.shared_vcpus, 12)
self.assertAlmostEqual(small.shared_memory_gb, 25)
self.assertAlmostEqual(big.shared_memory_gb, 75)
self.assertAlmostEqual(small.total_vcpus, 8)
self.assertAlmostEqual(small.total_vm_memory_gb, 33)
self.assertTrue(capacity.distributed)
self.assertEqual(capacity.unplaced_vm_count, 2)
self.assertAlmostEqual(small.shared_vcpus, 5)
self.assertAlmostEqual(big.shared_vcpus, 15)
self.assertAlmostEqual(small.shared_memory_gb, 27)
self.assertAlmostEqual(big.shared_memory_gb, 81)
# The cluster sums count every VM exactly once.
self.assertEqual((capacity.vcpus, capacity.vm_memory_gb), (20, 108))
def test_no_spreading_once_a_vm_is_pinned(self):
for status in ("active", "offline"):
with self.subTest(pinned_vm_status=status):
hosts = [
hardware.Host("a", cores=32, threads=64, memory_gb=512, key=1),
hardware.Host("b", cores=32, threads=64, memory_gb=512, key=2),
]
vms = [(None, 16, 64 * 1024, "active"), (2, 4, 8 * 1024, status)]
capacity = hardware.build_capacity(hosts, vms)
self.assertTrue(capacity.manual_placement)
self.assertFalse(capacity.distributed)
self.assertEqual([host.shared_vcpus for host in capacity.hosts], [0, 0])
self.assertEqual(capacity.unplaced_vm_count, 1)
self.assertEqual(capacity.vcpus, 20 if status == "active" else 16)
single = hardware.host_capacity(capacity, capacity.hosts[0])
self.assertEqual(single.vcpus, 0)
def test_equal_shares_without_recorded_hardware(self):
hosts = [hardware.Host("a", key=1), hardware.Host("b", key=2)]
capacity = hardware.build_capacity(hosts, [(None, 8, 16 * 1024, "active")])
@@ -148,12 +164,12 @@ class UnplacedVMTest(SimpleTestCase):
hardware.Host("a", cores=32, threads=64, memory_gb=512, key=1),
hardware.Host("b", cores=32, threads=64, memory_gb=512, key=2),
]
cluster = hardware.build_capacity(hosts, [(None, 32, 512 * 1024, "active"), (1, 8, 64 * 1024, "active")])
cluster = hardware.build_capacity(hosts, [(None, 32, 512 * 1024, "active")])
single = hardware.host_capacity(cluster, cluster.hosts[0])
self.assertTrue(single.host_share)
self.assertEqual((single.vm_count, single.vcpus, single.vm_memory_gb), (1, 24, 320))
self.assertEqual(single.memory_percent, 62)
self.assertEqual(single.vcpu_per_core, 0.75)
self.assertTrue(single.host_share and single.distributed)
self.assertEqual((single.vm_count, single.vcpus, single.vm_memory_gb), (0, 16, 256))
self.assertEqual(single.memory_percent, 50)
self.assertEqual(single.vcpu_per_core, 0.5)
def test_hosts_are_matched_by_key_not_name(self):
hosts = [hardware.Host("esx", key=1), hardware.Host("esx", key=2)]