Skip to main content

microvms_edges/
adapters.rs

1// SPDX-License-Identifier: Apache-2.0
2//! The production [`Adapters`]: reqwest for sessions, SigV4 for STS and S3, tokio's clock.
3
4use std::sync::Arc;
5use std::time::Duration;
6
7use futures_util::future::BoxFuture;
8
9use microvms_app::adapters::Adapters;
10use microvms_app::clock::Clock;
11use microvms_app::control::BuildServices;
12use microvms_app::error::Error;
13use microvms_app::region::Region;
14use microvms_app::session::SharedBackend;
15
16use crate::clock::TokioClock;
17use crate::control::services::SignedBuildServices;
18use crate::session::http::ReqwestBackend;
19
20/// The adapters every production control plane carries.
21///
22/// Holds nothing. Each call builds its piece fresh, the way the use cases did inline before
23/// they went through this port, so the credential chain resolves when it's first needed
24/// rather than when the plane is built.
25#[derive(Clone, Copy, Debug, Default)]
26pub struct SystemAdapters;
27
28impl Adapters for SystemAdapters {
29    fn http_backend(&self, endpoint: &str, timeout: Duration) -> Result<SharedBackend, Error> {
30        Ok(Arc::new(ReqwestBackend::new(endpoint, timeout)?))
31    }
32
33    fn build_services(
34        &self,
35        region: Region,
36    ) -> BoxFuture<'_, Result<Arc<dyn BuildServices>, Error>> {
37        Box::pin(async move {
38            Ok(Arc::new(SignedBuildServices::new(region).await?) as Arc<dyn BuildServices>)
39        })
40    }
41
42    fn clock(&self) -> Arc<dyn Clock> {
43        Arc::new(TokioClock::new())
44    }
45
46    /// A plain write to stderr rather than a log macro, because the client has no logging
47    /// dependency, and taking one on to warn about a leak would be a dependency for a
48    /// diagnostic. Not `eprintln!`: it panics when stderr's reader has gone, and a panic in
49    /// `drop` during an unwind aborts the process (CLI-7). The error is ignored; there is
50    /// nowhere left to report it.
51    fn warn(&self, warning: &str) {
52        use std::io::Write as _;
53        let _ = writeln!(std::io::stderr(), "{warning}");
54    }
55}