Files
werewulf/scripts/B40-publish-cluster.py
T
ilker 49ba7ed874 hpcod: separate the cluster from the tenant that owns it
One tenant may run several HPC clusters at the same time. The code did not
allow for that: a single tenant_id drove the bridge, the subnet, the MAC
prefix, the VM names, the vendor-data filename and the public hostname. A
tenant asking for a second cluster while the first was running would have
been handed the same bridge, the same subnet and the same MAC prefix - and
Warewulf matches nodes by MAC, so the second cluster would have broken the
first rather than failing cleanly.

Everything that must not collide is now keyed on cluster_id, and tenant_id
survives as what it always meant: who owns this. Bridges are vmbrC###,
hosts c###-head and c###-node#, the portal is c###.hpc.example.org, and the
Slurm ClusterName is the cluster's - which matters for the accounting work
next, because records attributed to a shared name cannot be separated
afterwards.

The bound of 254 therefore applies to clusters running at one time, not to
tenants. It comes from the addressing and nothing else: cluster_id is the
third octet of 10.64.x.0/24 and one byte of the MAC. That is written down
in 20.5a rather than left to be rediscovered.

The registry keeps its shape but not its name: /etc/hpcod/clusters.yaml
with a clusters: key, /etc/hpcod/clusters/ for the pinned certificates,
-clusters and -cluster-domain on the portal, and the two scripts renamed to
match. A half-finished rename would have left the most-read file still
calling a cluster a tenant, which is the confusion this commit exists to
remove.

Verified on a cold provision: terraform 37s, self-configuration 43s, nodes
61s, 141s total; nodes named c002-node[1-2], ClusterName c002, bridge
vmbrC002, and the portal answering 401 at c002.hpc.example.org.

Not addressed here, and the more binding limit at the scale this is meant
for: a cluster cannot span Proxmox nodes. Its bridge is bridge-ports none,
a local L2 island, so two hypervisors would give two segments and PXE would
not cross. Proxmox SDN is the way out and is already enabled on hv01;
docs/20 anticipated it with vlan_tag in the allocation record.
2026-09-20 20:26:49 +02:00

126 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""Publish a cluster's portal at c<NNN>.hpc.example.org, or withdraw it.
The control plane routes cluster portals by subdomain, resolving the label
through a registry file (see hpcod/internal/httpapi/tenantproxy.go). This
script is what writes that file, because the head node cannot: it has no
access to the control plane's configuration, and should not have.
Clusters, not tenants: one tenant may run several at once, so the id here
is the cluster's.
Publishing copies the cluster's own certificate to the control plane, which
then pins it as the sole trust anchor for that one cluster. Re-running after
the cluster regenerates its certificate refreshes the copy - necessary,
because the proxy will refuse a certificate it does not hold.
B40-publish-cluster.py 2 10.0.0.62
B40-publish-cluster.py --remove 2
"""
import argparse
import os
import subprocess
import sys
import tempfile
import yaml
REGISTRY = "/etc/hpcod/clusters.yaml"
CERT_DIR = "/etc/hpcod/clusters"
SSH_KEY = "/var/lib/hpcod/.ssh/id_ed25519"
SSH_USER = "ilker"
CLUSTER_CERT = "/etc/pki/tls/certs/ood-portal.crt"
def fetch_cert(head: str) -> bytes:
"""Read the certificate the cluster's portal presents, over SSH."""
out = subprocess.run(
[
"ssh", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null", "-o", "ConnectTimeout=10",
"-i", SSH_KEY, f"{SSH_USER}@{head}", f"sudo cat {CLUSTER_CERT}",
],
capture_output=True, check=True,
)
if b"BEGIN CERTIFICATE" not in out.stdout:
raise SystemExit(f"{head} did not return a certificate")
return out.stdout
def load() -> dict:
try:
with open(REGISTRY) as fh:
doc = yaml.safe_load(fh) or {}
except FileNotFoundError:
doc = {}
doc.setdefault("clusters", [])
return doc
def save(doc: dict) -> None:
# Written whole and moved into place: the control plane re-reads this
# file whenever its mtime moves, and a half-written one would take every
# published cluster offline until the next write.
d = os.path.dirname(REGISTRY)
with tempfile.NamedTemporaryFile("w", dir=d, delete=False) as tmp:
tmp.write("# Written by B40-publish-cluster.py - do not edit by hand.\n")
yaml.safe_dump(doc, tmp, default_flow_style=False, sort_keys=False)
name = tmp.name
os.chmod(name, 0o644)
os.replace(name, REGISTRY)
def publish(cluster_id: int, head: str) -> None:
os.makedirs(CERT_DIR, mode=0o755, exist_ok=True)
cert_path = os.path.join(CERT_DIR, f"c{cluster_id:03d}.crt")
with open(cert_path, "wb") as fh:
fh.write(fetch_cert(head))
os.chmod(cert_path, 0o644)
doc = load()
doc["clusters"] = [t for t in doc["clusters"] if t.get("id") != cluster_id]
doc["clusters"].append(
{"id": cluster_id, "portal": f"{head}:443", "cert": cert_path}
)
doc["clusters"].sort(key=lambda t: t.get("id", 0))
save(doc)
print(f"published c{cluster_id:03d} -> {head}:443")
print(f" https://c{cluster_id:03d}.hpc.example.org")
def remove(cluster_id: int) -> None:
doc = load()
before = len(doc["clusters"])
doc["clusters"] = [t for t in doc["clusters"] if t.get("id") != cluster_id]
save(doc)
# The certificate goes too. Leaving it would let a later cluster reusing
# this id be trusted against its predecessor's key.
cert_path = os.path.join(CERT_DIR, f"c{cluster_id:03d}.crt")
if os.path.exists(cert_path):
os.remove(cert_path)
print(f"withdrew c{cluster_id:03d}" if before != len(doc["clusters"])
else f"c{cluster_id:03d} was not published")
def main() -> int:
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("cluster_id", type=int)
ap.add_argument("head", nargs="?", help="management address of the head node")
ap.add_argument("--remove", action="store_true")
args = ap.parse_args()
if not 1 <= args.cluster_id <= 254:
raise SystemExit("cluster id must be 1-254")
if args.remove:
remove(args.cluster_id)
elif not args.head:
raise SystemExit("publishing needs the head node's address")
else:
publish(args.cluster_id, args.head)
return 0
if __name__ == "__main__":
sys.exit(main())