mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 22:06:31 +00:00
fix(mesh): keep every member on the address it already holds
The offset came from the spoke's position in a sorted list, so a host that sorts earlier renumbered every member behind it. `wg syncconf` carries keys and peers onto a live interface and never its address, so a renumbered member keeps answering on the old address while the peers and the inventory have moved to the new one -- and the address it gave up can be handed to a new member while the old owner still holds it. The planner now reuses the addresses already on disk and allocates only an unseen member, from the lowest free offset. A value outside the subnet, a duplicate, and the hub's own offset are refused, so a mirrored host_vars cannot hand a spoke the hub's address. Rotation keeps addresses: it replaces identities, not addresses. The routing assertions re-planned the mesh and compared the result with itself, so a writer that stored the mesh subnet in place of the routed range would have passed; they now read the entry off disk, which is what the role reads.
This commit is contained in:
7 files changed
+246
-10
No files matched your search
@@ -8,6 +8,7 @@ import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from cli.administration.inventory.mesh.__main__ import main
|
||||
from utils.cache.yaml import load_yaml_any
|
||||
|
||||
from .test_write import (
|
||||
BACKUP_INVENTORY,
|
||||
@@ -18,6 +19,7 @@ from .test_write import (
|
||||
)
|
||||
|
||||
CONTROLLER = "deploy-controller"
|
||||
SWARM_HOSTS = ("swarm-mgr-01", "swarm-wrk-01", "swarm-wrk-02")
|
||||
|
||||
|
||||
class TestMeshCli(unittest.TestCase):
|
||||
@@ -126,6 +128,45 @@ class TestMeshCli(unittest.TestCase):
|
||||
self.assertEqual(1, len(set(own)))
|
||||
self.assertIn(own[0], spoke_peers)
|
||||
|
||||
def _address(self, host: str, mesh_name: str) -> str:
|
||||
document = load_yaml_any(str(self.host_vars / f"{host}.yml"))
|
||||
return document["applications"]["svc-net-wireguard"]["meshes"][mesh_name][
|
||||
"address"
|
||||
]
|
||||
|
||||
def test_growing_the_cluster_leaves_every_member_on_its_address(self):
|
||||
"""A node added later must not renumber the nodes already up.
|
||||
|
||||
`wg syncconf` never moves a live interface's address, so a renumbered
|
||||
member answers on the old one while the inventory has moved to the
|
||||
new one -- and the next pass cannot reach it.
|
||||
"""
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
before = {host: self._address(host, "swarm") for host in SWARM_HOSTS}
|
||||
|
||||
(self.host_vars / "alpha-wrk-00.yml").write_text("---\n", encoding="utf-8")
|
||||
self.cluster.write_text(
|
||||
CLUSTER_INVENTORY.replace(
|
||||
" swarm-wrk-01: {}",
|
||||
" alpha-wrk-00: {}\n swarm-wrk-01: {}",
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
|
||||
for host, address in before.items():
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(self._address(host, "swarm"), address)
|
||||
self.assertNotIn(self._address("alpha-wrk-00", "swarm"), set(before.values()))
|
||||
|
||||
def test_a_rotation_leaves_every_member_on_its_address(self):
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
before = {host: self._address(host, "swarm") for host in SWARM_HOSTS}
|
||||
self.assertEqual(main(self._argv("--rotate")), 0)
|
||||
for host, address in before.items():
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(self._address(host, "swarm"), address)
|
||||
|
||||
def test_a_rerun_is_a_no_op(self):
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
before = {host: self._text(host) for host in HOSTS}
|
||||
|
||||
@@ -96,6 +96,61 @@ class TestAddressing(unittest.TestCase):
|
||||
self.assertEqual(swarm & data, set())
|
||||
|
||||
|
||||
class TestAddressPersistence(unittest.TestCase):
|
||||
"""A member keeps the address it already answers on.
|
||||
|
||||
`wg syncconf` carries keys and peers onto a live interface and never its
|
||||
address, so a member whose address is recomputed keeps answering on the
|
||||
old one while the rest of the mesh and the inventory have moved to the
|
||||
new one. The deploy then loses exactly the hosts that were already up.
|
||||
"""
|
||||
|
||||
def _held(self, spec, groups) -> dict[str, str]:
|
||||
return {m.host: m.address for m in plan_mesh(spec, groups).members}
|
||||
|
||||
def test_a_member_that_sorts_first_does_not_renumber_the_others(self):
|
||||
before = self._held(SWARM, GROUPS)
|
||||
grown = dict(GROUPS)
|
||||
grown["svc-swarm-node"] = ["alpha-wrk-00", *GROUPS["svc-swarm-node"]]
|
||||
|
||||
mesh = plan_mesh(SWARM, grown, addresses=before)
|
||||
after = {m.host: m.address for m in mesh.members}
|
||||
for host, address in before.items():
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(after[host], address)
|
||||
self.assertNotIn(after["alpha-wrk-00"], set(before.values()))
|
||||
|
||||
def test_rotation_replaces_identities_and_keeps_addresses(self):
|
||||
before = self._held(SWARM, GROUPS)
|
||||
mesh = plan_mesh(SWARM, GROUPS, rotate=True, addresses=before)
|
||||
self.assertEqual({m.host: m.address for m in mesh.members}, before)
|
||||
|
||||
def test_a_stored_address_outside_the_subnet_is_replaced(self):
|
||||
mesh = plan_mesh(SWARM, GROUPS, addresses={"swarm-wrk-01": "10.99.0.50"})
|
||||
address = mesh.member("swarm-wrk-01").address
|
||||
self.assertTrue(address.startswith("10.100.0."))
|
||||
|
||||
def test_a_spoke_never_adopts_the_hub_address(self):
|
||||
"""The mirror leaves every host holding the hub's entry.
|
||||
|
||||
A spoke that reused what it found there would bring up an interface
|
||||
claiming the hub's address and blackhole the mesh it belongs to.
|
||||
"""
|
||||
hub = plan_mesh(SWARM, GROUPS).hub.address
|
||||
mesh = plan_mesh(SWARM, GROUPS, addresses={"swarm-wrk-01": hub})
|
||||
self.assertNotEqual(mesh.member("swarm-wrk-01").address, hub)
|
||||
|
||||
def test_two_members_claiming_one_address_do_not_both_keep_it(self):
|
||||
taken = plan_mesh(SWARM, GROUPS).member("swarm-wrk-01").address
|
||||
mesh = plan_mesh(
|
||||
SWARM,
|
||||
GROUPS,
|
||||
addresses={"swarm-wrk-01": taken, "swarm-wrk-02": taken},
|
||||
)
|
||||
addresses = [m.address for m in mesh.members]
|
||||
self.assertEqual(len(addresses), len(set(addresses)))
|
||||
|
||||
|
||||
class TestPeering(unittest.TestCase):
|
||||
def test_the_hub_peers_with_every_spoke(self):
|
||||
mesh = plan_mesh(SWARM, GROUPS)
|
||||
|
||||
@@ -14,6 +14,7 @@ from pathlib import Path
|
||||
from cli.administration.inventory.mesh.inventory import groups_of, specs_of
|
||||
from cli.administration.inventory.mesh.plan import plan_mesh
|
||||
from cli.administration.inventory.mesh.write import (
|
||||
existing_addresses,
|
||||
existing_public_keys,
|
||||
private_key_name,
|
||||
prune_foreign_meshes,
|
||||
@@ -97,6 +98,16 @@ class MeshOnDisk(unittest.TestCase):
|
||||
.get("meshes", {})
|
||||
)
|
||||
|
||||
def _entry(self, host: str, mesh_name: str) -> dict:
|
||||
"""The mesh block as the role will read it back off disk."""
|
||||
document = load_yaml_any(str(self.host_vars / f"{host}.yml"))
|
||||
return (
|
||||
document.get("applications", {})
|
||||
.get("svc-net-wireguard", {})
|
||||
.get("meshes", {})
|
||||
.get(mesh_name, {})
|
||||
)
|
||||
|
||||
def _write_all(self, *, rotate: bool = False) -> None:
|
||||
for spec in self.specs:
|
||||
mesh = plan_mesh(
|
||||
@@ -178,15 +189,22 @@ class TestMeshShapeOnDisk(MeshOnDisk, unittest.TestCase):
|
||||
with no route to the NFS server, and the mount fails while every tunnel
|
||||
still reports a healthy handshake -- so the symptom points at storage
|
||||
rather than at routing.
|
||||
|
||||
Asserted against the serialized entry, not against a second plan: the
|
||||
role reads the file, so a writer that stored the mesh subnet in place
|
||||
of the routed range would satisfy a re-planned comparison.
|
||||
"""
|
||||
self._write_all()
|
||||
for spec in self.specs:
|
||||
mesh = plan_mesh(spec, self.groups)
|
||||
self.assertEqual(spec.routed_range, "10.100.0.0/16")
|
||||
for spoke in mesh.spokes:
|
||||
with self.subTest(mesh=spec.name, spoke=spoke.host):
|
||||
peers = mesh.peers_of(spoke.host)
|
||||
self.assertEqual([p.host for p in peers], [mesh.hub.host])
|
||||
self.assertEqual(spec.routed_range, "10.100.0.0/16")
|
||||
peers = self._entry(spoke.host, spec.name)["peers"]
|
||||
self.assertEqual([p["host"] for p in peers], [mesh.hub.host])
|
||||
self.assertEqual(
|
||||
[p["allowed_ips"] for p in peers], [spec.routed_range]
|
||||
)
|
||||
|
||||
def test_the_hub_pins_each_spoke_to_a_single_address(self):
|
||||
"""The inverse of the spoke rule.
|
||||
@@ -194,12 +212,44 @@ class TestMeshShapeOnDisk(MeshOnDisk, unittest.TestCase):
|
||||
A hub that routed the pool to one spoke would blackhole every other
|
||||
member behind it.
|
||||
"""
|
||||
self._write_all()
|
||||
for spec in self.specs:
|
||||
hub = plan_mesh(spec, self.groups).hub.host
|
||||
for peer in self._entry(hub, spec.name)["peers"]:
|
||||
with self.subTest(mesh=spec.name, peer=peer["host"]):
|
||||
self.assertFalse(peer["is_hub"])
|
||||
self.assertEqual(peer["allowed_ips"], peer["address"] + "/32")
|
||||
|
||||
|
||||
class TestStoredAddresses(MeshOnDisk, unittest.TestCase):
|
||||
"""What the planner is allowed to treat as an address already in use."""
|
||||
|
||||
def test_every_written_address_is_read_back(self):
|
||||
self._write_all()
|
||||
for spec in self.specs:
|
||||
mesh = plan_mesh(spec, self.groups)
|
||||
for peer in mesh.peers_of(mesh.hub.host):
|
||||
with self.subTest(mesh=spec.name, peer=peer.host):
|
||||
self.assertFalse(peer.is_hub)
|
||||
with self.subTest(mesh=spec.name):
|
||||
self.assertEqual(
|
||||
existing_addresses(self.host_vars, list(HOSTS), spec.name),
|
||||
{m.host: m.address for m in mesh.members},
|
||||
)
|
||||
|
||||
def test_a_host_with_no_entry_is_absent_rather_than_empty(self):
|
||||
self._write_all()
|
||||
stored = existing_addresses(self.host_vars, list(HOSTS), "data")
|
||||
self.assertNotIn("swarm-wrk-01", stored)
|
||||
|
||||
def test_a_mirrored_entry_is_not_read_as_the_hosts_own_address(self):
|
||||
"""The mirror hands every node the hub's entry.
|
||||
|
||||
Reusing what it finds there would put the hub's address on a worker,
|
||||
which is the same failure the key reader already guards against.
|
||||
"""
|
||||
self._write_all()
|
||||
hub_text = self._text("swarm-mgr-01")
|
||||
(self.host_vars / "swarm-wrk-01.yml").write_text(hub_text, encoding="utf-8")
|
||||
stored = existing_addresses(self.host_vars, list(HOSTS), "swarm")
|
||||
self.assertNotIn("swarm-wrk-01", stored)
|
||||
|
||||
|
||||
class TestIdempotence(MeshOnDisk, unittest.TestCase):
|
||||
|
||||
Reference in new issue
Block a user