runtime-rs: add the wait and status method for sandbox api

Add the sandbox wait and sandbox status method for sandbox
api.

Signed-off-by: Fupan Li <fupan.lfp@antgroup.com>
This commit is contained in:
Fupan Li 2025-02-05 17:37:01 +08:00 committed by Pavel Mores
parent 2d6b1e6b13
commit 8332f427d2
4 changed files with 49 additions and 6 deletions

View File

@ -4,7 +4,10 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use crate::{types::ContainerProcess, ContainerManager}; use crate::{
types::{ContainerProcess, SandboxExitInfo, SandboxStatus},
ContainerManager,
};
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
@ -31,6 +34,8 @@ pub trait Sandbox: Send + Sync {
async fn stop(&self) -> Result<()>; async fn stop(&self) -> Result<()>;
async fn cleanup(&self) -> Result<()>; async fn cleanup(&self) -> Result<()>;
async fn shutdown(&self) -> Result<()>; async fn shutdown(&self) -> Result<()>;
async fn status(&self) -> Result<SandboxStatus>;
async fn wait(&self) -> Result<SandboxExitInfo>;
// utils // utils
async fn set_iptables(&self, is_ipv6: bool, data: Vec<u8>) -> Result<Vec<u8>>; async fn set_iptables(&self, is_ipv6: bool, data: Vec<u8>) -> Result<Vec<u8>>;

View File

@ -9,7 +9,7 @@ use common::{
message::Message, message::Message,
types::{ types::{
ContainerProcess, PlatformInfo, SandboxConfig, SandboxRequest, SandboxResponse, ContainerProcess, PlatformInfo, SandboxConfig, SandboxRequest, SandboxResponse,
StartSandboxInfo, TaskRequest, TaskResponse, SandboxStatusInfo, StartSandboxInfo, TaskRequest, TaskResponse,
}, },
RuntimeHandler, RuntimeInstance, Sandbox, SandboxNetworkEnv, RuntimeHandler, RuntimeInstance, Sandbox, SandboxNetworkEnv,
}; };
@ -487,10 +487,20 @@ impl RuntimeHandlerManager {
Ok(SandboxResponse::StopSandbox) Ok(SandboxResponse::StopSandbox)
} }
SandboxRequest::WaitSandbox(_) => { SandboxRequest::WaitSandbox(_) => {
unimplemented!() let exit_info = sandbox.wait().await.context("wait sandbox")?;
Ok(SandboxResponse::WaitSandbox(exit_info))
} }
SandboxRequest::SandboxStatus(_) => { SandboxRequest::SandboxStatus(_) => {
unimplemented!() let status = sandbox.status().await?;
Ok(SandboxResponse::SandboxStatus(SandboxStatusInfo {
sandbox_id: status.sandbox_id,
pid: status.pid,
state: status.state,
created_at: None,
exited_at: None,
}))
} }
SandboxRequest::Ping(_) => Ok(SandboxResponse::Ping), SandboxRequest::Ping(_) => Ok(SandboxResponse::Ping),
SandboxRequest::ShutdownSandbox(_) => { SandboxRequest::ShutdownSandbox(_) => {

View File

@ -37,6 +37,7 @@ runtime-spec = { path = "../../../../libs/runtime-spec" }
oci-spec = { version = "0.6.8", features = ["runtime"] } oci-spec = { version = "0.6.8", features = ["runtime"] }
persist = { path = "../../persist"} persist = { path = "../../persist"}
resource = { path = "../../resource" } resource = { path = "../../resource" }
strum = { version = "0.24.0", features = ["derive"] }
[features] [features]
default = ["cloud-hypervisor"] default = ["cloud-hypervisor"]

View File

@ -14,7 +14,11 @@ use async_trait::async_trait;
use common::message::{Action, Message}; use common::message::{Action, Message};
use common::types::utils::option_system_time_into; use common::types::utils::option_system_time_into;
use common::types::ContainerProcess; use common::types::ContainerProcess;
use common::{types::SandboxConfig, ContainerManager, Sandbox, SandboxNetworkEnv}; use common::{
types::{SandboxConfig, SandboxExitInfo, SandboxStatus},
ContainerManager, Sandbox, SandboxNetworkEnv,
};
use containerd_shim_protos::events::task::{TaskExit, TaskOOM}; use containerd_shim_protos::events::task::{TaskExit, TaskOOM};
use hypervisor::VsockConfig; use hypervisor::VsockConfig;
#[cfg(not(target_arch = "s390x"))] #[cfg(not(target_arch = "s390x"))]
@ -38,6 +42,7 @@ use resource::network::{dan_config_path, DanNetworkConfig, NetworkConfig, Networ
use resource::{ResourceConfig, ResourceManager}; use resource::{ResourceConfig, ResourceManager};
use runtime_spec as spec; use runtime_spec as spec;
use std::sync::Arc; use std::sync::Arc;
use strum::Display;
use tokio::sync::{mpsc::Sender, Mutex, RwLock}; use tokio::sync::{mpsc::Sender, Mutex, RwLock};
use tracing::instrument; use tracing::instrument;
@ -51,7 +56,7 @@ pub struct SandboxRestoreArgs {
pub sender: Sender<Message>, pub sender: Sender<Message>,
} }
#[derive(Clone, Copy, PartialEq, Debug)] #[derive(Clone, Copy, PartialEq, Debug, Display)]
pub enum SandboxState { pub enum SandboxState {
Init, Init,
Running, Running,
@ -485,6 +490,28 @@ impl Sandbox for VirtSandbox {
Ok(()) Ok(())
} }
async fn status(&self) -> Result<SandboxStatus> {
info!(sl!(), "get sandbox status");
let inner = self.inner.read().await;
let state = inner.state.to_string();
Ok(SandboxStatus {
sandbox_id: self.sid.clone(),
pid: std::process::id(),
state,
..Default::default()
})
}
async fn wait(&self) -> Result<SandboxExitInfo> {
info!(sl!(), "wait sandbox");
let exit_code = self.hypervisor.wait_vm().await.context("wait vm")?;
Ok(SandboxExitInfo {
exit_status: exit_code as u32,
exited_at: Some(std::time::SystemTime::now()),
})
}
async fn stop(&self) -> Result<()> { async fn stop(&self) -> Result<()> {
let mut sandbox_inner = self.inner.write().await; let mut sandbox_inner = self.inner.write().await;