swarm-controller: agent schedules and limits take a settings-only sub-state
This commit is contained in:
parent
5616b221b8
commit
00e143fe7b
3 changed files with 77 additions and 26 deletions
|
|
@ -19,8 +19,8 @@ use swarm_controller_api::limits::Caps;
|
|||
use swarm_queue_client::agent_limits;
|
||||
use swarm_wire::agent_limits::AgentCaps;
|
||||
|
||||
use crate::routes::{AppState, error_problem, wanted_error_status};
|
||||
use crate::schedules::{live, queue, read_or_default, refused, write_revision};
|
||||
use crate::routes::{error_problem, wanted_error_status};
|
||||
use crate::schedules::{read_or_default, refused, write_revision};
|
||||
|
||||
/// How many times a losing writer re-reads and re-applies, as in
|
||||
/// `wanted::MAX_ATTEMPTS`.
|
||||
|
|
@ -172,11 +172,11 @@ async fn open_or_create(
|
|||
tag = "agents"
|
||||
)]
|
||||
pub(crate) async fn get_agent_limits(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<super::AgentSettingsState>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Json<Caps>, ProblemDetails> {
|
||||
let agent = agent_name(&name).map_err(refused)?;
|
||||
let client = queue(&state).map_err(refused)?;
|
||||
let client = state.queue().map_err(refused)?;
|
||||
let Some(store) = agent_limits::open(&client)
|
||||
.await
|
||||
.map_err(|e| queue_problem(&anyhow::Error::new(e)))?
|
||||
|
|
@ -212,14 +212,14 @@ pub(crate) async fn get_agent_limits(
|
|||
tag = "agents"
|
||||
)]
|
||||
pub(crate) async fn put_agent_limits(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<super::AgentSettingsState>,
|
||||
Path(name): Path<String>,
|
||||
Json(req): Json<Caps>,
|
||||
) -> Result<Json<Caps>, ProblemDetails> {
|
||||
let agent = agent_name(&name).map_err(refused)?;
|
||||
let value = into_stored(&req).map_err(|why| error_problem(StatusCode::BAD_REQUEST, &why))?;
|
||||
let client = queue(&state).map_err(refused)?;
|
||||
if !live(&state).await?.contains(&agent) {
|
||||
let client = state.queue().map_err(refused)?;
|
||||
if !state.live().await?.contains(&agent) {
|
||||
return Err(error_problem(
|
||||
StatusCode::BAD_REQUEST,
|
||||
&format!("no hive's wanted state declares an agent {agent:?} that is not destroyed"),
|
||||
|
|
@ -306,11 +306,11 @@ pub(crate) async fn restore(client: &async_nats::Client, agent: &str, replaced:
|
|||
tag = "agents"
|
||||
)]
|
||||
pub(crate) async fn delete_agent_limits(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<super::AgentSettingsState>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<StatusCode, ProblemDetails> {
|
||||
let agent = agent_name(&name).map_err(refused)?;
|
||||
let client = queue(&state).map_err(refused)?;
|
||||
let client = state.queue().map_err(refused)?;
|
||||
let store = open_or_create(&client).await?;
|
||||
store
|
||||
.delete(&agent)
|
||||
|
|
@ -325,9 +325,10 @@ pub(crate) mod tests {
|
|||
use super::{
|
||||
Caps, cpu_quota, delete_agent_limits, get_agent_limits, memory_max, put_agent_limits,
|
||||
};
|
||||
use crate::agents::AgentSettingsState;
|
||||
use crate::test_nats::TestNats;
|
||||
use crate::wanted::WantedWriter;
|
||||
use axum::extract::{Path, State};
|
||||
use axum::extract::{FromRef as _, Path, State};
|
||||
use axum::http::StatusCode;
|
||||
use std::sync::Arc;
|
||||
use swarm_wire::wanted::AgentState;
|
||||
|
|
@ -483,7 +484,7 @@ pub(crate) mod tests {
|
|||
value: Caps,
|
||||
) -> Result<axum::Json<Caps>, problem_details::ProblemDetails> {
|
||||
put_agent_limits(
|
||||
State(state.clone()),
|
||||
State(AgentSettingsState::from_ref(state)),
|
||||
Path(agent.to_owned()),
|
||||
axum::Json(value),
|
||||
)
|
||||
|
|
@ -491,10 +492,13 @@ pub(crate) mod tests {
|
|||
}
|
||||
|
||||
async fn get(state: &crate::routes::AppState, agent: &str) -> Caps {
|
||||
get_agent_limits(State(state.clone()), Path(agent.to_owned()))
|
||||
.await
|
||||
.expect("caps are read")
|
||||
.0
|
||||
get_agent_limits(
|
||||
State(AgentSettingsState::from_ref(state)),
|
||||
Path(agent.to_owned()),
|
||||
)
|
||||
.await
|
||||
.expect("caps are read")
|
||||
.0
|
||||
}
|
||||
|
||||
/// The raw value a hive reads for `agent`, or `None` for no key or a
|
||||
|
|
@ -534,7 +538,11 @@ pub(crate) mod tests {
|
|||
assert_eq!(get(&state, "atlas").await, caps(Some("400%"), Some("8G")));
|
||||
|
||||
assert_eq!(
|
||||
delete_agent_limits(State(state.clone()), Path("atlas".to_owned())).await,
|
||||
delete_agent_limits(
|
||||
State(AgentSettingsState::from_ref(&state)),
|
||||
Path("atlas".to_owned())
|
||||
)
|
||||
.await,
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
);
|
||||
assert_eq!(published(&client, "atlas").await, None);
|
||||
|
|
|
|||
|
|
@ -82,6 +82,49 @@ impl FromRef<AppState> for AgentReadersState {
|
|||
}
|
||||
}
|
||||
|
||||
/// What the agent settings handlers (schedules, limits) may reach of
|
||||
/// [`AppState`]: the queue reader, and the wanted-state writer and hive list
|
||||
/// that say which agents are live.
|
||||
///
|
||||
/// 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 AgentSettingsState {
|
||||
pub(crate) status: Option<Arc<crate::status::StatusReader>>,
|
||||
pub(crate) wanted: Option<Arc<crate::wanted::WantedWriter>>,
|
||||
pub(crate) hives: Arc<Vec<crate::routes::hives::HiveEntry>>,
|
||||
}
|
||||
|
||||
impl FromRef<AppState> for AgentSettingsState {
|
||||
fn from_ref(state: &AppState) -> Self {
|
||||
Self {
|
||||
status: state.status.clone(),
|
||||
wanted: state.wanted.clone(),
|
||||
hives: Arc::clone(&state.hives),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AgentSettingsState {
|
||||
/// The connected queue client, or the problem.
|
||||
///
|
||||
/// # Errors
|
||||
/// A 503 when no queue is configured or it is not connected.
|
||||
pub(crate) fn queue(&self) -> Result<async_nats::Client, (axum::http::StatusCode, String)> {
|
||||
schedules::queue_of(self.status.as_ref())
|
||||
}
|
||||
|
||||
/// Every agent some hive declares and has not destroyed.
|
||||
///
|
||||
/// # Errors
|
||||
/// A 503 when no wanted-state writer is configured.
|
||||
pub(crate) async fn live(
|
||||
&self,
|
||||
) -> Result<std::collections::BTreeSet<String>, problem_details::ProblemDetails> {
|
||||
schedules::live_of(self.wanted.as_ref(), &self.hives).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use axum::http::StatusCode;
|
||||
|
|
|
|||
|
|
@ -691,9 +691,9 @@ pub(crate) async fn live_of(
|
|||
tag = "schedules"
|
||||
)]
|
||||
pub(crate) async fn list_schedules(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<super::AgentSettingsState>,
|
||||
) -> Result<Json<Vec<ScheduleView>>, ProblemDetails> {
|
||||
let client = queue(&state).map_err(refused)?;
|
||||
let client = state.queue().map_err(refused)?;
|
||||
let store = open_or_create_definitions(&client)
|
||||
.await
|
||||
.map_err(|e| problem(e.into()))?;
|
||||
|
|
@ -724,11 +724,11 @@ pub(crate) async fn list_schedules(
|
|||
tag = "schedules"
|
||||
)]
|
||||
pub(crate) async fn add_schedule(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<super::AgentSettingsState>,
|
||||
Json(req): Json<AddScheduleRequest>,
|
||||
) -> Result<(StatusCode, Json<ScheduleView>), ProblemDetails> {
|
||||
let client = queue(&state).map_err(refused)?;
|
||||
let live = live(&state).await?;
|
||||
let client = state.queue().map_err(refused)?;
|
||||
let live = state.live().await?;
|
||||
let now = unix_now();
|
||||
let store = open_or_create_definitions(&client)
|
||||
.await
|
||||
|
|
@ -766,12 +766,12 @@ pub(crate) async fn add_schedule(
|
|||
tag = "schedules"
|
||||
)]
|
||||
pub(crate) async fn edit_schedule(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<super::AgentSettingsState>,
|
||||
Path(id): Path<String>,
|
||||
Json(req): Json<EditScheduleRequest>,
|
||||
) -> Result<Json<ScheduleView>, ProblemDetails> {
|
||||
let client = queue(&state).map_err(refused)?;
|
||||
let live = live(&state).await?;
|
||||
let client = state.queue().map_err(refused)?;
|
||||
let live = state.live().await?;
|
||||
let store = open_or_create_definitions(&client)
|
||||
.await
|
||||
.map_err(|e| problem(e.into()))?;
|
||||
|
|
@ -799,10 +799,10 @@ pub(crate) async fn edit_schedule(
|
|||
tag = "schedules"
|
||||
)]
|
||||
pub(crate) async fn remove_schedule(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<super::AgentSettingsState>,
|
||||
Path(id): Path<String>,
|
||||
) -> Result<StatusCode, ProblemDetails> {
|
||||
let client = queue(&state).map_err(refused)?;
|
||||
let client = state.queue().map_err(refused)?;
|
||||
let store = open_or_create_definitions(&client)
|
||||
.await
|
||||
.map_err(|e| problem(e.into()))?;
|
||||
|
|
|
|||
Loading…
Reference in a new issue