From a8c1c0f5c2a461329a75ee6c6dbe033770b25164 Mon Sep 17 00:00:00 2001 From: atlas Date: Sun, 29 Nov 2026 07:01:12 +0100 Subject: [PATCH] swarm-controller: ssh-connection handlers take their own sub-state via FromRef --- swarm-controller/src/agents/schedules.rs | 14 ++++- swarm-controller/src/ssh_connections.rs | 69 ++++++++++++++++++------ 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/swarm-controller/src/agents/schedules.rs b/swarm-controller/src/agents/schedules.rs index 3841ba2b9..4066224a5 100644 --- a/swarm-controller/src/agents/schedules.rs +++ b/swarm-controller/src/agents/schedules.rs @@ -34,6 +34,7 @@ use swarm_queue_client::agent_schedules; use swarm_wire::agent_schedules::{AgentSchedules, Schedule}; use crate::agent_renewal::unix_now; +use crate::routes::hives::HiveEntry; use crate::routes::{AppState, error_problem}; use crate::wanted::WantedWriter; @@ -657,13 +658,22 @@ pub(crate) fn refused((status, detail): (StatusCode, String)) -> ProblemDetails /// Every agent some hive declares and has not destroyed. pub(crate) async fn live(state: &AppState) -> Result, ProblemDetails> { - let Some(wanted) = state.wanted.as_ref() else { + live_of(state.wanted.as_ref(), &state.hives).await +} + +/// [`live`] over the wanted-state writer and the hive list alone, for a caller +/// that holds only those. +pub(crate) async fn live_of( + wanted: Option<&Arc>, + hives: &[HiveEntry], +) -> Result, ProblemDetails> { + let Some(wanted) = wanted else { return Err(error_problem( StatusCode::SERVICE_UNAVAILABLE, "no swarm queue is configured on this host", )); }; - let hives: Vec = state.hives.iter().map(|h| h.name.clone()).collect(); + let hives: Vec = hives.iter().map(|h| h.name.clone()).collect(); Ok(crate::subagent_term::live_agents(wanted, &hives).await) } diff --git a/swarm-controller/src/ssh_connections.rs b/swarm-controller/src/ssh_connections.rs index f4e575d09..70e248c44 100644 --- a/swarm-controller/src/ssh_connections.rs +++ b/swarm-controller/src/ssh_connections.rs @@ -8,9 +8,11 @@ //! rest of the record. The agent renders the connections into its ssh config //! while its `ssh` switch is on; nothing here depends on the switch. +use std::sync::Arc; + use axum::Json; use axum::extract::rejection::JsonRejection; -use axum::extract::{Path, State}; +use axum::extract::{FromRef, Path, State}; use axum::http::StatusCode; use problem_details::ProblemDetails; use swarm_controller_api::ssh_connections::{PutSshConnection, SshConnection, SshConnections}; @@ -18,8 +20,33 @@ use swarm_wire::agent_permissions::AgentPermissions; use swarm_wire::ssh_connection::{SshConnection as Stored, check_list}; use crate::agent_permissions::{agent_name, logged, open_or_create, queue_problem}; +use crate::routes::hives::HiveEntry; use crate::routes::{AppState, error_problem}; -use crate::schedules::{live, queue, read_or_default, refused, write}; +use crate::schedules::{live_of, queue_of, read_or_default, refused, write}; +use crate::status::StatusReader; +use crate::wanted::WantedWriter; + +/// What the ssh-connection routes may reach of [`AppState`]: the queue +/// connection, the wanted-state writer and the hive list. +/// +/// Derived per request from [`AppState`] (`FromRef`), so each field is a clone +/// of the same `Arc` and nothing is held twice. +#[derive(Clone)] +pub(crate) struct SshConnectionsState { + pub(crate) status: Option>, + pub(crate) wanted: Option>, + pub(crate) hives: Arc>, +} + +impl FromRef for SshConnectionsState { + fn from_ref(state: &AppState) -> Self { + Self { + status: state.status.clone(), + wanted: state.wanted.clone(), + hives: Arc::clone(&state.hives), + } + } +} /// How many times a losing writer re-reads and re-applies, as in /// `agent_permissions`. @@ -55,11 +82,11 @@ fn view(stored: Vec) -> SshConnections { tag = "agents" )] pub(crate) async fn get_ssh_connections( - State(state): State, + State(state): State, Path(name): Path, ) -> Result, ProblemDetails> { let agent = agent_name(&name).map_err(refused)?; - let client = queue(&state).map_err(refused)?; + let client = queue_of(state.status.as_ref()).map_err(refused)?; let store = open_or_create(&client).await?; let (current, _, _) = read_or_default::(&store, &agent) .await @@ -93,7 +120,7 @@ pub(crate) async fn get_ssh_connections( tag = "agents" )] pub(crate) async fn put_ssh_connection( - State(state): State, + State(state): State, Path((name, conn)): Path<(String, String)>, req: Result, JsonRejection>, ) -> Result, ProblemDetails> { @@ -109,8 +136,12 @@ pub(crate) async fn put_ssh_connection( } .checked() .map_err(|why| refuse(error_problem(StatusCode::BAD_REQUEST, &why)))?; - let client = queue(&state).map_err(|r| refuse(refused(r)))?; - if !live(&state).await.map_err(refuse)?.contains(&agent) { + let client = queue_of(state.status.as_ref()).map_err(|r| refuse(refused(r)))?; + if !live_of(state.wanted.as_ref(), &state.hives) + .await + .map_err(refuse)? + .contains(&agent) + { return Err(refuse(error_problem( StatusCode::BAD_REQUEST, &format!("no hive's wanted state declares an agent {agent:?} that is not destroyed"), @@ -162,12 +193,12 @@ pub(crate) async fn put_ssh_connection( tag = "agents" )] pub(crate) async fn delete_ssh_connection( - State(state): State, + State(state): State, Path((name, conn)): Path<(String, String)>, ) -> Result { let refuse = |p| logged("delete ssh connection", &name, p); let agent = agent_name(&name).map_err(|r| refuse(refused(r)))?; - let client = queue(&state).map_err(|r| refuse(refused(r)))?; + let client = queue_of(state.status.as_ref()).map_err(|r| refuse(refused(r)))?; let store = open_or_create(&client).await?; for _ in 0..MAX_ATTEMPTS { let (mut value, revision, _) = read_or_default::(&store, &agent) @@ -201,11 +232,14 @@ pub(crate) async fn delete_ssh_connection( #[cfg(test)] mod tests { - use super::{delete_ssh_connection, get_ssh_connections, put_ssh_connection}; + use super::{ + SshConnectionsState, delete_ssh_connection, get_ssh_connections, put_ssh_connection, + }; use crate::agent_permissions::{delete_agent_permissions, put_agent_permissions}; use crate::routes::AppState; use crate::test_nats::TestNats; use axum::Json; + use axum::extract::FromRef; use axum::extract::{Path, State}; use axum::http::StatusCode; use problem_details::ProblemDetails; @@ -227,7 +261,7 @@ mod tests { req: PutSshConnection, ) -> Result { put_ssh_connection( - State(state.clone()), + State(SshConnectionsState::from_ref(state)), Path((agent.to_owned(), name.to_owned())), Ok(Json(req)), ) @@ -236,15 +270,18 @@ mod tests { } async fn get(state: &AppState, agent: &str) -> SshConnections { - get_ssh_connections(State(state.clone()), Path(agent.to_owned())) - .await - .expect("read") - .0 + get_ssh_connections( + State(SshConnectionsState::from_ref(state)), + Path(agent.to_owned()), + ) + .await + .expect("read") + .0 } async fn remove(state: &AppState, agent: &str, name: &str) -> StatusCode { delete_ssh_connection( - State(state.clone()), + State(SshConnectionsState::from_ref(state)), Path((agent.to_owned(), name.to_owned())), ) .await