Watch
0
0
Fork
You've already forked hyperhive
0

swarmctl shares the create-agent, inference and permissions shapes with the controller

This commit is contained in:
atlas 2026-10-19 17:57:21 +02:00
commit 0831b85b73
26 changed files with 675 additions and 434 deletions

View file

@ -0,0 +1,45 @@
//! `/api/agents/{name}/limits`, and the `limits` of `POST /api/agents`.
use serde::{Deserialize, Serialize};
/// One agent's caps. A field left out is not capped by the swarm.
///
/// Each value is stored as given, and only in a form systemd applies: a value
/// systemd ignores ("Invalid …, ignoring") would leave the agent uncapped,
/// since a swarm cap replaces the hive's answer.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
#[serde(deny_unknown_fields)]
pub struct Caps {
/// systemd `CPUQuota=`: digits, optionally `.` and one or two digits, then
/// `%`; above zero and at most `21474836.47%`. `"100%"` is one core.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "schema", schema(example = "400%"))]
pub cpu_quota: Option<String>,
/// systemd `MemoryMax=`: `infinity`; a percentage of physical memory
/// written as for `cpu_quota`, above zero and at most `100%`; or a size:
/// digits, optionally `.` and digits, then nothing, `B` or one of
/// `K`/`M`/`G`/`T` (1024-based, upper case), at least one byte and below
/// 2^64 - 1 bytes.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "schema", schema(example = "8G"))]
pub memory_max: Option<String>,
}
#[cfg(test)]
mod tests {
use super::Caps;
#[test]
fn caps_round_trip() {
let wire = r#"{"cpu_quota":"400%","memory_max":"8G"}"#;
let caps = Caps {
cpu_quota: Some("400%".to_owned()),
memory_max: Some("8G".to_owned()),
};
assert_eq!(serde_json::to_string(&caps).unwrap(), wire);
assert_eq!(serde_json::from_str::<Caps>(wire).unwrap(), caps);
assert_eq!(serde_json::to_string(&Caps::default()).unwrap(), "{}");
assert!(serde_json::from_str::<Caps>(r#"{"cpu":"1%"}"#).is_err());
}
}