diff options
| author | aur-build <aur-build@localhost> | 2026-11-22 10:36:24 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-22 10:36:24 +0000 |
| commit | 3dc71b2f5160ef9bd46143eed0edb5d2557b58e6 (patch) | |
| tree | 7cc7169181b4bdd73fd8817f8c1d7cce6e6998a8 /src/sandbox/kata.rs | |
| parent | 9dc9087d89891c6e5ef7e5dc084dd13c8611843e (diff) | |
| download | aur-build-3dc71b2f5160ef9bd46143eed0edb5d2557b58e6.tar.gz aur-build-3dc71b2f5160ef9bd46143eed0edb5d2557b58e6.zip | |
rustfmt,lints: pin the formatting, adopt the idiom lints, pay them off
rustfmt.toml fixes the shape the tree is written in (120 columns, small
items kept on one line) and one canonical pass makes it the truth.
[lints.clippy] names the idiom lints worth enforcing and leaves out the
two that would add doc-comment boilerplate rather than remove it. The
tree pays them off here: write!/writeln! instead of push_str(&format!()),
let-else instead of match-on-Option, borrows where a thread entry point
only reads what it owns, no `&self` on a Copy state and no `self` nobody
reads.
ops::to_prometheus gains one gauge helper: a metric's HELP, TYPE and
value are now emitted by the same call instead of three.
Diffstat (limited to 'src/sandbox/kata.rs')
| -rw-r--r-- | src/sandbox/kata.rs | 92 |
1 files changed, 37 insertions, 55 deletions
diff --git a/src/sandbox/kata.rs b/src/sandbox/kata.rs index cfc3bd6..ca39c6b 100644 --- a/src/sandbox/kata.rs +++ b/src/sandbox/kata.rs @@ -77,11 +77,9 @@ fn sandbox_cr(req: &ProvisionRequest) -> serde_json::Value { impl K8sSandboxProvisioner { fn for_config(cfg: &Config) -> Option<Self> { match &cfg.sandbox { - crate::config::Sandbox::AgentSandbox { api, namespace, bearer, .. } => Some(Self { - api: api.clone(), - namespace: namespace.clone(), - bearer: bearer.clone(), - }), + crate::config::Sandbox::AgentSandbox { api, namespace, bearer, .. } => { + Some(Self { api: api.clone(), namespace: namespace.clone(), bearer: bearer.clone() }) + } _ => None, } } @@ -120,7 +118,7 @@ impl Provisioner for K8sSandboxProvisioner { Ok(()) } - fn name(&self) -> &str { + fn name(&self) -> &'static str { "k8s-sandbox-crd" } } @@ -173,7 +171,7 @@ impl Provisioner for LocalAgentProvisioner { Ok(()) } - fn name(&self) -> &str { + fn name(&self) -> &'static str { "local-agent" } } @@ -182,7 +180,11 @@ impl Provisioner for LocalAgentProvisioner { /// is not a nameable path from outside; the alias below is the only way to /// reach it, and the alias is what carries the feature-conditional visibility. mod kata_backend { - use super::*; + use super::{ + bounded, route_dial_in, wire, Arc, AtomicU64, Config, Duration, Error, Frame, HashMap, K8sSandboxProvisioner, + Mutex, OnceLock, Ordering, PoolFull, ProvisionRequest, Provisioner, Receiver, RemoteSandbox, Result, Sandbox, + SandboxBackend, SandboxInfo, SandboxSpec, Sender, TcpListener, TcpStream, + }; pub struct KataBackend { cfg: Config, @@ -198,22 +200,19 @@ mod kata_backend { let crate::config::Sandbox::AgentSandbox { api, namespace, bearer, image, listen, token, .. } = &cfg.sandbox else { - return Err(Error::invalid( - "KataBackend::shared_for needs an agent-sandbox configuration", - )); + return Err(Error::invalid("KataBackend::shared_for needs an agent-sandbox configuration")); }; - let key = crate::hash::hex( - format!("{listen}\n{token}\n{api}\n{namespace}\n{bearer}\n{image}").as_bytes(), - ); + let key = crate::hash::hex(format!("{listen}\n{token}\n{api}\n{namespace}\n{bearer}\n{image}").as_bytes()); static CACHE: OnceLock<Mutex<HashMap<String, Arc<KataBackend>>>> = OnceLock::new(); let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new())); - let mut guard = cache.lock().unwrap_or_else(|p| p.into_inner()); + let mut guard = cache.lock().unwrap_or_else(std::sync::PoisonError::into_inner); if let Some(existing) = guard.get(&key) { return Ok(existing.clone()); } - let provisioner = Arc::new(K8sSandboxProvisioner::for_config(cfg).ok_or_else(|| { - Error::invalid("KataBackend::shared_for needs an agent-sandbox configuration") - })?); + let provisioner = Arc::new( + K8sSandboxProvisioner::for_config(cfg) + .ok_or_else(|| Error::invalid("KataBackend::shared_for needs an agent-sandbox configuration"))?, + ); let backend = Arc::new(Self::bind(cfg, listen, token, provisioner)?); guard.insert(key, backend.clone()); Ok(backend) @@ -230,12 +229,7 @@ mod kata_backend { } } - pub fn bind( - cfg: &Config, - listen: &str, - token: &str, - provisioner: Arc<dyn Provisioner>, - ) -> Result<Self> { + pub fn bind(cfg: &Config, listen: &str, token: &str, provisioner: Arc<dyn Provisioner>) -> Result<Self> { let listener = TcpListener::bind(listen)?; let addr = listener.local_addr()?.to_string(); let pending: Arc<Mutex<HashMap<String, Sender<TcpStream>>>> = Arc::new(Mutex::new(HashMap::new())); @@ -267,10 +261,7 @@ mod kata_backend { fn nonce(&self) -> String { let n = self.counter.fetch_add(1, Ordering::SeqCst); - let nanos = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_nanos()) - .unwrap_or(0); + let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); crate::hash::hex_str(&format!("{}-{n}-{nanos}", std::process::id()))[..20].to_string() } } @@ -285,19 +276,13 @@ mod kata_backend { self.pending.lock().unwrap().remove(&id); return Err(e); } - let stream = match rx.recv_timeout(spec.ttl) { - Ok(s) => s, - Err(_) => { - self.pending.lock().unwrap().remove(&id); - let _ = self.provisioner.reclaim(&id); - return Err(Error::pool_exhausted(PoolFull::Backend { - backend: "kata", - detail: format!( - "sandbox {id} for profile {} never dialed in within its TTL", - spec.profile - ), - })); - } + let Ok(stream) = rx.recv_timeout(spec.ttl) else { + self.pending.lock().unwrap().remove(&id); + let _ = self.provisioner.reclaim(&id); + return Err(Error::pool_exhausted(PoolFull::Backend { + backend: "kata", + detail: format!("sandbox {id} for profile {} never dialed in within its TTL", spec.profile), + })); }; let ttl = spec.ttl.max(Duration::from_secs(1)); wire::write_frame(&mut stream.try_clone()?, &Frame::HelloAck { ttl_secs: ttl.as_secs() })?; @@ -322,11 +307,7 @@ pub use kata_backend::KataBackend; #[cfg(not(feature = "test-support"))] pub(crate) use kata_backend::KataBackend; -fn route_dial_in( - stream: TcpStream, - token: &str, - pending: &Arc<Mutex<HashMap<String, Sender<TcpStream>>>>, -) { +fn route_dial_in(stream: TcpStream, token: &str, pending: &Arc<Mutex<HashMap<String, Sender<TcpStream>>>>) { let _ = stream.set_read_timeout(Some(Duration::from_secs(10))); let mut reader = match stream.try_clone() { Ok(s) => BufReader::new(s), @@ -361,8 +342,10 @@ mod tests { fn the_cr_asks_kata_for_a_vm_that_dies_on_its_own_by_ttl() { let cr = sandbox_cr(&req()); assert_eq!(cr["spec"]["runtimeClass"], "kata"); - assert_eq!(cr["spec"]["activeDeadlineSeconds"], 120, - "the orchestrator must enforce the TTL even if the hub dies"); + assert_eq!( + cr["spec"]["activeDeadlineSeconds"], 120, + "the orchestrator must enforce the TTL even if the hub dies" + ); assert_eq!(cr["spec"]["shutdownPolicy"], "Delete"); let args = cr["spec"]["template"]["spec"]["containers"][0]["args"] .as_array() @@ -455,17 +438,15 @@ mod tests { fn reclaim(&self, _id: &str) -> Result<()> { Ok(()) } - fn name(&self) -> &str { + fn name(&self) -> &'static str { "null" } } let cfg = Config::default(); - let backend = - KataBackend::bind(&cfg, "127.0.0.1:0", "tok", Arc::new(NullProvisioner)).unwrap(); + let backend = KataBackend::bind(&cfg, "127.0.0.1:0", "tok", Arc::new(NullProvisioner)).unwrap(); let started = std::time::Instant::now(); - let err = match backend.acquire(&SandboxSpec::new("extra-x86_64", "ghost", Duration::from_secs(1))) { - Ok(_) => panic!("nobody provisioned anything; the lease must not succeed"), - Err(e) => e, + let Err(err) = backend.acquire(&SandboxSpec::new("extra-x86_64", "ghost", Duration::from_secs(1))) else { + panic!("nobody provisioned anything; the lease must not succeed") }; assert!(started.elapsed() >= Duration::from_millis(900), "acquire gave up before the TTL"); assert!(err.to_string().contains("never dialed in"), "{err}"); @@ -495,7 +476,8 @@ mod tests { bearer: std::env::var("AUR_KATA_TOKEN").unwrap_or_default(), namespace: std::env::var("AUR_KATA_NS").unwrap_or_else(|_| "default".into()), }); - let backend = KataBackend::bind(&cfg, "0.0.0.0:9443", &std::env::var("AUR_AGENT_TOKEN").unwrap(), prov).unwrap(); + let backend = + KataBackend::bind(&cfg, "0.0.0.0:9443", &std::env::var("AUR_AGENT_TOKEN").unwrap(), prov).unwrap(); let sbx = backend .acquire(&SandboxSpec::new("extra-x86_64", "live", Duration::from_secs(300))) .expect("live kata sandbox should lease"); |
