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 maia
commit ee6b5f69f6
2 changed files with 222 additions and 157 deletions

View file

@ -337,26 +337,40 @@ pub(crate) fn describe_error(status: hyper::StatusCode, body: &[u8]) -> String {
/// Message for a socket that could not be dialled.
///
/// Worth spelling out because the three ways this fails need three
/// different fixes and `No such file or directory (os error 2)` names
/// none of them: the daemon is off, it binds elsewhere, or this host is
/// not the one running it at all. No group-membership case to mention
/// here — `swarmctl` already runs as root.
/// The controller runs as a pod, so the host has no socket to dial: the way in
/// is the swarm's gateway.
pub(crate) fn connect_hint(socket: &Path) -> String {
format!(
"could not connect to swarm-controller at {} — is it running on this host? \
(`systemctl status swarm-controller`). The path comes from \
`--controller-socket` or `SWARM_CONTROLLER_SOCKET`, which the \
swarm-controller nix module sets from `socketPath`",
"could not connect to a swarm-controller socket at {} — the controller runs as a pod \
and has no socket on this host; reach it through the swarm's gateway with \
`--swarm <domain>` (or `SWARMCTL_SWARM`) after `swarmctl login`",
socket.display()
)
}
#[cfg(test)]
mod tests {
use super::{check_confirmed, describe_error, parse_ident, parse_state};
use super::{check_confirmed, connect_hint, describe_error, parse_ident, parse_state};
use swarm_wire::wanted::AgentState;
/// The controller has no socket on the host, so the hint sends the operator to the
/// gateway and the login, not to a unit that is not there.
#[test]
fn the_connect_hint_names_the_gateway_and_the_login() {
let hint = connect_hint(std::path::Path::new(
"/run/swarm-controller/controller.sock",
));
assert!(
hint.contains("/run/swarm-controller/controller.sock"),
"{hint}"
);
assert!(
hint.contains("--swarm <domain>") && hint.contains("swarmctl login"),
"{hint}"
);
assert!(!hint.contains("systemctl"), "{hint}");
}
/// The accepted values are the wire enum's: each spelling it writes
/// parses back, and an unknown one names the valid ones.
#[test]

View file

@ -92,8 +92,9 @@ struct RemoteArgs {
/// socket. Falls back to `SWARMCTL_SWARM`; `login` then falls back to
/// `SWARMCTL_LOGIN_DEFAULT_SWARM`. The swarm-controller nix module sets both
/// to its host's own swarm, so there the verbs use the gateway and need
/// `swarmctl login` once; an empty `SWARMCTL_SWARM` or `--swarm ""` puts
/// them back on the controller's socket.
/// `swarmctl login` once. With no swarm, they use the socket that
/// `--controller-socket` or `SWARM_CONTROLLER_SOCKET` names; the module sets
/// neither.
#[arg(long, value_name = "URL", global = true)]
swarm: Option<String>,
/// A PEM CA certificate to trust beside the system roots, for a swarm
@ -134,12 +135,19 @@ impl RemoteArgs {
.transpose()
}
/// The swarm when one is named, the controller's socket otherwise.
/// The swarm when one is named, else the controller socket the flag or
/// `SWARM_CONTROLLER_SOCKET` names. The controller runs as a pod and has no
/// socket on the host, so a host with neither is told to name the swarm.
fn target(&self, socket: Option<PathBuf>) -> Result<Target> {
Ok(match self.swarm()? {
Some(swarm) => Target::Swarm(swarm),
None => Target::Socket(path_from(socket, "SWARM_CONTROLLER_SOCKET")?),
})
if let Some(swarm) = self.swarm()? {
return Ok(Target::Swarm(swarm));
}
let socket =
socket.or_else(|| std::env::var_os("SWARM_CONTROLLER_SOCKET").map(PathBuf::from));
socket.map(Target::Socket).context(
"no swarm is named and no controller socket is given — name the swarm with \
`--swarm <domain>` or `SWARMCTL_SWARM` and run `swarmctl login`",
)
}
}
@ -615,9 +623,10 @@ struct AgentCreateArgs {
agents_md: Option<PathBuf>,
/// swarm-controller's unix socket.
///
/// Supplied by the nix module that installs this binary, from the same
/// `socketPath` option the daemon binds; falls back to
/// Used when `--swarm` and `SWARMCTL_SWARM` give no swarm; falls back to
/// `SWARM_CONTROLLER_SOCKET`.
/// The controller runs as a pod and has no socket on the host, so name
/// the swarm instead.
#[arg(long, value_name = "PATH")]
controller_socket: Option<PathBuf>,
}
@ -635,9 +644,10 @@ struct AgentMintForgeTokenArgs {
name: String,
/// swarm-controller's unix socket.
///
/// Supplied by the nix module that installs this binary, from the same
/// `socketPath` option the daemon binds; falls back to
/// Used when `--swarm` and `SWARMCTL_SWARM` give no swarm; falls back to
/// `SWARM_CONTROLLER_SOCKET`.
/// The controller runs as a pod and has no socket on the host, so name
/// the swarm instead.
#[arg(long, value_name = "PATH")]
controller_socket: Option<PathBuf>,
}
@ -658,9 +668,10 @@ struct ForgeMakeAdminArgs {
name: String,
/// swarm-controller's unix socket.
///
/// Supplied by the nix module that installs this binary, from the same
/// `socketPath` option the daemon binds; falls back to
/// Used when `--swarm` and `SWARMCTL_SWARM` give no swarm; falls back to
/// `SWARM_CONTROLLER_SOCKET`.
/// The controller runs as a pod and has no socket on the host, so name
/// the swarm instead.
#[arg(long, value_name = "PATH")]
controller_socket: Option<PathBuf>,
}
@ -765,9 +776,10 @@ struct ScheduleIdArgs {
struct ControllerSocketArg {
/// swarm-controller's unix socket.
///
/// Supplied by the nix module that installs this binary, from the same
/// `socketPath` option the daemon binds; falls back to
/// Used when `--swarm` and `SWARMCTL_SWARM` give no swarm; falls back to
/// `SWARM_CONTROLLER_SOCKET`.
/// The controller runs as a pod and has no socket on the host, so name
/// the swarm instead.
#[arg(long, value_name = "PATH")]
controller_socket: Option<PathBuf>,
}
@ -1553,6 +1565,31 @@ mod tests {
);
}
/// A host that names no swarm and gives no socket (the host of a pod-only
/// controller) is told to name the swarm, not to look for a unit.
#[test]
fn with_no_swarm_and_no_socket_the_error_names_the_swarm() {
assert!(
std::env::var_os("SWARMCTL_SWARM").is_none()
&& std::env::var_os("SWARM_CONTROLLER_SOCKET").is_none(),
"this test needs both variables unset"
);
let cli = Cli::try_parse_from(["swarmctl", "schedule", "list"]).expect("parses");
let Verb::Schedule {
command: ScheduleVerb::List(args),
} = cli.command
else {
panic!("expected `schedule list`");
};
let err = args.socket.resolve(&cli.remote).err().expect("no target");
let text = format!("{err:#}");
assert!(
text.contains("--swarm <domain>") && text.contains("swarmctl login"),
"{text}"
);
assert!(!text.contains("systemctl"), "{text}");
}
/// Root on the controller host, with no `--swarm` and no login: the verbs
/// still go over the unix socket, unchanged by the remote path.
#[test]