agent: use ok_or/map_err instead of match

Sometimes `Option.or_or` and `Result.map_err` may be simpler
than match statement. Especially in rpc.rs, there are
many `ctr.get_process` and `sandbox.get_container` which
are using `match`.

Signed-off-by: bin liu <bin@hyper.sh>
This commit is contained in:
bin liu 2020-10-12 16:59:02 +08:00
parent 6b9f99156e
commit 11c1ab8bca

View File

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