diff --git a/src/runtime-rs/crates/runtimes/src/lib.rs b/src/runtime-rs/crates/runtimes/src/lib.rs index 00d75e4ada..177a75a4db 100644 --- a/src/runtime-rs/crates/runtimes/src/lib.rs +++ b/src/runtime-rs/crates/runtimes/src/lib.rs @@ -12,5 +12,5 @@ logging::logger_with_subsystem!(sl, "runtimes"); pub mod manager; pub use manager::RuntimeHandlerManager; mod shim_mgmt; -pub use shim_mgmt::client::MgmtClient; +pub use shim_mgmt::{client::MgmtClient, server::sb_storage_path}; mod static_resource; diff --git a/src/runtime-rs/crates/runtimes/src/manager.rs b/src/runtime-rs/crates/runtimes/src/manager.rs index 7a14edd2ce..b0773c5f17 100644 --- a/src/runtime-rs/crates/runtimes/src/manager.rs +++ b/src/runtime-rs/crates/runtimes/src/manager.rs @@ -8,10 +8,7 @@ use std::sync::Arc; use anyhow::{anyhow, Context, Result}; -use crate::{ - shim_mgmt::server::MgmtServer, - static_resource::StaticResourceManager, -}; +use crate::{shim_mgmt::server::MgmtServer, static_resource::StaticResourceManager}; use common::{ message::Message, types::{Request, Response}, diff --git a/src/runtime-rs/crates/runtimes/src/shim_mgmt/client.rs b/src/runtime-rs/crates/runtimes/src/shim_mgmt/client.rs index f44f0a50f8..267de24282 100644 --- a/src/runtime-rs/crates/runtimes/src/shim_mgmt/client.rs +++ b/src/runtime-rs/crates/runtimes/src/shim_mgmt/client.rs @@ -4,8 +4,6 @@ // SPDX-License-Identifier: Apache-2.0 // -#![allow(dead_code)] - // Defines the general client functions used by other components acting like // clients. To be specific, a client first connect to the socket, then send // request to destined URL, and finally handle the request(or not) @@ -13,19 +11,20 @@ use std::{path::Path, path::PathBuf, time::Duration}; use super::server::mgmt_socket_addr; -use anyhow::{Context, Result}; -use hyper::{Body, Client, Method, Request, Response}; +use anyhow::{anyhow, Context, Result}; +use hyper::{Body, Client, Response}; use hyperlocal::{UnixClientExt, UnixConnector, Uri}; /// Shim management client with timeout pub struct MgmtClient { /// The socket *file path* on host file system - s_path: PathBuf, + sock_path: PathBuf, /// The http client connect to the long standing shim mgmt server client: Client, - /// timeout value for each dial + /// Timeout value for each dial, usually 200ms will be enough + /// For heavier workload, you may want longer timeout timeout: Option, } @@ -36,10 +35,10 @@ impl MgmtClient { let s_addr = unix_socket_path .strip_prefix("unix:") .context("failed to strix prefix")?; - let s_path = Path::new("/").join(s_addr).as_path().to_owned(); + let sock_path = Path::new("/").join(s_addr).as_path().to_owned(); let client = Client::unix(); Ok(Self { - s_path, + sock_path, client, timeout, }) @@ -48,32 +47,15 @@ impl MgmtClient { /// The http GET method for client, return a raw response. Further handling should be done by caller. /// Parameter uri should be like "/agent-url" etc. pub async fn get(&self, uri: &str) -> Result> { - let url: hyper::Uri = Uri::new(&self.s_path, uri).into(); - let response = self.client.get(url).await.context("failed to GET")?; - Ok(response) - } - - /// The http PUT method for client - pub async fn put(&self, uri: &str) -> Result> { - let url: hyper::Uri = Uri::new(&self.s_path, uri).into(); - let req = Request::builder() - .method(Method::PUT) - .uri(url) - .body(Body::from("")) - .expect("request builder"); - let response = self.client.request(req).await?; - Ok(response) - } - - /// The http POST method for client - pub async fn post(&self, uri: &str) -> Result> { - let url: hyper::Uri = Uri::new(&self.s_path, uri).into(); - let req = Request::builder() - .method(Method::POST) - .uri(url) - .body(Body::from("")) - .expect("request builder"); - let response = self.client.request(req).await?; - Ok(response) + let url: hyper::Uri = Uri::new(&self.sock_path, uri).into(); + let work = self.client.get(url); + match self.timeout { + Some(timeout) => match tokio::time::timeout(timeout, work).await { + Ok(result) => result.map_err(|e| anyhow!(e)), + Err(_) => Err(anyhow!("TIMEOUT")), + }, + // if timeout not set, work executes directly + None => work.await.context("failed to GET"), + } } } diff --git a/src/runtime-rs/crates/runtimes/src/shim_mgmt/mod.rs b/src/runtime-rs/crates/runtimes/src/shim_mgmt/mod.rs index ff053b3370..08ccd9d1d8 100644 --- a/src/runtime-rs/crates/runtimes/src/shim_mgmt/mod.rs +++ b/src/runtime-rs/crates/runtimes/src/shim_mgmt/mod.rs @@ -4,6 +4,6 @@ // SPDX-License-Identifier: Apache-2.0 // +pub mod client; mod handlers; pub mod server; -pub mod client; diff --git a/src/runtime-rs/crates/runtimes/src/shim_mgmt/server.rs b/src/runtime-rs/crates/runtimes/src/shim_mgmt/server.rs index 7b1f8cf18b..e8cc316892 100644 --- a/src/runtime-rs/crates/runtimes/src/shim_mgmt/server.rs +++ b/src/runtime-rs/crates/runtimes/src/shim_mgmt/server.rs @@ -21,7 +21,7 @@ use tokio::net::UnixListener; use super::handlers::handler_mux; pub(crate) const DIRECT_VOLUMN_PATH_KEY: &str = "path"; -pub(crate) const DIRECT_VOLUMN_STAT_URL: &str = "/direct-volumn/stats"; +pub(crate) const DIRECT_VOLUMN_STATS_URL: &str = "/direct-volumn/stats"; pub(crate) const DIRECT_VOLUMN_RESIZE_URL: &str = "/direct-volumn/resize"; pub(crate) const AGENT_URL: &str = "/agent-url"; pub(crate) const IP_TABLE_URL: &str = "/iptables"; @@ -53,10 +53,10 @@ impl MgmtServer { // TODO(when metrics is supported): register sandbox metrics // running management http server in an infinite loop, able to serve concurrent requests pub async fn run(self: Arc) { - let lsnr = lsnr_from_path(self.s_addr.clone()).await.unwrap(); - // start an infinate loop, which serves the incomming uds stream + let listener = listener_from_path(self.s_addr.clone()).await.unwrap(); + // start an infinite loop, which serves the incomming uds stream loop { - let (stream, _) = lsnr.accept().await.unwrap(); + let (stream, _) = listener.accept().await.unwrap(); let me = self.clone(); // spawn a light weight thread to multiplex to the handler tokio::task::spawn(async move { @@ -91,7 +91,7 @@ pub fn mgmt_socket_addr(sid: String) -> String { // from path, return a unix listener corresponding to that path, // if the path(socket file) is not created, we create that here -async fn lsnr_from_path(path: String) -> Result { +async fn listener_from_path(path: String) -> Result { // create the socket if not present let trim_path = path.strip_prefix("unix:").context("trim path")?; let file_path = Path::new("/").join(trim_path); @@ -103,3 +103,15 @@ async fn lsnr_from_path(path: String) -> Result { info!(sl!(), "mgmt-svr: binding to path {}", path); UnixListener::bind(file_path).context("bind address") } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn mgmt_svr_test_sock_addr() { + let sid = String::from("414123"); + let addr = mgmt_socket_addr(sid); + assert_eq!(addr, "unix:///run/kata/414123/shim-monitor.sock"); + } +}