diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index 1e3492f208..e7933b3ad9 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -181,12 +181,9 @@ impl agentService { let mut s = sandbox.lock().unwrap(); let sid = s.id.clone(); - let ctr: &mut LinuxContainer = match s.get_container(cid.as_str()) { - Some(cr) => cr, - None => { - return Err(anyhow!(nix::Error::from_errno(Errno::EINVAL))); - } - }; + let ctr = s + .get_container(&cid) + .ok_or(anyhow!("Invalid container id"))?; ctr.exec()?; @@ -233,12 +230,9 @@ impl agentService { 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))); - } - }; + let ctr = sandbox + .get_container(&cid) + .ok_or(anyhow!("Invalid container id"))?; ctr.destroy()?; @@ -254,15 +248,14 @@ impl agentService { let handle = thread::spawn(move || { let mut sandbox = s.lock().unwrap(); - let ctr: &mut LinuxContainer = match sandbox.get_container(cid2.as_str()) { - Some(cr) => cr, - None => { - return; - } - }; - - ctr.destroy().unwrap(); - tx.send(1).unwrap(); + let _ctr = sandbox + .get_container(&cid2) + .ok_or(anyhow!("Invalid container id")) + .and_then(|ctr| { + ctr.destroy().unwrap(); + tx.send(1).unwrap(); + Ok(ctr) + }); }); if let Err(_) = rx.recv_timeout(Duration::from_secs(req.timeout as u64)) { @@ -302,12 +295,9 @@ impl agentService { let ocip = rustjail::process_grpc_to_oci(process); let p = Process::new(&sl!(), &ocip, exec_id.as_str(), false, pipe_size)?; - let ctr = match sandbox.get_container(cid.as_str()) { - Some(v) => v, - None => { - return Err(anyhow!(nix::Error::from_errno(nix::errno::Errno::EINVAL))); - } - }; + let ctr = sandbox + .get_container(&cid) + .ok_or(anyhow!("Invalid container id"))?; ctr.run(p)?; @@ -387,12 +377,9 @@ impl agentService { } 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))); - } - }; + let ctr = sandbox + .get_container(&cid) + .ok_or(anyhow!("Invalid container id"))?; let mut p = match ctr.processes.get_mut(&pid) { Some(p) => p, @@ -626,15 +613,12 @@ impl protocols::agent_ttrpc::AgentService for agentService { 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(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INVALID_ARGUMENT, - "invalid container id".to_string(), - ))); - } - }; + let ctr = sandbox + .get_container(&cid) + .ok_or(ttrpc::Error::RpcStatus(ttrpc::get_status( + ttrpc::Code::INVALID_ARGUMENT, + "invalid container id".to_string(), + )))?; let pids = ctr.processes().unwrap(); @@ -711,15 +695,12 @@ impl protocols::agent_ttrpc::AgentService for agentService { 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(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - "invalid container id".to_string(), - ))); - } - }; + let ctr = sandbox + .get_container(&cid) + .ok_or(ttrpc::Error::RpcStatus(ttrpc::get_status( + ttrpc::Code::INVALID_ARGUMENT, + "invalid container id".to_string(), + )))?; let resp = Empty::new(); @@ -749,15 +730,12 @@ impl protocols::agent_ttrpc::AgentService for agentService { 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(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - "invalid container id".to_string(), - ))); - } - }; + let ctr = sandbox + .get_container(&cid) + .ok_or(ttrpc::Error::RpcStatus(ttrpc::get_status( + ttrpc::Code::INVALID_ARGUMENT, + "invalid container id".to_string(), + )))?; match ctr.stats() { Err(e) => Err(ttrpc::Error::RpcStatus(ttrpc::get_status( @@ -776,22 +754,19 @@ impl protocols::agent_ttrpc::AgentService for agentService { let cid = req.get_container_id(); let s = Arc::clone(&self.sandbox); let mut sandbox = s.lock().unwrap(); - if let Some(ctr) = sandbox.get_container(cid) { - match ctr.pause() { - Err(e) => { - return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - e.to_string(), - ))) - } - Ok(_) => return Ok(Empty::new()), - } - }; - Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INVALID_ARGUMENT, - "invalid argument".to_string(), - ))) + let ctr = sandbox + .get_container(&cid) + .ok_or(ttrpc::Error::RpcStatus(ttrpc::get_status( + ttrpc::Code::INVALID_ARGUMENT, + "invalid container id".to_string(), + )))?; + + ctr.pause().map_err(|e| { + ttrpc::Error::RpcStatus(ttrpc::get_status(ttrpc::Code::INTERNAL, e.to_string())) + })?; + + Ok(Empty::new()) } fn resume_container( @@ -802,22 +777,19 @@ impl protocols::agent_ttrpc::AgentService for agentService { let cid = req.get_container_id(); let s = Arc::clone(&self.sandbox); let mut sandbox = s.lock().unwrap(); - if let Some(ctr) = sandbox.get_container(cid) { - match ctr.resume() { - Err(e) => { - return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - e.to_string(), - ))) - } - Ok(_) => return Ok(Empty::new()), - } - }; - Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INVALID_ARGUMENT, - "invalid argument: ".to_string(), - ))) + let ctr = sandbox + .get_container(&cid) + .ok_or(ttrpc::Error::RpcStatus(ttrpc::get_status( + ttrpc::Code::INVALID_ARGUMENT, + "invalid container id".to_string(), + )))?; + + ctr.resume().map_err(|e| { + ttrpc::Error::RpcStatus(ttrpc::get_status(ttrpc::Code::INTERNAL, e.to_string())) + })?; + + Ok(Empty::new()) } fn write_stdin( @@ -957,15 +929,14 @@ impl protocols::agent_ttrpc::AgentService for agentService { let rtnl = sandbox.rtnl.as_mut().unwrap(); - let iface = match rtnl.update_interface(interface.as_ref().unwrap()) { - Ok(v) => v, - Err(e) => { - return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( + let iface = rtnl + .update_interface(interface.as_ref().unwrap()) + .map_err(|e| { + ttrpc::Error::RpcStatus(ttrpc::get_status( ttrpc::Code::INTERNAL, format!("update interface: {:?}", e), - ))); - } - }; + )) + })?; Ok(iface) } @@ -986,16 +957,15 @@ impl protocols::agent_ttrpc::AgentService for agentService { } let rtnl = sandbox.rtnl.as_mut().unwrap(); + // get current routes to return when error out - let crs = match rtnl.list_routes() { - Ok(routes) => routes, - Err(e) => { - return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - format!("update routes: {:?}", e), - ))); - } - }; + let crs = rtnl.list_routes().map_err(|e| { + ttrpc::Error::RpcStatus(ttrpc::get_status( + ttrpc::Code::INTERNAL, + format!("update routes: {:?}", e), + )) + })?; + let v = match rtnl.update_routes(rs.as_ref()) { Ok(value) => value, Err(_) => crs, @@ -1020,15 +990,12 @@ impl protocols::agent_ttrpc::AgentService for agentService { } let rtnl = sandbox.rtnl.as_mut().unwrap(); - let v = match rtnl.list_interfaces() { - Ok(value) => value, - Err(e) => { - return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - format!("list interface: {:?}", e), - ))); - } - }; + let v = rtnl.list_interfaces().map_err(|e| { + ttrpc::Error::RpcStatus(ttrpc::get_status( + ttrpc::Code::INTERNAL, + format!("list interface: {:?}", e), + )) + })?; interface.set_Interfaces(RepeatedField::from_vec(v)); @@ -1050,15 +1017,12 @@ impl protocols::agent_ttrpc::AgentService for agentService { let rtnl = sandbox.rtnl.as_mut().unwrap(); - let v = match rtnl.list_routes() { - Ok(value) => value, - Err(e) => { - return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - format!("list routes: {:?}", e), - ))); - } - }; + let v = rtnl.list_routes().map_err(|e| { + ttrpc::Error::RpcStatus(ttrpc::get_status( + ttrpc::Code::INTERNAL, + format!("list routes: {:?}", e), + )) + })?; routes.set_Routes(RepeatedField::from_vec(v)); @@ -1111,26 +1075,14 @@ impl protocols::agent_ttrpc::AgentService for agentService { } for m in req.kernel_modules.iter() { - match load_kernel_module(m) { - Ok(_) => (), - Err(e) => { - return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - e.to_string(), - ))) - } - } + let _ = load_kernel_module(m).map_err(|e| { + ttrpc::Error::RpcStatus(ttrpc::get_status(ttrpc::Code::INTERNAL, e.to_string())) + })?; } - match s.setup_shared_namespaces() { - Ok(_) => (), - Err(e) => { - return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - e.to_string(), - ))) - } - } + s.setup_shared_namespaces().map_err(|e| { + ttrpc::Error::RpcStatus(ttrpc::get_status(ttrpc::Code::INTERNAL, e.to_string())) + })?; } match add_storages(sl!(), req.storages.to_vec(), self.sandbox.clone()) { @@ -1201,12 +1153,9 @@ impl protocols::agent_ttrpc::AgentService for agentService { let rtnl = sandbox.rtnl.as_mut().unwrap(); - if let Err(e) = rtnl.add_arp_neighbors(neighs.as_ref()) { - return Err(ttrpc::Error::RpcStatus(ttrpc::get_status( - ttrpc::Code::INTERNAL, - e.to_string(), - ))); - } + rtnl.add_arp_neighbors(neighs.as_ref()).map_err(|e| { + ttrpc::Error::RpcStatus(ttrpc::get_status(ttrpc::Code::INTERNAL, e.to_string())) + })?; Ok(Empty::new()) } @@ -1491,26 +1440,18 @@ fn find_process<'a>( eid: &'a str, init: bool, ) -> Result<&'a mut Process> { - let ctr = match sandbox.get_container(cid) { - Some(v) => v, - None => return Err(anyhow!("Invalid container id")), - }; + let ctr = sandbox + .get_container(cid) + .ok_or(anyhow!("Invalid container id"))?; if init || eid == "" { - let p = match ctr.processes.get_mut(&ctr.init_process_pid) { - Some(v) => v, - None => return Err(anyhow!("cannot find init process!")), - }; - - return Ok(p); + return ctr + .processes + .get_mut(&ctr.init_process_pid) + .ok_or(anyhow!("cannot find init process!")); } - let p = match ctr.get_process(eid) { - Ok(v) => v, - Err(_) => return Err(anyhow!("Invalid exec id")), - }; - - Ok(p) + ctr.get_process(eid).map_err(|_| anyhow!("Invalid exec id")) } pub fn start(s: Arc>, server_address: &str) -> ttrpc::Server { @@ -1554,10 +1495,10 @@ fn update_container_namespaces( spec: &mut Spec, sandbox_pidns: bool, ) -> Result<()> { - let linux = match spec.linux.as_mut() { - None => return Err(anyhow!("Spec didn't container linux field")), - Some(l) => l, - }; + let linux = spec + .linux + .as_mut() + .ok_or(anyhow!("Spec didn't container linux field"))?; let namespaces = linux.namespaces.as_mut_slice(); for namespace in namespaces.iter_mut() {