feat(runtime-rs): update linux resource when stop_process

Update the resource when delete container, which is in
stop_process in runtime-rs.

Fixes: #5030

Signed-off-by: Yushuo <y-shuo@linux.alibaba.com>
Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com>
This commit is contained in:
Yushuo 2023-05-06 17:43:36 +08:00
parent a39e1e6cd1
commit a0385e1383
3 changed files with 33 additions and 14 deletions

View File

@ -42,7 +42,6 @@ pub struct Container {
agent: Arc<dyn Agent>, agent: Arc<dyn Agent>,
resource_manager: Arc<ResourceManager>, resource_manager: Arc<ResourceManager>,
logger: slog::Logger, logger: slog::Logger,
pub linux_resources: Option<LinuxResources>,
} }
impl Container { impl Container {
@ -52,7 +51,6 @@ impl Container {
spec: oci::Spec, spec: oci::Spec,
agent: Arc<dyn Agent>, agent: Arc<dyn Agent>,
resource_manager: Arc<ResourceManager>, resource_manager: Arc<ResourceManager>,
linux_resources: Option<LinuxResources>,
) -> Result<Self> { ) -> Result<Self> {
let container_id = ContainerID::new(&config.container_id).context("new container id")?; let container_id = ContainerID::new(&config.container_id).context("new container id")?;
let logger = sl!().new(o!("container_id" => config.container_id.clone())); let logger = sl!().new(o!("container_id" => config.container_id.clone()));
@ -66,6 +64,10 @@ impl Container {
config.stderr.clone(), config.stderr.clone(),
config.terminal, config.terminal,
); );
let linux_resources = spec
.linux
.as_ref()
.and_then(|linux| linux.resources.clone());
Ok(Self { Ok(Self {
pid, pid,
@ -76,11 +78,11 @@ impl Container {
agent.clone(), agent.clone(),
init_process, init_process,
logger.clone(), logger.clone(),
linux_resources,
))), ))),
agent, agent,
resource_manager, resource_manager,
logger, logger,
linux_resources,
}) })
} }
@ -153,13 +155,11 @@ impl Container {
.handler_devices(&config.container_id, linux) .handler_devices(&config.container_id, linux)
.await?; .await?;
// update cgroups // update vcpus, mems and host cgroups
self.resource_manager self.resource_manager
.update_linux_resource( .update_linux_resource(
&config.container_id, &config.container_id,
spec.linux inner.linux_resources.as_ref(),
.as_ref()
.and_then(|linux| linux.resources.as_ref()),
ResourceUpdateOp::Add, ResourceUpdateOp::Add,
) )
.await?; .await?;
@ -327,7 +327,20 @@ impl Container {
inner inner
.stop_process(container_process, true, &device_manager) .stop_process(container_process, true, &device_manager)
.await .await
.context("stop process") .context("stop process")?;
// update vcpus, mems and host cgroups
if container_process.process_type == ProcessType::Container {
self.resource_manager
.update_linux_resource(
&self.config.container_id,
inner.linux_resources.as_ref(),
ResourceUpdateOp::Del,
)
.await?;
}
Ok(())
} }
pub async fn pause(&self) -> Result<()> { pub async fn pause(&self) -> Result<()> {
@ -402,6 +415,9 @@ impl Container {
} }
pub async fn update(&self, resources: &LinuxResources) -> Result<()> { pub async fn update(&self, resources: &LinuxResources) -> Result<()> {
let mut inner = self.inner.write().await;
inner.linux_resources = Some(resources.clone());
// update vcpus, mems and host cgroups
self.resource_manager self.resource_manager
.update_linux_resource( .update_linux_resource(
&self.config.container_id, &self.config.container_id,

View File

@ -14,6 +14,7 @@ use common::{
}; };
use hypervisor::device::device_manager::DeviceManager; use hypervisor::device::device_manager::DeviceManager;
use nix::sys::signal::Signal; use nix::sys::signal::Signal;
use oci::LinuxResources;
use resource::{rootfs::Rootfs, volume::Volume}; use resource::{rootfs::Rootfs, volume::Volume};
use tokio::sync::RwLock; use tokio::sync::RwLock;
@ -32,10 +33,16 @@ pub struct ContainerInner {
pub(crate) exec_processes: HashMap<String, Exec>, pub(crate) exec_processes: HashMap<String, Exec>,
pub(crate) rootfs: Vec<Arc<dyn Rootfs>>, pub(crate) rootfs: Vec<Arc<dyn Rootfs>>,
pub(crate) volumes: Vec<Arc<dyn Volume>>, pub(crate) volumes: Vec<Arc<dyn Volume>>,
pub(crate) linux_resources: Option<LinuxResources>,
} }
impl ContainerInner { impl ContainerInner {
pub(crate) fn new(agent: Arc<dyn Agent>, init_process: Process, logger: slog::Logger) -> Self { pub(crate) fn new(
agent: Arc<dyn Agent>,
init_process: Process,
logger: slog::Logger,
linux_resources: Option<LinuxResources>,
) -> Self {
Self { Self {
agent, agent,
logger, logger,
@ -43,6 +50,7 @@ impl ContainerInner {
exec_processes: HashMap::new(), exec_processes: HashMap::new(),
rootfs: vec![], rootfs: vec![],
volumes: vec![], volumes: vec![],
linux_resources,
} }
} }

View File

@ -60,17 +60,12 @@ impl VirtContainerManager {
#[async_trait] #[async_trait]
impl ContainerManager for VirtContainerManager { impl ContainerManager for VirtContainerManager {
async fn create_container(&self, config: ContainerConfig, spec: oci::Spec) -> Result<PID> { async fn create_container(&self, config: ContainerConfig, spec: oci::Spec) -> Result<PID> {
let linux_resources = match spec.linux.clone() {
Some(linux) => linux.resources,
_ => None,
};
let container = Container::new( let container = Container::new(
self.pid, self.pid,
config.clone(), config.clone(),
spec.clone(), spec.clone(),
self.agent.clone(), self.agent.clone(),
self.resource_manager.clone(), self.resource_manager.clone(),
linux_resources,
) )
.context("new container")?; .context("new container")?;