Skip to content

Commit

Permalink
agent: Fix cargo 1.54 clippy warning
Browse files Browse the repository at this point in the history
Mostly the needless borrow one, plus a few others that are now enforced.

Fixes #2408

Signed-off-by: Samuel Ortiz <samuel.e.ortiz@protonmail.com>
  • Loading branch information
sameo committed Aug 5, 2021
1 parent b4b8431 commit 233b53c
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 72 deletions.
10 changes: 5 additions & 5 deletions src/agent/rustjail/src/cgroups/fs/mod.rs
Expand Up @@ -232,19 +232,19 @@ fn set_devices_resources(
let mut devices = vec![];

for d in device_resources.iter() {
if let Some(dev) = linux_device_group_to_cgroup_device(&d) {
if let Some(dev) = linux_device_group_to_cgroup_device(d) {
devices.push(dev);
}
}

for d in DEFAULT_DEVICES.iter() {
if let Some(dev) = linux_device_to_cgroup_device(&d) {
if let Some(dev) = linux_device_to_cgroup_device(d) {
devices.push(dev);
}
}

for d in DEFAULT_ALLOWED_DEVICES.iter() {
if let Some(dev) = linux_device_group_to_cgroup_device(&d) {
if let Some(dev) = linux_device_group_to_cgroup_device(d) {
devices.push(dev);
}
}
Expand Down Expand Up @@ -828,7 +828,7 @@ fn get_blkio_stats_v2(cg: &cgroups::Cgroup) -> SingularPtrField<BlkioStats> {

fn get_blkio_stats(cg: &cgroups::Cgroup) -> SingularPtrField<BlkioStats> {
if cg.v2() {
return get_blkio_stats_v2(&cg);
return get_blkio_stats_v2(cg);
}

let blkio_controller: &BlkIoController = get_controller_or_return_singular_none!(cg);
Expand Down Expand Up @@ -1022,7 +1022,7 @@ impl Manager {
.unwrap()
.trim_start_matches(root_path.to_str().unwrap());
info!(sl!(), "updating cpuset for parent path {:?}", &r_path);
let cg = new_cgroup(cgroups::hierarchies::auto(), &r_path);
let cg = new_cgroup(cgroups::hierarchies::auto(), r_path);
let cpuset_controller: &CpuSetController = cg.controller_of().unwrap();
cpuset_controller.set_cpus(guest_cpuset)?;
}
Expand Down
6 changes: 3 additions & 3 deletions src/agent/rustjail/src/container.rs
Expand Up @@ -390,7 +390,7 @@ fn do_init_child(cwfd: RawFd) -> Result<()> {
let linux = spec.linux.as_ref().unwrap();

// get namespace vector to join/new
let nses = get_namespaces(&linux);
let nses = get_namespaces(linux);

let mut userns = false;
let mut to_new = CloneFlags::empty();
Expand Down Expand Up @@ -939,7 +939,7 @@ impl BaseContainer for LinuxContainer {

join_namespaces(
&logger,
&spec,
spec,
&p,
self.cgroup_manager.as_ref().unwrap(),
&st,
Expand Down Expand Up @@ -1031,7 +1031,7 @@ impl BaseContainer for LinuxContainer {
let fifo = format!("{}/{}", &self.root, EXEC_FIFO_FILENAME);
let fd = fcntl::open(fifo.as_str(), OFlag::O_WRONLY, Mode::from_bits_truncate(0))?;
let data: &[u8] = &[0];
unistd::write(fd, &data)?;
unistd::write(fd, data)?;
info!(self.logger, "container started");
self.init_process_start_time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
Expand Down
8 changes: 4 additions & 4 deletions src/agent/rustjail/src/mount.rs
Expand Up @@ -189,7 +189,7 @@ pub fn init_rootfs(

let mut bind_mount_dev = false;
for m in &spec.mounts {
let (mut flags, pgflags, data) = parse_mount(&m);
let (mut flags, pgflags, data) = parse_mount(m);
if !m.destination.starts_with('/') || m.destination.contains("..") {
return Err(anyhow!(
"the mount destination {} is invalid",
Expand All @@ -198,7 +198,7 @@ pub fn init_rootfs(
}

if m.r#type == "cgroup" {
mount_cgroups(cfd_log, &m, rootfs, flags, &data, cpath, mounts)?;
mount_cgroups(cfd_log, m, rootfs, flags, &data, cpath, mounts)?;
} else {
if m.destination == "/dev" {
if m.r#type == "bind" {
Expand Down Expand Up @@ -226,7 +226,7 @@ pub fn init_rootfs(
}
}

mount_from(cfd_log, &m, &rootfs, flags, &data, "")?;
mount_from(cfd_log, m, rootfs, flags, &data, "")?;
// bind mount won't change mount options, we need remount to make mount options
// effective.
// first check that we have non-default options required before attempting a
Expand Down Expand Up @@ -356,7 +356,7 @@ fn mount_cgroups(
mounts: &HashMap<String, String>,
) -> Result<()> {
if cgroups::hierarchies::is_cgroup2_unified_mode() {
return mount_cgroups_v2(cfd_log, &m, rootfs, flags);
return mount_cgroups_v2(cfd_log, m, rootfs, flags);
}
// mount tmpfs
let ctm = Mount {
Expand Down
2 changes: 1 addition & 1 deletion src/agent/rustjail/src/validator.rs
Expand Up @@ -266,7 +266,7 @@ pub fn validate(conf: &Config) -> Result<()> {
security(oci).context("security")?;
usernamespace(oci).context("usernamespace")?;
cgroupnamespace(oci).context("cgroupnamespace")?;
sysctl(&oci).context("sysctl")?;
sysctl(oci).context("sysctl")?;

if conf.rootless_euid {
rootless_euid(oci).context("rootless euid")?;
Expand Down
10 changes: 5 additions & 5 deletions src/agent/src/config.rs
Expand Up @@ -372,8 +372,8 @@ mod tests {
#[test]
fn test_new() {
let config = AgentConfig::new();
assert_eq!(config.debug_console, false);
assert_eq!(config.dev_mode, false);
assert!(!config.debug_console);
assert!(!config.dev_mode);
assert_eq!(config.log_level, DEFAULT_LOG_LEVEL);
assert_eq!(config.hotplug_timeout, DEFAULT_HOTPLUG_TIMEOUT);
}
Expand Down Expand Up @@ -754,9 +754,9 @@ mod tests {
}

let mut config = AgentConfig::new();
assert_eq!(config.debug_console, false, "{}", msg);
assert_eq!(config.dev_mode, false, "{}", msg);
assert_eq!(config.unified_cgroup_hierarchy, false, "{}", msg);
assert!(!config.debug_console, "{}", msg);
assert!(!config.dev_mode, "{}", msg);
assert!(!config.unified_cgroup_hierarchy, "{}", msg);
assert_eq!(
config.hotplug_timeout,
time::Duration::from_secs(3),
Expand Down
8 changes: 4 additions & 4 deletions src/agent/src/device.rs
Expand Up @@ -966,12 +966,12 @@ mod tests {
uev_a.subsystem = "block".to_string();
uev_a.devname = devname.to_string();
uev_a.devpath = format!("{}{}/virtio4/block/{}", root_bus, relpath_a, devname);
let matcher_a = VirtioBlkPciMatcher::new(&relpath_a);
let matcher_a = VirtioBlkPciMatcher::new(relpath_a);

let mut uev_b = uev_a.clone();
let relpath_b = "/0000:00:0a.0/0000:00:0b.0";
uev_b.devpath = format!("{}{}/virtio0/block/{}", root_bus, relpath_b, devname);
let matcher_b = VirtioBlkPciMatcher::new(&relpath_b);
let matcher_b = VirtioBlkPciMatcher::new(relpath_b);

assert!(matcher_a.is_match(&uev_a));
assert!(matcher_b.is_match(&uev_b));
Expand Down Expand Up @@ -1053,15 +1053,15 @@ mod tests {
"{}/0000:00:00.0/virtio0/host0/target0:0:0/0:0:{}/block/sda",
root_bus, addr_a
);
let matcher_a = ScsiBlockMatcher::new(&addr_a);
let matcher_a = ScsiBlockMatcher::new(addr_a);

let mut uev_b = uev_a.clone();
let addr_b = "2:0";
uev_b.devpath = format!(
"{}/0000:00:00.0/virtio0/host0/target0:0:2/0:0:{}/block/sdb",
root_bus, addr_b
);
let matcher_b = ScsiBlockMatcher::new(&addr_b);
let matcher_b = ScsiBlockMatcher::new(addr_b);

assert!(matcher_a.is_match(&uev_a));
assert!(matcher_b.is_match(&uev_b));
Expand Down
2 changes: 1 addition & 1 deletion src/agent/src/main.rs
Expand Up @@ -302,7 +302,7 @@ async fn start_sandbox(
}

// Initialize unique sandbox structure.
let s = Sandbox::new(&logger).context("Failed to create sandbox")?;
let s = Sandbox::new(logger).context("Failed to create sandbox")?;
if init_mode {
s.rtnl.handle_localhost().await?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/agent/src/metrics.rs
Expand Up @@ -193,7 +193,7 @@ fn update_guest_metrics() {
Ok(kernel_stats) => {
set_gauge_vec_cpu_time(&GUEST_CPU_TIME, "total", &kernel_stats.total);
for (i, cpu_time) in kernel_stats.cpu_time.iter().enumerate() {
set_gauge_vec_cpu_time(&GUEST_CPU_TIME, format!("{}", i).as_str(), &cpu_time);
set_gauge_vec_cpu_time(&GUEST_CPU_TIME, format!("{}", i).as_str(), cpu_time);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/agent/src/mount.rs
Expand Up @@ -282,7 +282,7 @@ async fn ephemeral_storage_handler(
fs::set_permissions(&storage.mount_point, permission)?;
}
} else {
common_storage_handler(logger, &storage)?;
common_storage_handler(logger, storage)?;
}

Ok("".to_string())
Expand Down Expand Up @@ -1104,8 +1104,8 @@ mod tests {

// Create an actual mount
let bare_mount = BareMount::new(
&mnt_src_filename,
&mnt_dest_filename,
mnt_src_filename,
mnt_dest_filename,
"bind",
MsFlags::MS_BIND,
"",
Expand Down Expand Up @@ -1274,7 +1274,7 @@ mod tests {
let logger = slog::Logger::root(drain, o!());
let result = get_cgroup_mounts(&logger, "", true);

assert_eq!(true, result.is_ok());
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(1, result.len());
assert_eq!(result[0].fstype, "cgroup2");
Expand Down
2 changes: 1 addition & 1 deletion src/agent/src/namespace.rs
Expand Up @@ -102,7 +102,7 @@ impl Namespace {

let new_thread = tokio::spawn(async move {
if let Err(err) = || -> Result<()> {
let origin_ns_path = get_current_thread_ns_path(&ns_type.get());
let origin_ns_path = get_current_thread_ns_path(ns_type.get());

File::open(Path::new(&origin_ns_path))?;

Expand Down
6 changes: 3 additions & 3 deletions src/agent/src/netlink.rs
Expand Up @@ -82,8 +82,8 @@ impl Handle {

// Add new ip addresses from request
for ip_address in &iface.IPAddresses {
let ip = IpAddr::from_str(&ip_address.get_address())?;
let mask = u8::from_str_radix(ip_address.get_mask(), 10)?;
let ip = IpAddr::from_str(ip_address.get_address())?;
let mask = ip_address.get_mask().parse::<u8>()?;

self.add_addresses(link.index(), std::iter::once(IpNetwork::new(ip, mask)?))
.await?;
Expand Down Expand Up @@ -512,7 +512,7 @@ impl Handle {
.and_then(|addr| if addr.is_empty() { None } else { Some(addr) }) // Make sure it's not empty
.ok_or(nix::Error::Sys(nix::errno::Errno::EINVAL))?;

let ip = IpAddr::from_str(&ip_address)
let ip = IpAddr::from_str(ip_address)
.map_err(|e| anyhow!("Failed to parse IP {}: {:?}", ip_address, e))?;

// Import rtnetlink objects that make sense only for this function
Expand Down
9 changes: 2 additions & 7 deletions src/agent/src/network.rs
Expand Up @@ -127,16 +127,11 @@ mod tests {
// call do_setup_guest_dns
let result = do_setup_guest_dns(logger, dns.clone(), src_filename, dst_filename);

assert_eq!(
true,
result.is_ok(),
"result should be ok, but {:?}",
result
);
assert!(result.is_ok(), "result should be ok, but {:?}", result);

// get content of /etc/resolv.conf
let content = fs::read_to_string(dst_filename);
assert_eq!(true, content.is_ok());
assert!(content.is_ok());
let content = content.unwrap();

let expected_dns: Vec<&str> = content.split('\n').collect();
Expand Down
8 changes: 4 additions & 4 deletions src/agent/src/rpc.rs
Expand Up @@ -190,7 +190,7 @@ impl AgentService {
let p = if oci.process.is_some() {
Process::new(
&sl!(),
&oci.process.as_ref().unwrap(),
oci.process.as_ref().unwrap(),
cid.as_str(),
true,
pipe_size,
Expand Down Expand Up @@ -247,7 +247,7 @@ impl AgentService {
// Find the sandbox storage used by this container
let mounts = sandbox.container_mounts.get(&cid);
if let Some(mounts) = mounts {
remove_mounts(&mounts)?;
remove_mounts(mounts)?;

for m in mounts.iter() {
if sandbox.storages.get(m).is_some() {
Expand Down Expand Up @@ -666,7 +666,7 @@ impl protocols::agent_ttrpc::AgentService for AgentService {
let s = Arc::clone(&self.sandbox);
let mut sandbox = s.lock().await;

let ctr = sandbox.get_container(&cid).ok_or_else(|| {
let ctr = sandbox.get_container(cid).ok_or_else(|| {
ttrpc_error(
ttrpc::Code::INVALID_ARGUMENT,
"invalid container id".to_string(),
Expand All @@ -689,7 +689,7 @@ impl protocols::agent_ttrpc::AgentService for AgentService {
let s = Arc::clone(&self.sandbox);
let mut sandbox = s.lock().await;

let ctr = sandbox.get_container(&cid).ok_or_else(|| {
let ctr = sandbox.get_container(cid).ok_or_else(|| {
ttrpc_error(
ttrpc::Code::INVALID_ARGUMENT,
"invalid container id".to_string(),
Expand Down

0 comments on commit 233b53c

Please sign in to comment.