diff --git a/src/runtime-rs/Cargo.lock b/src/runtime-rs/Cargo.lock index e38ae35105..e2525363b6 100644 --- a/src/runtime-rs/Cargo.lock +++ b/src/runtime-rs/Cargo.lock @@ -2470,6 +2470,7 @@ dependencies = [ "slog", "slog-scope", "tokio", + "url", "virt_container", "wasm_container", ] diff --git a/src/runtime-rs/crates/agent/src/kata/agent.rs b/src/runtime-rs/crates/agent/src/kata/agent.rs index b403f0f44c..6baab2dd1f 100644 --- a/src/runtime-rs/crates/agent/src/kata/agent.rs +++ b/src/runtime-rs/crates/agent/src/kata/agent.rs @@ -115,5 +115,6 @@ impl_agent!( copy_file | crate::CopyFileRequest | crate::Empty | None, get_oom_event | crate::Empty | crate::OomEventResponse | Some(0), get_ip_tables | crate::GetIPTablesRequest | crate::GetIPTablesResponse | None, - set_ip_tables | crate::SetIPTablesRequest | crate::SetIPTablesResponse | None + set_ip_tables | crate::SetIPTablesRequest | crate::SetIPTablesResponse | None, + get_volume_stats | crate::VolumeStatsRequest | crate::VolumeStatsResponse | None ); diff --git a/src/runtime-rs/crates/agent/src/kata/trans.rs b/src/runtime-rs/crates/agent/src/kata/trans.rs index 4ac0c45ec2..f2f7255354 100644 --- a/src/runtime-rs/crates/agent/src/kata/trans.rs +++ b/src/runtime-rs/crates/agent/src/kata/trans.rs @@ -8,7 +8,7 @@ use std::convert::Into; use protocols::{ agent::{self, OOMEvent}, - empty, health, types, + csi, empty, health, types, }; use crate::{ @@ -24,7 +24,7 @@ use crate::{ SetGuestDateTimeRequest, SetIPTablesRequest, SetIPTablesResponse, SignalProcessRequest, StatsContainerResponse, Storage, StringUser, ThrottlingData, TtyWinResizeRequest, UpdateContainerRequest, UpdateInterfaceRequest, UpdateRoutesRequest, VersionCheckResponse, - WaitProcessRequest, WriteStreamRequest, + VolumeStatsRequest, VolumeStatsResponse, WaitProcessRequest, WriteStreamRequest, }, OomEventResponse, WaitProcessResponse, WriteStreamResponse, }; @@ -846,3 +846,24 @@ impl From for OomEventResponse { } } } + +impl From for agent::VolumeStatsRequest { + fn from(from: VolumeStatsRequest) -> Self { + Self { + volume_guest_path: from.volume_guest_path, + unknown_fields: Default::default(), + cached_size: Default::default(), + } + } +} + +impl From for VolumeStatsResponse { + fn from(from: csi::VolumeStatsResponse) -> Self { + let result: String = format!( + "Usage: {:?}\nVolume Condition: {:?}", + from.get_usage(), + from.get_volume_condition() + ); + Self { data: result } + } +} diff --git a/src/runtime-rs/crates/agent/src/lib.rs b/src/runtime-rs/crates/agent/src/lib.rs index a3d1da72ac..fd4dde2557 100644 --- a/src/runtime-rs/crates/agent/src/lib.rs +++ b/src/runtime-rs/crates/agent/src/lib.rs @@ -23,8 +23,8 @@ pub use types::{ ReseedRandomDevRequest, ResizeVolumeRequest, Route, Routes, SetGuestDateTimeRequest, SetIPTablesRequest, SetIPTablesResponse, SignalProcessRequest, StatsContainerResponse, Storage, TtyWinResizeRequest, UpdateContainerRequest, UpdateInterfaceRequest, UpdateRoutesRequest, - VersionCheckResponse, WaitProcessRequest, WaitProcessResponse, WriteStreamRequest, - WriteStreamResponse, + VersionCheckResponse, VolumeStatsRequest, VolumeStatsResponse, WaitProcessRequest, + WaitProcessResponse, WriteStreamRequest, WriteStreamResponse, }; use anyhow::Result; @@ -88,4 +88,5 @@ pub trait Agent: AgentManager + HealthService + Send + Sync { async fn get_oom_event(&self, req: Empty) -> Result; async fn get_ip_tables(&self, req: GetIPTablesRequest) -> Result; async fn set_ip_tables(&self, req: SetIPTablesRequest) -> Result; + async fn get_volume_stats(&self, req: VolumeStatsRequest) -> Result; } diff --git a/src/runtime-rs/crates/agent/src/types.rs b/src/runtime-rs/crates/agent/src/types.rs index 0cd509ff5e..7937166bd6 100644 --- a/src/runtime-rs/crates/agent/src/types.rs +++ b/src/runtime-rs/crates/agent/src/types.rs @@ -568,6 +568,16 @@ pub struct ResizeVolumeRequest { pub size: u64, } +#[derive(PartialEq, Clone, Default, Debug)] +pub struct VolumeStatsRequest { + pub volume_guest_path: String, +} + +#[derive(PartialEq, Clone, Default, Debug)] +pub struct VolumeStatsResponse { + pub data: String, +} + #[cfg(test)] mod test { use std::convert::TryFrom; diff --git a/src/runtime-rs/crates/runtimes/common/src/sandbox.rs b/src/runtime-rs/crates/runtimes/common/src/sandbox.rs index 3fee8165d4..7b1bcd8d54 100644 --- a/src/runtime-rs/crates/runtimes/common/src/sandbox.rs +++ b/src/runtime-rs/crates/runtimes/common/src/sandbox.rs @@ -26,4 +26,5 @@ pub trait Sandbox: Send + Sync { // utils async fn set_iptables(&self, is_ipv6: bool, data: Vec) -> Result>; async fn get_iptables(&self, is_ipv6: bool) -> Result>; + async fn direct_volume_stats(&self, volume_path: &str) -> Result; } diff --git a/src/runtime-rs/crates/runtimes/src/shim_mgmt/handlers.rs b/src/runtime-rs/crates/runtimes/src/shim_mgmt/handlers.rs index dea9725a5e..b2ee0fd137 100644 --- a/src/runtime-rs/crates/runtimes/src/shim_mgmt/handlers.rs +++ b/src/runtime-rs/crates/runtimes/src/shim_mgmt/handlers.rs @@ -11,8 +11,9 @@ use anyhow::{anyhow, Result}; use common::Sandbox; use hyper::{Body, Method, Request, Response, StatusCode}; use std::sync::Arc; +use url::Url; -use shim_interface::shim_mgmt::{AGENT_URL, IP6_TABLE_URL, IP_TABLE_URL}; +use shim_interface::shim_mgmt::{AGENT_URL, IP6_TABLE_URL, IP_TABLE_URL, DIRECT_VOLUMN_PATH_KEY, DIRECT_VOLUMN_STATS_URL,}; // main router for response, this works as a multiplexer on // http arrival which invokes the corresponding handler function @@ -34,6 +35,7 @@ pub(crate) async fn handler_mux( (&Method::PUT, IP6_TABLE_URL) | (&Method::GET, IP6_TABLE_URL) => { ipv6_table_handler(sandbox, req).await } + (&Method::POST, DIRECT_VOLUMN_STATS_URL) => direct_volume_stats_handler(sandbox, req).await, _ => Ok(not_found(req).await), } } @@ -101,3 +103,24 @@ async fn generic_ip_table_handler( _ => Err(anyhow!("IP Tables only takes PUT and GET")), } } + +async fn direct_volume_stats_handler( + sandbox: Arc, + req: Request, +) -> Result> { + let params = Url::parse(&req.uri().to_string()) + .unwrap() + .query_pairs() + .into_owned() + .collect::>(); + let volume_path = params.get(DIRECT_VOLUMN_PATH_KEY).unwrap(); + let result = sandbox.direct_volume_stats(volume_path).await; + match result { + Ok(stats) => Ok(Response::new(Body::from(stats))), + Err(e) => { + let builder = Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR); + let resp = builder.body(Body::from(e.to_string())).unwrap(); + Ok(resp) + } + } +} diff --git a/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs b/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs index 0d6e4765ec..41fdab15fe 100644 --- a/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs +++ b/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs @@ -8,6 +8,7 @@ use std::sync::Arc; use agent::{ self, kata::KataAgent, types::KernelModule, Agent, GetIPTablesRequest, SetIPTablesRequest, + VolumeStatsRequest, }; use anyhow::{anyhow, Context, Result}; use async_trait::async_trait; @@ -329,6 +330,14 @@ impl Sandbox for VirtSandbox { self.agent.agent_sock().await } + async fn direct_volume_stats(&self, volume_guest_path: &str) -> Result { + let req: agent::VolumeStatsRequest = VolumeStatsRequest { + volume_guest_path: volume_guest_path.to_string(), + }; + let result = self.agent.get_volume_stats(req).await?.data; + Ok(result) + } + async fn set_iptables(&self, is_ipv6: bool, data: Vec) -> Result> { info!(sl!(), "sb: set_iptables invoked"); let req = SetIPTablesRequest { is_ipv6, data };