diff --git a/src/agent/src/image.rs b/src/agent/src/image.rs index 151ab89433..66b9e195d4 100644 --- a/src/agent/src/image.rs +++ b/src/agent/src/image.rs @@ -5,20 +5,32 @@ // SPDX-License-Identifier: Apache-2.0 // -use std::sync::Arc; +use anyhow::{anyhow, Result}; use tokio::sync::Mutex; -use crate::sandbox::Sandbox; + +#[rustfmt::skip] +lazy_static! { + pub static ref IMAGE_SERVICE: Mutex> = Mutex::new(None); +} // Convenience function to obtain the scope logger. fn sl() -> slog::Logger { slog_scope::logger().new(o!("subsystem" => "image")) } -pub struct ImageService { - sandbox: Arc>, -} +#[derive(Clone)] +pub struct ImageService {} impl ImageService { - pub fn new(sandbox: Arc>) -> Self { - Self { sandbox } + pub fn new() -> Self { + Self {} + } + + /// Get the singleton instance of image service. + pub async fn singleton() -> Result { + IMAGE_SERVICE + .lock() + .await + .clone() + .ok_or_else(|| anyhow!("image service is uninitialized")) } } diff --git a/src/agent/src/main.rs b/src/agent/src/main.rs index 8bb7fa0423..7e7979b10c 100644 --- a/src/agent/src/main.rs +++ b/src/agent/src/main.rs @@ -380,7 +380,7 @@ async fn start_sandbox( sandbox.lock().await.sender = Some(tx); // vsock:///dev/vsock, port - let mut server = rpc::start(sandbox.clone(), config.server_addr.as_str(), init_mode)?; + let mut server = rpc::start(sandbox.clone(), config.server_addr.as_str(), init_mode).await?; server.start().await?; rx.await?; diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index 56cff23edb..1d5558fecd 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -1584,7 +1584,11 @@ async fn read_stream(reader: &Mutex>, l: usize) -> Result>, server_address: &str, init_mode: bool) -> Result { +pub async fn start( + s: Arc>, + server_address: &str, + init_mode: bool, +) -> Result { let agent_service = Box::new(AgentService { sandbox: s, init_mode, @@ -1594,6 +1598,9 @@ pub fn start(s: Arc>, server_address: &str, init_mode: bool) -> R let health_service = Box::new(HealthService {}) as Box; let hservice = health_ttrpc::create_health(Arc::new(health_service)); + let image_service = image::ImageService::new(); + *image::IMAGE_SERVICE.lock().await = Some(image_service.clone()); + let server = TtrpcServer::new() .bind(server_address)? .register_service(aservice)