agent: use a local fn to reduce duplicated codes

The same codes used twices, aggregated into a function can
reduce codes.

Signed-off-by: bin liu <bin@hyper.sh>
This commit is contained in:
bin liu 2020-10-10 19:55:05 +08:00 committed by Peng Tao
parent 7bb3e562bc
commit efddcb4ab8

View File

@ -208,18 +208,7 @@ impl agentService {
let cid = req.container_id.clone(); let cid = req.container_id.clone();
let mut cmounts: Vec<String> = vec![]; let mut cmounts: Vec<String> = vec![];
if req.timeout == 0 { let mut remove_container_resources = |sandbox: &mut Sandbox| -> Result<()> {
let s = Arc::clone(&self.sandbox);
let mut sandbox = s.lock().unwrap();
let ctr: &mut LinuxContainer = match sandbox.get_container(cid.as_str()) {
Some(cr) => cr,
None => {
return Err(anyhow!(nix::Error::from_errno(Errno::EINVAL)));
}
};
ctr.destroy()?;
// Find the sandbox storage used by this container // Find the sandbox storage used by this container
let mounts = sandbox.container_mounts.get(&cid); let mounts = sandbox.container_mounts.get(&cid);
if mounts.is_some() { if mounts.is_some() {
@ -240,6 +229,22 @@ impl agentService {
sandbox.container_mounts.remove(cid.as_str()); sandbox.container_mounts.remove(cid.as_str());
sandbox.containers.remove(cid.as_str()); sandbox.containers.remove(cid.as_str());
Ok(())
};
if req.timeout == 0 {
let s = Arc::clone(&self.sandbox);
let mut sandbox = s.lock().unwrap();
let ctr: &mut LinuxContainer = match sandbox.get_container(cid.as_str()) {
Some(cr) => cr,
None => {
return Err(anyhow!(nix::Error::from_errno(Errno::EINVAL)));
}
};
ctr.destroy()?;
remove_container_resources(&mut sandbox)?;
return Ok(()); return Ok(());
} }
@ -275,26 +280,7 @@ impl agentService {
let s = self.sandbox.clone(); let s = self.sandbox.clone();
let mut sandbox = s.lock().unwrap(); let mut sandbox = s.lock().unwrap();
// Find the sandbox storage used by this container remove_container_resources(&mut sandbox)?;
let mounts = sandbox.container_mounts.get(&cid);
if mounts.is_some() {
let mounts = mounts.unwrap();
remove_mounts(&mounts)?;
for m in mounts.iter() {
if sandbox.storages.get(m).is_some() {
cmounts.push(m.to_string());
}
}
}
for m in cmounts.iter() {
sandbox.unset_and_remove_sandbox_storage(m)?;
}
sandbox.container_mounts.remove(&cid);
sandbox.containers.remove(cid.as_str());
Ok(()) Ok(())
} }