Watch
0
0
Fork
You've already forked hyperhive
0

full-setup and the host swarmctl reach the controller as a pod, not through a host socket

This commit is contained in:
atlas 2026-11-26 09:22:29 +01:00 • committed by mara
commit 087c51eef6

View file

@ -2,8 +2,6 @@ import json
import os
import shlex
API = "http://localhost"
SOCKET = os.environ["FULL_SETUP_SOCKET"]
FORGE = "https://" + os.environ["FULL_SETUP_FORGE_DOMAIN"]
HIVE = os.environ["FULL_SETUP_HIVE"]
TOPLEVELS = json.loads(os.environ["FULL_SETUP_TOPLEVELS"])
@ -11,10 +9,19 @@ REPO = os.environ["FULL_SETUP_HYPERHIVE_REPO"]
CONTROL_NIX = os.environ["FULL_SETUP_CONTROL_NIX"]
# Where `swarmctl bao init` saves what `bao operator init` printed, for root.
INIT = "/root/.local/state/swarmctl/bao-init.json"
# The host's swarmctl defaults to the swarm's gateway, where each controller verb needs a
# `swarmctl login`, which a VM without a browser cannot do. An empty `SWARMCTL_SWARM` puts a
# verb on the controller socket.
VIA_SOCKET = "SWARMCTL_SWARM="
# The controller is a pod, so the host reaches its TLS listener the way the gateway does: the
# Service's ClusterIP, the swarm services root as trust, and the gateway's client leaf. The host's
# swarmctl goes through the gateway instead, where each controller verb needs a `swarmctl login`,
# whose browser flow a VM cannot complete.
CONTROLLER_NAME = os.environ["FULL_SETUP_CONTROLLER_NAME"]
CONTROLLER_PORT = os.environ["FULL_SETUP_CONTROLLER_PORT"]
CONTROLLER_URL = f"https://{CONTROLLER_NAME}:{CONTROLLER_PORT}"
CONTROLLER_CURL = (
f"curl --silent --cacert {os.environ['FULL_SETUP_SERVICES_ROOT']} "
f"--cert {os.environ['FULL_SETUP_CONTROLLER_CERT']} --key {os.environ['FULL_SETUP_CONTROLLER_KEY']} "
f"--resolve {CONTROLLER_NAME}:{CONTROLLER_PORT}:"
f"$(getent ahostsv4 {os.environ['FULL_SETUP_CONTROLLER_SERVICE']} | awk 'NR==1 {{print $1}}')"
)
SWARM_URL = "https://" + os.environ["FULL_SETUP_SWARM_DOMAIN"]
FORGE_AUTH = "/root/forge-auth.header"
NIX = "nix --extra-experimental-features nix-command"
@ -26,17 +33,6 @@ POLICY_UNITS_ACTIVE = (
)
def controller(method, path, body=None, check="--fail-with-body"):
data = (
f"-H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
if body
else ""
)
return machine.succeed(
f"curl {check} --silent --unix-socket {SOCKET} -X {method} {API}{path} {data}"
)
def forge(method, path, body=None):
data = (
f"-H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
@ -84,8 +80,8 @@ def create_agent(body):
# archetype list comes from the builder's evaluation), so ask again.
for _ in range(60):
status, out = machine.execute(
f"curl --silent --unix-socket {SOCKET} -o /root/create.out -w '%{{http_code}}' "
f"-X POST {API}/api/agents -H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
f"{CONTROLLER_CURL} -o /root/create.out -w '%{{http_code}}' "
f"-X POST {CONTROLLER_URL}/api/agents -H 'Content-Type: application/json' -d {shlex.quote(json.dumps(body))}"
)
if out.strip().startswith("2"):
return
@ -306,7 +302,7 @@ with subtest(
"step 8: spawn, a reviewer-archetype agent, through the endpoint the swarm UI calls"
):
machine.wait_until_succeeds(
f"curl --fail --silent --unix-socket {SOCKET} {API}/api/permissions >/dev/null",
f"{CONTROLLER_CURL} --fail {CONTROLLER_URL}/api/permissions >/dev/null",
timeout=900,
)
# A custom root agent (ruth, as the UI creates her) is a follow-up, not covered yet.
@ -343,8 +339,10 @@ with subtest("internal/meta holds the flake the toplevels were built from"):
)
with subtest("swarmctl setup status reports the steps done so far"):
# Step 9 names ruth, the root agent of the real setup; this test creates none, so the report
# ends on step 9 not done and the exit status is 1.
# The host's swarmctl goes through the gateway and no login exists in the VM, so steps 1-6,
# which read the host, are reported and the two that read the controller are unknown, with
# the login named as the reason. Step 9 names ruth, the root agent of the real setup, which
# this test does not create, so the exit status is 1 either way.
reported = [
"✅ 1 secret store initialised",
"✅ 2 secret store unsealed",
@ -352,12 +350,14 @@ with subtest("swarmctl setup status reports the steps done so far"):
"✅ 4 granting units active",
"✅ 5 store unsealed after the granting units",
"✅ 6 SSO account in the admins group",
"👉 9 ruth created and up",
"✅ 10 another agent [optional]",
]
unreadable = ["❓ 9 ", "❓ 10 "]
for _ in range(60):
lines = machine.execute(f"{VIA_SOCKET} swarmctl setup status")[1].splitlines()
if all(any(line.startswith(prefix) for line in lines) for prefix in reported):
lines = machine.execute("swarmctl setup status")[1].splitlines()
if all(
any(line.startswith(prefix) for line in lines)
for prefix in reported + unreadable
):
break
machine.sleep(10)
else:
@ -365,23 +365,25 @@ with subtest("swarmctl setup status reports the steps done so far"):
"swarmctl setup status did not report the steps done so far:\n"
+ "\n".join(lines)
)
assert machine.execute(f"{VIA_SOCKET} swarmctl setup status >/dev/null")[0] == 1
for prefix in unreadable:
line = next(line for line in lines if line.startswith(prefix))
assert f"not logged in to {SWARM_URL}" in line, line
assert machine.execute("swarmctl setup status >/dev/null")[0] == 1
with subtest(
"the host's swarmctl reaches the controller through the gateway by default"
"the host's swarmctl reaches the controller through the gateway, and stops at the login"
):
# No login exists in the VM, so a verb that names the controller stops at the login, and
# says which swarm it went to. Over the socket the same verb needs none.
# No login exists in the VM, so a verb that names the controller says which swarm it went to
# and that it is not logged in.
status, out = machine.execute("swarmctl schedule list 2>&1")
assert status != 0 and f"not logged in to {SWARM_URL}" in out, (status, out)
machine.succeed(f"{VIA_SOCKET} swarmctl schedule list")
with subtest("swarmctl login discovers the swarm's SSO provider from the gateway"):
# The swarm's own CA signs the gateway's leaf. A VM has no browser, so the login runs to the
# point where it prints the authorization URL and waits for the answer, which the timeout ends.
# Reaching that URL takes the gateway's 401 challenge, its published metadata, and authelia
# accepting the pushed authorization request.
ca = "/var/lib/hive-tls/swarm-services-root.pem"
ca = os.environ["FULL_SETUP_SERVICES_ROOT"]
auth = f"https://auth.{SWARM_URL.removeprefix('https://')}"
challenge = machine.succeed(
f"curl --silent --cacert {ca} -D - -o /dev/null {SWARM_URL}/api/"
@ -408,7 +410,7 @@ with subtest("swarmctl login discovers the swarm's SSO provider from the gateway
with subtest("the agents are deployed and need a login"):
settle(
f"curl --fail --silent --unix-socket {SOCKET} {API}/api/agents/status "
f"{CONTROLLER_CURL} --fail {CONTROLLER_URL}/api/agents/status "
"| jq -e '[.[] | select(.hive == \""
+ HIVE
+ '" and .freshness == "fresh" and .needs_login == true)'