From f750750b9de7c0009d9b1fdd9f70081d617b422f Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Thu, 29 Sep 2022 10:02:43 -0700 Subject: [PATCH 1/2] Update rustfmt to sort import Signed-off-by: Maksym Pavlenko --- .github/workflows/ci.yml | 6 +++++- rustfmt.toml | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ba97bf3..5489b59 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,9 +22,13 @@ jobs: repo-token: ${{ secrets.GITHUB_TOKEN }} - run: cargo check --examples --tests --all-targets - run: cargo check --examples --tests --all-targets --all-features - - run: cargo fmt --all -- --check --files-with-diff + + - run: rustup toolchain install nightly --component rustfmt + - run: cargo +nightly fmt --all -- --check --files-with-diff + - run: cargo clippy --all-targets -- -D warnings - run: cargo clippy --all-targets --all-features -- -D warnings + - run: cargo doc --no-deps env: RUSTDOCFLAGS: -Dwarnings diff --git a/rustfmt.toml b/rustfmt.toml index 43d4840..80eda74 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1,4 @@ newline_style = "Unix" +unstable_features = true # Cargo fmt now needs to be called with `cargo +nightly fmt` +group_imports = "StdExternalCrate" # Create 3 groups: std, external crates, and self. +imports_granularity = "Crate" # Merge imports from the same crate into a single use statement From 5ddd3e37bb6afd593708c70f70437833f9898099 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Thu, 29 Sep 2022 10:06:14 -0700 Subject: [PATCH 2/2] Run fmt Signed-off-by: Maksym Pavlenko --- crates/client/build.rs | 4 +- crates/client/examples/container.rs | 25 +++--- crates/client/examples/version.rs | 3 +- crates/client/src/lib.rs | 1 + crates/logging/examples/journal.rs | 6 +- crates/logging/src/lib.rs | 6 +- crates/runc-shim/src/asynchronous/mod.rs | 45 ++++++----- crates/runc-shim/src/asynchronous/runc.rs | 70 +++++++++-------- crates/runc-shim/src/common.rs | 36 +++++---- crates/runc-shim/src/synchronous/container.rs | 47 ++++++------ crates/runc-shim/src/synchronous/io.rs | 15 ++-- crates/runc-shim/src/synchronous/runc.rs | 76 ++++++++++--------- crates/runc-shim/src/synchronous/service.rs | 58 ++++++++------ crates/runc-shim/src/synchronous/task.rs | 32 ++++---- crates/runc/src/container.rs | 3 +- crates/runc/src/error.rs | 4 +- crates/runc/src/io.rs | 15 ++-- crates/runc/src/lib.rs | 27 ++++--- crates/runc/src/monitor.rs | 9 ++- crates/runc/src/options.rs | 13 ++-- crates/runc/src/utils.rs | 6 +- crates/shim-protos/build.rs | 10 ++- crates/shim-protos/examples/connect-async.rs | 7 +- crates/shim-protos/examples/connect.rs | 3 +- .../examples/ttrpc-client-async.rs | 9 ++- crates/shim-protos/examples/ttrpc-client.rs | 7 +- .../examples/ttrpc-server-async.rs | 9 ++- crates/shim-protos/examples/ttrpc-server.rs | 3 +- crates/shim-protos/src/lib.rs | 16 ++-- crates/shim-protos/src/shim.rs | 5 +- crates/shim-protos/tests/ttrpc.rs | 15 ++-- crates/shim/examples/publish.rs | 3 +- crates/shim/examples/skeleton.rs | 9 ++- crates/shim/examples/skeleton_async.rs | 16 ++-- crates/shim/src/asynchronous/console.rs | 7 +- crates/shim/src/asynchronous/container.rs | 13 ++-- crates/shim/src/asynchronous/mod.rs | 60 ++++++++------- crates/shim/src/asynchronous/monitor.rs | 21 +++-- crates/shim/src/asynchronous/processes.rs | 16 ++-- crates/shim/src/asynchronous/publisher.rs | 47 +++++++----- crates/shim/src/asynchronous/task.rs | 51 +++++++------ crates/shim/src/asynchronous/util.rs | 19 ++--- crates/shim/src/cgroup.rs | 21 +++-- crates/shim/src/error.rs | 6 +- crates/shim/src/event.rs | 3 +- crates/shim/src/lib.rs | 31 ++++---- crates/shim/src/logger.rs | 14 ++-- crates/shim/src/mount.rs | 10 ++- crates/shim/src/synchronous/console.rs | 13 ++-- crates/shim/src/synchronous/mod.rs | 51 +++++++------ crates/shim/src/synchronous/monitor.rs | 16 ++-- crates/shim/src/synchronous/publisher.rs | 33 ++++---- crates/shim/src/synchronous/util.rs | 17 +++-- crates/shim/src/util.rs | 23 +++--- crates/snapshots/build.rs | 4 +- crates/snapshots/examples/snapshotter.rs | 11 +-- crates/snapshots/src/convert.rs | 3 +- crates/snapshots/src/lib.rs | 5 +- crates/snapshots/src/wrap.rs | 13 ++-- 59 files changed, 592 insertions(+), 529 deletions(-) diff --git a/crates/client/build.rs b/crates/client/build.rs index b4e29df..38c92c6 100644 --- a/crates/client/build.rs +++ b/crates/client/build.rs @@ -14,9 +14,7 @@ limitations under the License. */ -use std::env; -use std::fs; -use std::io; +use std::{env, fs, io}; const PROTO_FILES: &[&str] = &[ // Types diff --git a/crates/client/examples/container.rs b/crates/client/examples/container.rs index 8265ac1..bb93764 100644 --- a/crates/client/examples/container.rs +++ b/crates/client/examples/container.rs @@ -14,25 +14,20 @@ limitations under the License. */ +use std::{fs, fs::File}; + +use client::{ + services::v1::{ + container::Runtime, containers_client::ContainersClient, tasks_client::TasksClient, + Container, CreateContainerRequest, CreateTaskRequest, DeleteContainerRequest, + DeleteTaskRequest, StartRequest, WaitRequest, + }, + with_namespace, +}; use containerd_client as client; - -use client::services::v1::container::Runtime; -use client::services::v1::containers_client::ContainersClient; -use client::services::v1::Container; -use client::services::v1::{CreateContainerRequest, DeleteContainerRequest}; - -use client::services::v1::tasks_client::TasksClient; -use client::services::v1::{CreateTaskRequest, DeleteTaskRequest, StartRequest, WaitRequest}; - use prost_types::Any; - use tonic::Request; -use std::fs; -use std::fs::File; - -use client::with_namespace; - const CID: &str = "abc123"; const NAMESPACE: &str = "default"; diff --git a/crates/client/examples/version.rs b/crates/client/examples/version.rs index a6c73fb..25cc17a 100644 --- a/crates/client/examples/version.rs +++ b/crates/client/examples/version.rs @@ -14,9 +14,8 @@ limitations under the License. */ -use containerd_client as client; - use client::services::v1::version_client::VersionClient; +use containerd_client as client; /// Make sure you run containerd before running this example. #[tokio::main(flavor = "current_thread")] diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index fe65733..da331b5 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -69,6 +69,7 @@ pub async fn connect( path: impl AsRef, ) -> Result { use std::convert::TryFrom; + use tokio::net::UnixStream; use tonic::transport::Endpoint; diff --git a/crates/logging/examples/journal.rs b/crates/logging/examples/journal.rs index 4d15ee2..528816c 100644 --- a/crates/logging/examples/journal.rs +++ b/crates/logging/examples/journal.rs @@ -14,13 +14,9 @@ limitations under the License. */ -use std::fs; -use std::io; -use std::io::BufRead; -use std::thread; +use std::{fs, io, io::BufRead, thread}; use containerd_shim_logging as logging; - use logging::{Config, Driver}; fn pump(reader: fs::File) { diff --git a/crates/logging/src/lib.rs b/crates/logging/src/lib.rs index 9d2f8dd..67ebd51 100644 --- a/crates/logging/src/lib.rs +++ b/crates/logging/src/lib.rs @@ -23,11 +23,7 @@ //! This crates replicates APIs provided by Go [version](https://github.com/containerd/containerd/blob/main/runtime/v2/README.md#logging). //! -use std::env; -use std::fmt; -use std::fs; -use std::os::unix::io::FromRawFd; -use std::process; +use std::{env, fmt, fs, os::unix::io::FromRawFd, process}; /// Logging binary configuration received from containerd. #[derive(Debug)] diff --git a/crates/runc-shim/src/asynchronous/mod.rs b/crates/runc-shim/src/asynchronous/mod.rs index f42e269..cfb3e38 100644 --- a/crates/runc-shim/src/asynchronous/mod.rs +++ b/crates/runc-shim/src/asynchronous/mod.rs @@ -14,33 +14,36 @@ limitations under the License. */ -use std::env::current_dir; -use std::sync::Arc; +use std::{env::current_dir, sync::Arc}; +use ::runc::options::DeleteOpts; use async_trait::async_trait; +use containerd_shim::{ + asynchronous::{ + container::Container, + monitor::{monitor_subscribe, monitor_unsubscribe, Subscription}, + processes::Process, + publisher::RemotePublisher, + spawn, + task::TaskService, + ExitSignal, Shim, + }, + event::Event, + io_error, + monitor::{Subject, Topic}, + protos::{events::task::TaskExit, protobuf::MessageDyn}, + util::{ + convert_to_timestamp, read_options, read_runtime, read_spec, timestamp, write_str_to_file, + }, + Config, Context, DeleteResponse, Error, StartOpts, +}; use log::{debug, error, warn}; use tokio::sync::mpsc::{channel, Receiver, Sender}; -use ::runc::options::DeleteOpts; -use containerd_shim::asynchronous::container::Container; -use containerd_shim::asynchronous::monitor::{ - monitor_subscribe, monitor_unsubscribe, Subscription, +use crate::{ + asynchronous::runc::{RuncContainer, RuncFactory}, + common::{create_runc, has_shared_pid_namespace, ShimExecutor, GROUP_LABELS}, }; -use containerd_shim::asynchronous::processes::Process; -use containerd_shim::asynchronous::publisher::RemotePublisher; -use containerd_shim::asynchronous::task::TaskService; -use containerd_shim::asynchronous::{spawn, ExitSignal, Shim}; -use containerd_shim::event::Event; -use containerd_shim::monitor::{Subject, Topic}; -use containerd_shim::protos::events::task::TaskExit; -use containerd_shim::protos::protobuf::MessageDyn; -use containerd_shim::util::{convert_to_timestamp, timestamp}; -use containerd_shim::util::{read_options, read_runtime, read_spec, write_str_to_file}; -use containerd_shim::{io_error, Config, Context, DeleteResponse, Error, StartOpts}; - -use crate::asynchronous::runc::{RuncContainer, RuncFactory}; -use crate::common::{create_runc, has_shared_pid_namespace}; -use crate::common::{ShimExecutor, GROUP_LABELS}; mod runc; diff --git a/crates/runc-shim/src/asynchronous/runc.rs b/crates/runc-shim/src/asynchronous/runc.rs index 2f62090..7363d19 100644 --- a/crates/runc-shim/src/asynchronous/runc.rs +++ b/crates/runc-shim/src/asynchronous/runc.rs @@ -14,46 +14,50 @@ limitations under the License. */ -use std::convert::TryFrom; -use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; -use std::os::unix::prelude::ExitStatusExt; -use std::path::{Path, PathBuf}; -use std::process::ExitStatus; -use std::sync::Arc; +use std::{ + convert::TryFrom, + os::unix::{ + io::{AsRawFd, FromRawFd, RawFd}, + prelude::ExitStatusExt, + }, + path::{Path, PathBuf}, + process::ExitStatus, + sync::Arc, +}; use async_trait::async_trait; +use containerd_shim::{ + api::{CreateTaskRequest, ExecProcessRequest, Options, Status}, + asynchronous::{ + console::ConsoleSocket, + container::{ContainerFactory, ContainerTemplate, ProcessFactory}, + monitor::{monitor_subscribe, monitor_unsubscribe, Subscription}, + processes::{ProcessLifecycle, ProcessTemplate}, + }, + io::Stdio, + io_error, + monitor::{ExitEvent, Subject, Topic}, + other, other_error, + protos::{ + api::ProcessInfo, + cgroups::metrics::Metrics, + protobuf::{CodedInputStream, Message}, + }, + util::{asyncify, mkdir, mount_rootfs, read_file_to_str, write_options, write_runtime}, + Console, Error, ExitSignal, Result, +}; use log::{debug, error}; -use nix::sys::signal::kill; -use nix::unistd::Pid; +use nix::{sys::signal::kill, unistd::Pid}; use oci_spec::runtime::{LinuxResources, Process}; -use tokio::fs::{File, OpenOptions}; -use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite}; - -use containerd_shim::api::{CreateTaskRequest, ExecProcessRequest, Options, Status}; -use containerd_shim::asynchronous::console::ConsoleSocket; -use containerd_shim::asynchronous::container::{ - ContainerFactory, ContainerTemplate, ProcessFactory, -}; -use containerd_shim::asynchronous::monitor::{ - monitor_subscribe, monitor_unsubscribe, Subscription, -}; -use containerd_shim::asynchronous::processes::{ProcessLifecycle, ProcessTemplate}; -use containerd_shim::io::Stdio; -use containerd_shim::monitor::{ExitEvent, Subject, Topic}; -use containerd_shim::protos::api::ProcessInfo; -use containerd_shim::protos::cgroups::metrics::Metrics; -use containerd_shim::protos::protobuf::{CodedInputStream, Message}; -use containerd_shim::util::{ - asyncify, mkdir, mount_rootfs, read_file_to_str, write_options, write_runtime, -}; -use containerd_shim::{io_error, other, other_error, Console, Error, ExitSignal, Result}; use runc::{Command, Runc, Spawner}; +use tokio::{ + fs::{File, OpenOptions}, + io::{AsyncRead, AsyncReadExt, AsyncWrite}, +}; -use crate::common::receive_socket; -use crate::common::CreateConfig; use crate::common::{ - check_kill_error, create_io, create_runc, get_spec_from_request, ProcessIO, ShimExecutor, - INIT_PID_FILE, + check_kill_error, create_io, create_runc, get_spec_from_request, receive_socket, CreateConfig, + ProcessIO, ShimExecutor, INIT_PID_FILE, }; pub type ExecProcess = ProcessTemplate; diff --git a/crates/runc-shim/src/common.rs b/crates/runc-shim/src/common.rs index 3687fa7..936e9ad 100644 --- a/crates/runc-shim/src/common.rs +++ b/crates/runc-shim/src/common.rs @@ -14,25 +14,29 @@ limitations under the License. */ -use std::io::IoSliceMut; -use std::ops::Deref; -use std::os::unix::io::RawFd; -use std::path::Path; -use std::sync::Arc; +use std::{io::IoSliceMut, ops::Deref, os::unix::io::RawFd, path::Path, sync::Arc}; +use containerd_shim::{ + api::{ExecProcessRequest, Options}, + io::Stdio, + io_error, other, other_error, + util::IntoOption, + Error, +}; use log::{debug, warn}; -use nix::cmsg_space; -use nix::sys::socket::{recvmsg, ControlMessageOwned, MsgFlags, UnixAddr}; -use nix::sys::termios::tcgetattr; +use nix::{ + cmsg_space, + sys::{ + socket::{recvmsg, ControlMessageOwned, MsgFlags, UnixAddr}, + termios::tcgetattr, + }, +}; use oci_spec::runtime::{LinuxNamespaceType, Spec}; - -use containerd_shim::api::{ExecProcessRequest, Options}; -use containerd_shim::io::Stdio; -use containerd_shim::util::IntoOption; -use containerd_shim::{io_error, other, other_error, Error}; -use runc::io::{Io, NullIo, FIFO}; -use runc::options::GlobalOpts; -use runc::{Runc, Spawner}; +use runc::{ + io::{Io, NullIo, FIFO}, + options::GlobalOpts, + Runc, Spawner, +}; pub const GROUP_LABELS: [&str; 2] = [ "io.containerd.runc.v2.group", diff --git a/crates/runc-shim/src/synchronous/container.rs b/crates/runc-shim/src/synchronous/container.rs index 22b7182..d54b97c 100644 --- a/crates/runc-shim/src/synchronous/container.rs +++ b/crates/runc-shim/src/synchronous/container.rs @@ -14,31 +14,34 @@ limitations under the License. */ -use std::collections::HashMap; -use std::convert::TryFrom; -use std::fs::{File, OpenOptions}; -use std::os::unix::io::{AsRawFd, FromRawFd}; -use std::path::Path; -use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; - -use log::debug; -use oci_spec::runtime::LinuxResources; -use time::OffsetDateTime; +use std::{ + collections::HashMap, + convert::TryFrom, + fs::{File, OpenOptions}, + os::unix::io::{AsRawFd, FromRawFd}, + path::Path, + sync::mpsc::{sync_channel, Receiver, SyncSender}, +}; use containerd_shim as shim; -use shim::api::*; -use shim::console::ConsoleSocket; -use shim::error::{Error, Result}; -use shim::io::Stdio; -use shim::ioctl_set_winsz; -use shim::protos::cgroups::metrics::Metrics; -use shim::util::{convert_to_timestamp, read_pid_from_file}; -use shim::Console; -use shim::{io_error, other, other_error}; +use log::debug; +use oci_spec::runtime::LinuxResources; +use shim::{ + api::*, + console::ConsoleSocket, + error::{Error, Result}, + io::Stdio, + io_error, ioctl_set_winsz, other, other_error, + protos::cgroups::metrics::Metrics, + util::{convert_to_timestamp, read_pid_from_file}, + Console, +}; +use time::OffsetDateTime; -use crate::common::receive_socket; -use crate::common::ProcessIO; -use crate::synchronous::io::spawn_copy_for_tty; +use crate::{ + common::{receive_socket, ProcessIO}, + synchronous::io::spawn_copy_for_tty, +}; pub trait ContainerFactory { fn create(&self, ns: &str, req: &CreateTaskRequest) -> Result; diff --git a/crates/runc-shim/src/synchronous/io.rs b/crates/runc-shim/src/synchronous/io.rs index 5781c09..2c81980 100644 --- a/crates/runc-shim/src/synchronous/io.rs +++ b/crates/runc-shim/src/synchronous/io.rs @@ -14,18 +14,19 @@ limitations under the License. */ -use std::fs::OpenOptions; -use std::io::{ErrorKind, Read, Write}; -use std::thread::JoinHandle; +use std::{ + fs::OpenOptions, + io::{ErrorKind, Read, Write}, + thread::JoinHandle, +}; -use crossbeam::sync::WaitGroup; -use log::debug; - -use containerd_shim::io::Stdio; use containerd_shim::{ error::{Error, Result}, + io::Stdio, io_error, }; +use crossbeam::sync::WaitGroup; +use log::debug; use crate::common::ProcessIO; diff --git a/crates/runc-shim/src/synchronous/runc.rs b/crates/runc-shim/src/synchronous/runc.rs index dbc7299..3459004 100644 --- a/crates/runc-shim/src/synchronous/runc.rs +++ b/crates/runc-shim/src/synchronous/runc.rs @@ -15,43 +15,51 @@ */ #![allow(unused)] -use std::convert::TryFrom; -use std::io::Read; -use std::os::unix::prelude::ExitStatusExt; -use std::path::{Path, PathBuf}; -use std::process::ExitStatus; -use std::sync::mpsc::{Receiver, SyncSender}; -use std::sync::Arc; - -use log::{debug, error}; -use nix::sys::signal::kill; -use nix::sys::stat::Mode; -use nix::unistd::{mkdir, Pid}; -use oci_spec::runtime::{LinuxNamespaceType, LinuxResources}; -use time::OffsetDateTime; +use std::{ + convert::TryFrom, + io::Read, + os::unix::prelude::ExitStatusExt, + path::{Path, PathBuf}, + process::ExitStatus, + sync::{ + mpsc::{Receiver, SyncSender}, + Arc, + }, +}; use containerd_shim as shim; -use runc::{Command, Spawner}; -use shim::api::*; -use shim::console::ConsoleSocket; -use shim::error::{Error, Result}; -use shim::io::Stdio; -use shim::monitor::{monitor_subscribe, wait_pid, ExitEvent, Subject, Subscription, Topic}; -use shim::mount::mount_rootfs; -use shim::protos::api::ProcessInfo; -use shim::protos::cgroups::metrics::Metrics; -use shim::protos::protobuf::{CodedInputStream, Message}; -use shim::protos::shim::oci::ProcessDetails; -use shim::util::{convert_to_any, read_spec_from_file, write_options, write_runtime, IntoOption}; -use shim::Console; -use shim::{other, other_error}; - -use crate::common; -use crate::common::{ - create_io, has_shared_pid_namespace, CreateConfig, ShimExecutor, INIT_PID_FILE, +use log::{debug, error}; +use nix::{ + sys::{signal::kill, stat::Mode}, + unistd::{mkdir, Pid}, }; -use crate::synchronous::container::{ - CommonContainer, CommonProcess, Container, ContainerFactory, Process, +use oci_spec::runtime::{LinuxNamespaceType, LinuxResources}; +use runc::{Command, Spawner}; +use shim::{ + api::*, + console::ConsoleSocket, + error::{Error, Result}, + io::Stdio, + monitor::{monitor_subscribe, wait_pid, ExitEvent, Subject, Subscription, Topic}, + mount::mount_rootfs, + other, other_error, + protos::{ + api::ProcessInfo, + cgroups::metrics::Metrics, + protobuf::{CodedInputStream, Message}, + shim::oci::ProcessDetails, + }, + util::{convert_to_any, read_spec_from_file, write_options, write_runtime, IntoOption}, + Console, +}; +use time::OffsetDateTime; + +use crate::{ + common, + common::{create_io, has_shared_pid_namespace, CreateConfig, ShimExecutor, INIT_PID_FILE}, + synchronous::container::{ + CommonContainer, CommonProcess, Container, ContainerFactory, Process, + }, }; #[derive(Clone, Default)] diff --git a/crates/runc-shim/src/synchronous/service.rs b/crates/runc-shim/src/synchronous/service.rs index 8236eef..d39217f 100644 --- a/crates/runc-shim/src/synchronous/service.rs +++ b/crates/runc-shim/src/synchronous/service.rs @@ -16,33 +16,47 @@ #![allow(unused)] -use std::env::current_dir; -use std::path::Path; -use std::sync::mpsc::{channel, Receiver, Sender}; -use std::sync::Arc; - -use log::{debug, error}; +use std::{ + env::current_dir, + path::Path, + sync::{ + mpsc::{channel, Receiver, Sender}, + Arc, + }, +}; use containerd_shim as shim; +use log::{debug, error}; use runc::options::{DeleteOpts, GlobalOpts, DEFAULT_COMMAND}; -use shim::api::*; -use shim::error::{Error, Result}; -use shim::event::Event; -use shim::monitor::{monitor_subscribe, Subject, Subscription, Topic}; -use shim::protos::events::task::TaskExit; -use shim::protos::protobuf::{Message, MessageDyn}; -use shim::publisher::RemotePublisher; -use shim::util::{ - convert_to_timestamp, read_options, read_runtime, read_spec_from_file, timestamp, write_address, +use shim::{ + api::*, + error::{Error, Result}, + event::Event, + io_error, + monitor::{monitor_subscribe, Subject, Subscription, Topic}, + other_error, + protos::{ + events::task::TaskExit, + protobuf::{Message, MessageDyn}, + }, + publisher::RemotePublisher, + spawn, + util::{ + convert_to_timestamp, read_options, read_runtime, read_spec_from_file, timestamp, + write_address, + }, + warn, Config, Context, ExitSignal, Shim, StartOpts, }; -use shim::{io_error, other_error, warn}; -use shim::{spawn, Config, Context, ExitSignal, Shim, StartOpts}; -use crate::common::{create_runc, ShimExecutor, GROUP_LABELS}; -use crate::synchronous::container::{Container, Process}; -use crate::synchronous::runc::{RuncContainer, RuncFactory}; -use crate::synchronous::task::ShimTask; -use crate::synchronous::Service; +use crate::{ + common::{create_runc, ShimExecutor, GROUP_LABELS}, + synchronous::{ + container::{Container, Process}, + runc::{RuncContainer, RuncFactory}, + task::ShimTask, + Service, + }, +}; impl Shim for Service { type T = ShimTask; diff --git a/crates/runc-shim/src/synchronous/task.rs b/crates/runc-shim/src/synchronous/task.rs index 3f9a2eb..2cae298 100644 --- a/crates/runc-shim/src/synchronous/task.rs +++ b/crates/runc-shim/src/synchronous/task.rs @@ -14,24 +14,26 @@ limitations under the License. */ -use std::collections::HashMap; -use std::process; -use std::sync::mpsc::Sender; -use std::sync::{Arc, Mutex, Once}; - -use log::{debug, info, warn}; -use oci_spec::runtime::LinuxResources; +use std::{ + collections::HashMap, + process, + sync::{mpsc::Sender, Arc, Mutex, Once}, +}; use containerd_shim as shim; - -use shim::api::*; -use shim::event::Event; -use shim::protos::events::task::{ - TaskCreate, TaskDelete, TaskExecAdded, TaskExecStarted, TaskIO, TaskStart, +use log::{debug, info, warn}; +use oci_spec::runtime::LinuxResources; +use shim::{ + api::*, + event::Event, + other_error, + protos::{ + events::task::{TaskCreate, TaskDelete, TaskExecAdded, TaskExecStarted, TaskIO, TaskStart}, + protobuf::MessageDyn, + }, + util::{convert_to_any, convert_to_timestamp, IntoOption}, + Error, ExitSignal, Task, TtrpcContext, TtrpcResult, }; -use shim::protos::protobuf::MessageDyn; -use shim::util::{convert_to_any, convert_to_timestamp, IntoOption}; -use shim::{other_error, Error, ExitSignal, Task, TtrpcContext, TtrpcResult}; use crate::synchronous::container::{Container, ContainerFactory}; diff --git a/crates/runc/src/container.rs b/crates/runc/src/container.rs index 1351ca2..0f846c7 100644 --- a/crates/runc/src/container.rs +++ b/crates/runc/src/container.rs @@ -36,8 +36,7 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; -use time::serde::timestamp; -use time::OffsetDateTime; +use time::{serde::timestamp, OffsetDateTime}; /// Information for runc container #[derive(Debug, Serialize, Deserialize)] diff --git a/crates/runc/src/error.rs b/crates/runc/src/error.rs index 4204952..86242d9 100644 --- a/crates/runc/src/error.rs +++ b/crates/runc/src/error.rs @@ -33,9 +33,7 @@ * limitations under the License. */ -use std::env; -use std::io; -use std::process::ExitStatus; +use std::{env, io, process::ExitStatus}; use thiserror::Error; diff --git a/crates/runc/src/io.rs b/crates/runc/src/io.rs index 63d6fd1..d95c8b1 100644 --- a/crates/runc/src/io.rs +++ b/crates/runc/src/io.rs @@ -13,15 +13,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -use std::fmt::Debug; -use std::fs::{File, OpenOptions}; -use std::io::Result; #[cfg(not(feature = "async"))] use std::io::{Read, Write}; -use std::os::unix::fs::OpenOptionsExt; -use std::os::unix::io::AsRawFd; -use std::process::Stdio; -use std::sync::Mutex; +use std::{ + fmt::Debug, + fs::{File, OpenOptions}, + io::Result, + os::unix::{fs::OpenOptionsExt, io::AsRawFd}, + process::Stdio, + sync::Mutex, +}; use log::debug; use nix::unistd::{Gid, Uid}; diff --git a/crates/runc/src/lib.rs b/crates/runc/src/lib.rs index a1e6288..fe01d49 100644 --- a/crates/runc/src/lib.rs +++ b/crates/runc/src/lib.rs @@ -35,10 +35,12 @@ //! A crate for consuming the runc binary in your Rust applications, similar to //! [go-runc](https://github.com/containerd/go-runc) for Go. -use std::fmt::{self, Debug, Display}; -use std::path::{Path, PathBuf}; -use std::process::{ExitStatus, Stdio}; -use std::sync::Arc; +use std::{ + fmt::{self, Debug, Display}, + path::{Path, PathBuf}, + process::{ExitStatus, Stdio}, + sync::Arc, +}; #[cfg(feature = "async")] use async_trait::async_trait; @@ -46,10 +48,7 @@ use async_trait::async_trait; use log::debug; use oci_spec::runtime::{LinuxResources, Process}; -use crate::container::Container; -use crate::error::Error; -use crate::options::*; -use crate::utils::write_value_to_temp_file; +use crate::{container::Container, error::Error, options::*, utils::write_value_to_temp_file}; pub mod container; pub mod error; @@ -603,8 +602,10 @@ impl Runc { mod tests { use std::sync::Arc; - use super::io::{InheritedStdIo, PipedStdIo}; - use super::*; + use super::{ + io::{InheritedStdIo, PipedStdIo}, + *, + }; fn ok_client() -> Runc { GlobalOpts::new() @@ -786,8 +787,10 @@ mod tests { mod tests { use std::sync::Arc; - use super::io::{InheritedStdIo, PipedStdIo}; - use super::*; + use super::{ + io::{InheritedStdIo, PipedStdIo}, + *, + }; fn ok_client() -> Runc { GlobalOpts::new() diff --git a/crates/runc/src/monitor.rs b/crates/runc/src/monitor.rs index e4e3a64..cadabc1 100644 --- a/crates/runc/src/monitor.rs +++ b/crates/runc/src/monitor.rs @@ -19,8 +19,10 @@ use std::process::{ExitStatus, Output}; use async_trait::async_trait; use log::error; use time::OffsetDateTime; -use tokio::process::Command; -use tokio::sync::oneshot::{channel, Receiver, Sender}; +use tokio::{ + process::Command, + sync::oneshot::{channel, Receiver, Sender}, +}; use crate::error::Error; @@ -132,8 +134,7 @@ pub async fn execute( mod tests { use std::process::Stdio; - use tokio::process::Command; - use tokio::sync::oneshot::channel; + use tokio::{process::Command, sync::oneshot::channel}; use super::*; diff --git a/crates/runc/src/options.rs b/crates/runc/src/options.rs index ac2eb6d..2df488b 100644 --- a/crates/runc/src/options.rs +++ b/crates/runc/src/options.rs @@ -33,14 +33,13 @@ * limitations under the License. */ -use std::path::{Path, PathBuf}; -use std::sync::Arc; -use std::time::Duration; +use std::{ + path::{Path, PathBuf}, + sync::Arc, + time::Duration, +}; -use crate::error::Error; -use crate::io::Io; -use crate::{utils, DefaultExecutor, Spawner}; -use crate::{LogFormat, Runc}; +use crate::{error::Error, io::Io, utils, DefaultExecutor, LogFormat, Runc, Spawner}; // constants for log format pub const JSON: &str = "json"; diff --git a/crates/runc/src/utils.rs b/crates/runc/src/utils.rs index df52d02..750a116 100644 --- a/crates/runc/src/utils.rs +++ b/crates/runc/src/utils.rs @@ -14,10 +14,12 @@ limitations under the License. */ -use std::env; #[cfg(not(feature = "async"))] use std::io::Write; -use std::path::{Path, PathBuf}; +use std::{ + env, + path::{Path, PathBuf}, +}; use path_absolutize::*; use serde::Serialize; diff --git a/crates/shim-protos/build.rs b/crates/shim-protos/build.rs index 8ab6dfc..26d9b63 100644 --- a/crates/shim-protos/build.rs +++ b/crates/shim-protos/build.rs @@ -14,10 +14,12 @@ limitations under the License. */ -use std::fs::File; -use std::io::{BufRead, BufReader}; -use std::path::PathBuf; -use std::{env, fs}; +use std::{ + env, fs, + fs::File, + io::{BufRead, BufReader}, + path::PathBuf, +}; use ttrpc_codegen::{Codegen, Customize, ProtobufCustomize}; diff --git a/crates/shim-protos/examples/connect-async.rs b/crates/shim-protos/examples/connect-async.rs index 9c81456..665db8e 100644 --- a/crates/shim-protos/examples/connect-async.rs +++ b/crates/shim-protos/examples/connect-async.rs @@ -16,12 +16,9 @@ use std::env; +use client::{api, shim::shim_ttrpc_async::TaskClient}; use containerd_shim_protos as client; - -use client::api; -use client::shim::shim_ttrpc_async::TaskClient; -use ttrpc::asynchronous::Client; -use ttrpc::context::Context; +use ttrpc::{asynchronous::Client, context::Context}; #[tokio::main] async fn main() { diff --git a/crates/shim-protos/examples/connect.rs b/crates/shim-protos/examples/connect.rs index 2bc6f56..bc5bc3d 100644 --- a/crates/shim-protos/examples/connect.rs +++ b/crates/shim-protos/examples/connect.rs @@ -16,9 +16,8 @@ use std::env; -use containerd_shim_protos as client; - use client::api; +use containerd_shim_protos as client; use ttrpc::context::Context; fn main() { diff --git a/crates/shim-protos/examples/ttrpc-client-async.rs b/crates/shim-protos/examples/ttrpc-client-async.rs index f6497a0..e7fa657 100644 --- a/crates/shim-protos/examples/ttrpc-client-async.rs +++ b/crates/shim-protos/examples/ttrpc-client-async.rs @@ -13,10 +13,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -use containerd_shim_protos::api::CreateTaskRequest; -use containerd_shim_protos::shim::shim_ttrpc_async::TaskClient; -use ttrpc::asynchronous::Client; -use ttrpc::context::{self, Context}; +use containerd_shim_protos::{api::CreateTaskRequest, shim::shim_ttrpc_async::TaskClient}; +use ttrpc::{ + asynchronous::Client, + context::{self, Context}, +}; fn default_ctx() -> Context { let mut ctx = context::with_timeout(0); diff --git a/crates/shim-protos/examples/ttrpc-client.rs b/crates/shim-protos/examples/ttrpc-client.rs index 827fd60..217bb3e 100644 --- a/crates/shim-protos/examples/ttrpc-client.rs +++ b/crates/shim-protos/examples/ttrpc-client.rs @@ -14,9 +14,10 @@ // limitations under the License. use containerd_shim_protos::{api::CreateTaskRequest, TaskClient}; - -use ttrpc::context::{self, Context}; -use ttrpc::Client; +use ttrpc::{ + context::{self, Context}, + Client, +}; fn main() { let c = Client::connect("unix:///tmp/shim-proto-ttrpc-001").unwrap(); diff --git a/crates/shim-protos/examples/ttrpc-server-async.rs b/crates/shim-protos/examples/ttrpc-server-async.rs index c100a68..c6d373e 100644 --- a/crates/shim-protos/examples/ttrpc-server-async.rs +++ b/crates/shim-protos/examples/ttrpc-server-async.rs @@ -13,12 +13,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::sync::Arc; -use std::thread; +use std::{sync::Arc, thread}; use async_trait::async_trait; -use containerd_shim_protos::api::{CreateTaskRequest, CreateTaskResponse}; -use containerd_shim_protos::shim::shim_ttrpc_async::{create_task, Task}; +use containerd_shim_protos::{ + api::{CreateTaskRequest, CreateTaskResponse}, + shim::shim_ttrpc_async::{create_task, Task}, +}; use ttrpc::asynchronous::Server; #[derive(Debug, PartialEq)] diff --git a/crates/shim-protos/examples/ttrpc-server.rs b/crates/shim-protos/examples/ttrpc-server.rs index 058c341..7c6c736 100644 --- a/crates/shim-protos/examples/ttrpc-server.rs +++ b/crates/shim-protos/examples/ttrpc-server.rs @@ -13,8 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::sync::Arc; -use std::thread; +use std::{sync::Arc, thread}; use containerd_shim_protos::{ api::{CreateTaskRequest, CreateTaskResponse}, diff --git a/crates/shim-protos/src/lib.rs b/crates/shim-protos/src/lib.rs index de41e12..3001397 100644 --- a/crates/shim-protos/src/lib.rs +++ b/crates/shim-protos/src/lib.rs @@ -77,11 +77,10 @@ pub mod shim_sync { /// TTRPC client reexport for easier access. pub use ttrpc::Client; - /// Shim task service. - pub use crate::shim::shim_ttrpc::{create_task, Task, TaskClient}; - /// Shim events service. pub use crate::shim::events_ttrpc::{create_events, Events, EventsClient}; + /// Shim task service. + pub use crate::shim::shim_ttrpc::{create_task, Task, TaskClient}; } pub use shim_sync::*; @@ -91,18 +90,13 @@ pub mod shim_async { /// TTRPC client reexport for easier access. pub use ttrpc::asynchronous::Client; - /// Shim task service. - pub use crate::shim::shim_ttrpc_async::{create_task, Task, TaskClient}; - /// Shim events service. pub use crate::shim::events_ttrpc_async::{create_events, Events, EventsClient}; + /// Shim task service. + pub use crate::shim::shim_ttrpc_async::{create_task, Task, TaskClient}; } /// Reexport auto-generated public data structures. pub mod api { - pub use crate::shim::empty::*; - pub use crate::shim::events::*; - pub use crate::shim::mount::*; - pub use crate::shim::shim::*; - pub use crate::shim::task::*; + pub use crate::shim::{empty::*, events::*, mount::*, shim::*, task::*}; } diff --git a/crates/shim-protos/src/shim.rs b/crates/shim-protos/src/shim.rs index ee14c49..caa8fdb 100644 --- a/crates/shim-protos/src/shim.rs +++ b/crates/shim-protos/src/shim.rs @@ -64,8 +64,7 @@ mod gogo { pub use crate::types::gogo::*; } -/// Shim task service. -pub use shim_ttrpc::{create_task, Task, TaskClient}; - /// Shim events service. pub use events_ttrpc::{create_events, Events, EventsClient}; +/// Shim task service. +pub use shim_ttrpc::{create_task, Task, TaskClient}; diff --git a/crates/shim-protos/tests/ttrpc.rs b/crates/shim-protos/tests/ttrpc.rs index d668046..e8892a5 100644 --- a/crates/shim-protos/tests/ttrpc.rs +++ b/crates/shim-protos/tests/ttrpc.rs @@ -3,13 +3,16 @@ // SPDX-License-Identifier: Apache-2.0 // -use std::collections::HashMap; -use std::sync::mpsc::channel; -use std::sync::Arc; +use std::{ + collections::HashMap, + sync::{mpsc::channel, Arc}, +}; -use containerd_shim_protos::api::{CreateTaskRequest, CreateTaskResponse, DeleteRequest}; -use containerd_shim_protos::shim::shim_ttrpc::create_task; -use containerd_shim_protos::Task; +use containerd_shim_protos::{ + api::{CreateTaskRequest, CreateTaskResponse, DeleteRequest}, + shim::shim_ttrpc::create_task, + Task, +}; use protobuf::{CodedInputStream, CodedOutputStream, Message}; use ttrpc::{Code, MessageHeader, Request, Response, TtrpcContext}; diff --git a/crates/shim/examples/publish.rs b/crates/shim/examples/publish.rs index db3c370..dc70b89 100644 --- a/crates/shim/examples/publish.rs +++ b/crates/shim/examples/publish.rs @@ -15,8 +15,7 @@ */ use std::env; -use containerd_shim::publisher::RemotePublisher; -use containerd_shim::Context; +use containerd_shim::{publisher::RemotePublisher, Context}; use containerd_shim_protos::events::task::TaskOOM; #[cfg(not(feature = "async"))] diff --git a/crates/shim/examples/skeleton.rs b/crates/shim/examples/skeleton.rs index ceb1959..8f80f02 100644 --- a/crates/shim/examples/skeleton.rs +++ b/crates/shim/examples/skeleton.rs @@ -20,11 +20,12 @@ use containerd_shim as shim; mod skeleton { use std::sync::Arc; - use log::info; - use containerd_shim as shim; - use shim::synchronous::publisher::RemotePublisher; - use shim::{api, Config, DeleteResponse, ExitSignal, TtrpcContext, TtrpcResult}; + use log::info; + use shim::{ + api, synchronous::publisher::RemotePublisher, Config, DeleteResponse, ExitSignal, + TtrpcContext, TtrpcResult, + }; #[derive(Clone)] pub(crate) struct Service { diff --git a/crates/shim/examples/skeleton_async.rs b/crates/shim/examples/skeleton_async.rs index 7f54fc4..af49271 100644 --- a/crates/shim/examples/skeleton_async.rs +++ b/crates/shim/examples/skeleton_async.rs @@ -17,16 +17,16 @@ use std::sync::Arc; use async_trait::async_trait; +use containerd_shim::{ + asynchronous::{run, spawn, ExitSignal, Shim}, + publisher::RemotePublisher, + Config, Error, StartOpts, TtrpcResult, +}; +use containerd_shim_protos::{ + api, api::DeleteResponse, shim_async::Task, ttrpc::r#async::TtrpcContext, +}; use log::info; -use containerd_shim::asynchronous::{run, spawn, ExitSignal, Shim}; -use containerd_shim::publisher::RemotePublisher; -use containerd_shim::{Config, Error, StartOpts, TtrpcResult}; -use containerd_shim_protos::api; -use containerd_shim_protos::api::DeleteResponse; -use containerd_shim_protos::shim_async::Task; -use containerd_shim_protos::ttrpc::r#async::TtrpcContext; - #[derive(Clone)] struct Service { exit: Arc, diff --git a/crates/shim/src/asynchronous/console.rs b/crates/shim/src/asynchronous/console.rs index 70b8d9f..a09026e 100644 --- a/crates/shim/src/asynchronous/console.rs +++ b/crates/shim/src/asynchronous/console.rs @@ -20,9 +20,10 @@ use log::warn; use tokio::net::{UnixListener, UnixStream}; use uuid::Uuid; -use crate::util::{mkdir, xdg_runtime_dir}; -use crate::Error; -use crate::Result; +use crate::{ + util::{mkdir, xdg_runtime_dir}, + Error, Result, +}; pub struct ConsoleSocket { pub listener: UnixListener, diff --git a/crates/shim/src/asynchronous/container.rs b/crates/shim/src/asynchronous/container.rs index 8217fae..751f19b 100644 --- a/crates/shim/src/asynchronous/container.rs +++ b/crates/shim/src/asynchronous/container.rs @@ -17,19 +17,16 @@ use std::collections::HashMap; use async_trait::async_trait; +use containerd_shim_protos::{ + api::{CreateTaskRequest, ExecProcessRequest, ProcessInfo, StateResponse}, + cgroups::metrics::Metrics, +}; use log::debug; use oci_spec::runtime::LinuxResources; use time::OffsetDateTime; use tokio::sync::oneshot::Receiver; -use containerd_shim_protos::api::{ - CreateTaskRequest, ExecProcessRequest, ProcessInfo, StateResponse, -}; -use containerd_shim_protos::cgroups::metrics::Metrics; - -use crate::asynchronous::processes::Process; -use crate::error::Result; -use crate::Error; +use crate::{asynchronous::processes::Process, error::Result, Error}; #[async_trait] pub trait Container { diff --git a/crates/shim/src/asynchronous/mod.rs b/crates/shim/src/asynchronous/mod.rs index 3ccb896..43465ef 100644 --- a/crates/shim/src/asynchronous/mod.rs +++ b/crates/shim/src/asynchronous/mod.rs @@ -14,42 +14,48 @@ limitations under the License. */ -use std::convert::TryFrom; -use std::os::unix::fs::FileTypeExt; -use std::os::unix::io::AsRawFd; -use std::os::unix::net::UnixListener; -use std::path::Path; -use std::process::Command; -use std::process::Stdio; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::Arc; -use std::{env, process}; +use std::{ + convert::TryFrom, + env, + os::unix::{fs::FileTypeExt, io::AsRawFd, net::UnixListener}, + path::Path, + process, + process::{Command, Stdio}, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, + }, +}; use async_trait::async_trait; use command_fds::{CommandFdExt, FdMapping}; +use containerd_shim_protos::{ + api::DeleteResponse, + protobuf::Message, + shim_async::{create_task, Client, Task}, + ttrpc::r#async::Server, +}; use futures::StreamExt; use libc::{SIGCHLD, SIGINT, SIGPIPE, SIGTERM}; use log::{debug, error, info, warn}; -use nix::errno::Errno; -use nix::sys::signal::Signal; -use nix::sys::wait::{self, WaitPidFlag, WaitStatus}; -use nix::unistd::Pid; +use nix::{ + errno::Errno, + sys::{ + signal::Signal, + wait::{self, WaitPidFlag, WaitStatus}, + }, + unistd::Pid, +}; use signal_hook_tokio::Signals; -use tokio::io::AsyncWriteExt; -use tokio::sync::Notify; +use tokio::{io::AsyncWriteExt, sync::Notify}; -use containerd_shim_protos::api::DeleteResponse; -use containerd_shim_protos::protobuf::Message; -use containerd_shim_protos::shim_async::{create_task, Client, Task}; -use containerd_shim_protos::ttrpc::r#async::Server; - -use crate::asynchronous::monitor::monitor_notify_by_pid; -use crate::asynchronous::publisher::RemotePublisher; -use crate::error::Error; -use crate::error::Result; -use crate::util::{asyncify, read_file_to_str, write_str_to_file}; use crate::{ - args, logger, parse_sockaddr, reap, socket_address, Config, StartOpts, SOCKET_FD, TTRPC_ADDRESS, + args, + asynchronous::{monitor::monitor_notify_by_pid, publisher::RemotePublisher}, + error::{Error, Result}, + logger, parse_sockaddr, reap, socket_address, + util::{asyncify, read_file_to_str, write_str_to_file}, + Config, StartOpts, SOCKET_FD, TTRPC_ADDRESS, }; pub mod console; diff --git a/crates/shim/src/asynchronous/monitor.rs b/crates/shim/src/asynchronous/monitor.rs index 83662af..b29545f 100644 --- a/crates/shim/src/asynchronous/monitor.rs +++ b/crates/shim/src/asynchronous/monitor.rs @@ -18,12 +18,15 @@ use std::collections::HashMap; use lazy_static::lazy_static; use log::error; -use tokio::sync::mpsc::{channel, Receiver, Sender}; -use tokio::sync::Mutex; +use tokio::sync::{ + mpsc::{channel, Receiver, Sender}, + Mutex, +}; -use crate::error::Error; -use crate::error::Result; -use crate::monitor::{ExitEvent, Subject, Topic}; +use crate::{ + error::{Error, Result}, + monitor::{ExitEvent, Subject, Topic}, +}; lazy_static! { pub static ref MONITOR: Mutex = { @@ -143,10 +146,12 @@ impl Monitor { #[cfg(test)] mod tests { - use crate::asynchronous::monitor::{ - monitor_notify_by_exec, monitor_notify_by_pid, monitor_subscribe, monitor_unsubscribe, + use crate::{ + asynchronous::monitor::{ + monitor_notify_by_exec, monitor_notify_by_pid, monitor_subscribe, monitor_unsubscribe, + }, + monitor::{ExitEvent, Subject, Topic}, }; - use crate::monitor::{ExitEvent, Subject, Topic}; #[tokio::test] async fn test_monitor() { diff --git a/crates/shim/src/asynchronous/processes.rs b/crates/shim/src/asynchronous/processes.rs index 6469aab..048f78c 100644 --- a/crates/shim/src/asynchronous/processes.rs +++ b/crates/shim/src/asynchronous/processes.rs @@ -14,21 +14,19 @@ limitations under the License. */ -use std::os::unix::io::AsRawFd; -use std::sync::Arc; +use std::{os::unix::io::AsRawFd, sync::Arc}; use async_trait::async_trait; +use containerd_shim_protos::{ + api::{ProcessInfo, StateResponse, Status}, + cgroups::metrics::Metrics, + protobuf::well_known_types::timestamp::Timestamp, +}; use oci_spec::runtime::LinuxResources; use time::OffsetDateTime; use tokio::sync::oneshot::{channel, Receiver, Sender}; -use containerd_shim_protos::api::{ProcessInfo, StateResponse, Status}; -use containerd_shim_protos::cgroups::metrics::Metrics; -use containerd_shim_protos::protobuf::well_known_types::timestamp::Timestamp; - -use crate::io::Stdio; -use crate::util::asyncify; -use crate::{ioctl_set_winsz, Console}; +use crate::{io::Stdio, ioctl_set_winsz, util::asyncify, Console}; #[async_trait] pub trait Process { diff --git a/crates/shim/src/asynchronous/publisher.rs b/crates/shim/src/asynchronous/publisher.rs index b106b8e..1c68f57 100644 --- a/crates/shim/src/asynchronous/publisher.rs +++ b/crates/shim/src/asynchronous/publisher.rs @@ -17,18 +17,19 @@ use std::os::unix::io::RawFd; use async_trait::async_trait; +use containerd_shim_protos::{ + api::Empty, + protobuf::MessageDyn, + shim::events, + shim_async::{Client, Events, EventsClient}, + ttrpc, + ttrpc::{context::Context, r#async::TtrpcContext}, +}; -use containerd_shim_protos::api::Empty; -use containerd_shim_protos::protobuf::MessageDyn; -use containerd_shim_protos::shim::events; -use containerd_shim_protos::shim_async::{Client, Events, EventsClient}; -use containerd_shim_protos::ttrpc; -use containerd_shim_protos::ttrpc::context::Context; -use containerd_shim_protos::ttrpc::r#async::TtrpcContext; - -use crate::error::Result; -use crate::util::asyncify; -use crate::util::{connect, convert_to_any, timestamp}; +use crate::{ + error::Result, + util::{asyncify, connect, convert_to_any, timestamp}, +}; /// Async Remote publisher connects to containerd's TTRPC endpoint to publish events from shim. pub struct RemotePublisher { @@ -97,17 +98,21 @@ impl Events for RemotePublisher { #[cfg(test)] mod tests { - use std::os::unix::io::AsRawFd; - use std::os::unix::net::UnixListener; - use std::sync::Arc; + use std::{ + os::unix::{io::AsRawFd, net::UnixListener}, + sync::Arc, + }; - use tokio::sync::mpsc::{channel, Sender}; - use tokio::sync::Barrier; - - use containerd_shim_protos::api::{Empty, ForwardRequest}; - use containerd_shim_protos::events::task::TaskOOM; - use containerd_shim_protos::shim_async::create_events; - use containerd_shim_protos::ttrpc::asynchronous::Server; + use containerd_shim_protos::{ + api::{Empty, ForwardRequest}, + events::task::TaskOOM, + shim_async::create_events, + ttrpc::asynchronous::Server, + }; + use tokio::sync::{ + mpsc::{channel, Sender}, + Barrier, + }; use super::*; diff --git a/crates/shim/src/asynchronous/task.rs b/crates/shim/src/asynchronous/task.rs index b2685e1..f1910ff 100644 --- a/crates/shim/src/asynchronous/task.rs +++ b/crates/shim/src/asynchronous/task.rs @@ -14,37 +14,38 @@ limitations under the License. */ -use std::collections::HashMap; -use std::sync::Arc; +use std::{collections::HashMap, sync::Arc}; use async_trait::async_trait; +use containerd_shim_protos::{ + api::{ + CloseIORequest, ConnectRequest, ConnectResponse, DeleteResponse, PidsRequest, PidsResponse, + StatsRequest, StatsResponse, UpdateTaskRequest, + }, + events::task::{TaskCreate, TaskDelete, TaskExecAdded, TaskExecStarted, TaskIO, TaskStart}, + protobuf::MessageDyn, + shim_async::Task, + ttrpc, + ttrpc::r#async::TtrpcContext, +}; use log::{debug, info, warn}; use oci_spec::runtime::LinuxResources; -use tokio::sync::mpsc::Sender; -use tokio::sync::{MappedMutexGuard, Mutex, MutexGuard}; +use tokio::sync::{mpsc::Sender, MappedMutexGuard, Mutex, MutexGuard}; -use containerd_shim_protos::api::{ - CloseIORequest, ConnectRequest, ConnectResponse, DeleteResponse, PidsRequest, PidsResponse, - StatsRequest, StatsResponse, UpdateTaskRequest, +use crate::{ + api::{ + CreateTaskRequest, CreateTaskResponse, DeleteRequest, Empty, ExecProcessRequest, + KillRequest, ResizePtyRequest, ShutdownRequest, StartRequest, StartResponse, StateRequest, + StateResponse, Status, WaitRequest, WaitResponse, + }, + asynchronous::{ + container::{Container, ContainerFactory}, + ExitSignal, + }, + event::Event, + util::{convert_to_any, convert_to_timestamp, AsOption}, + TtrpcResult, }; -use containerd_shim_protos::events::task::{ - TaskCreate, TaskDelete, TaskExecAdded, TaskExecStarted, TaskIO, TaskStart, -}; -use containerd_shim_protos::protobuf::MessageDyn; -use containerd_shim_protos::shim_async::Task; -use containerd_shim_protos::ttrpc; -use containerd_shim_protos::ttrpc::r#async::TtrpcContext; - -use crate::api::{ - CreateTaskRequest, CreateTaskResponse, DeleteRequest, Empty, ExecProcessRequest, KillRequest, - ResizePtyRequest, ShutdownRequest, StartRequest, StartResponse, StateRequest, StateResponse, - Status, WaitRequest, WaitResponse, -}; -use crate::asynchronous::container::{Container, ContainerFactory}; -use crate::asynchronous::ExitSignal; -use crate::event::Event; -use crate::util::{convert_to_any, convert_to_timestamp, AsOption}; -use crate::TtrpcResult; type EventSender = Sender<(String, Box)>; diff --git a/crates/shim/src/asynchronous/util.rs b/crates/shim/src/asynchronous/util.rs index c5af169..27874b6 100644 --- a/crates/shim/src/asynchronous/util.rs +++ b/crates/shim/src/asynchronous/util.rs @@ -16,19 +16,20 @@ use std::path::Path; +use containerd_shim_protos::{api::Mount, shim::oci::Options}; use libc::mode_t; use nix::sys::stat::Mode; use oci_spec::runtime::Spec; -use tokio::fs::OpenOptions; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use tokio::task::spawn_blocking; +use tokio::{ + fs::OpenOptions, + io::{AsyncReadExt, AsyncWriteExt}, + task::spawn_blocking, +}; -use containerd_shim_protos::api::Mount; -use containerd_shim_protos::shim::oci::Options; - -use crate::error::Error; -use crate::error::Result; -use crate::util::{AsOption, JsonOptions, CONFIG_FILE_NAME, OPTIONS_FILE_NAME, RUNTIME_FILE_NAME}; +use crate::{ + error::{Error, Result}, + util::{AsOption, JsonOptions, CONFIG_FILE_NAME, OPTIONS_FILE_NAME, RUNTIME_FILE_NAME}, +}; pub async fn asyncify(f: F) -> Result where diff --git a/crates/shim/src/cgroup.rs b/crates/shim/src/cgroup.rs index f7f82af..69bf4ed 100644 --- a/crates/shim/src/cgroup.rs +++ b/crates/shim/src/cgroup.rs @@ -16,20 +16,17 @@ #![cfg(target_os = "linux")] -use std::fs; -use std::io::Read; -use std::path::Path; +use std::{fs, io::Read, path::Path}; -use cgroups_rs::cgroup::get_cgroups_relative_paths_by_pid; -use cgroups_rs::{hierarchies, Cgroup, CgroupPid, MaxValue, Subsystem}; -use oci_spec::runtime::LinuxResources; - -use containerd_shim_protos::cgroups::metrics::{ - CPUStat, CPUUsage, MemoryEntry, MemoryStat, Metrics, +use cgroups_rs::{ + cgroup::get_cgroups_relative_paths_by_pid, hierarchies, Cgroup, CgroupPid, MaxValue, Subsystem, }; -use containerd_shim_protos::protobuf::well_known_types::any::Any; -use containerd_shim_protos::protobuf::Message; -use containerd_shim_protos::shim::oci::Options; +use containerd_shim_protos::{ + cgroups::metrics::{CPUStat, CPUUsage, MemoryEntry, MemoryStat, Metrics}, + protobuf::{well_known_types::any::Any, Message}, + shim::oci::Options, +}; +use oci_spec::runtime::LinuxResources; use crate::error::{Error, Result}; diff --git a/crates/shim/src/error.rs b/crates/shim/src/error.rs index 86fa928..095f610 100644 --- a/crates/shim/src/error.rs +++ b/crates/shim/src/error.rs @@ -16,8 +16,10 @@ use thiserror::Error; -use crate::monitor::ExitEvent; -use crate::protos::{protobuf, ttrpc}; +use crate::{ + monitor::ExitEvent, + protos::{protobuf, ttrpc}, +}; pub type Result = std::result::Result; diff --git a/crates/shim/src/event.rs b/crates/shim/src/event.rs index d0d4875..7181ba0 100644 --- a/crates/shim/src/event.rs +++ b/crates/shim/src/event.rs @@ -1,5 +1,4 @@ -use containerd_shim_protos::events::task::*; -use containerd_shim_protos::protobuf::MessageDyn; +use containerd_shim_protos::{events::task::*, protobuf::MessageDyn}; pub trait Event: MessageDyn { fn topic(&self) -> String; diff --git a/crates/shim/src/lib.rs b/crates/shim/src/lib.rs index a0e9464..60157f0 100644 --- a/crates/shim/src/lib.rs +++ b/crates/shim/src/lib.rs @@ -32,18 +32,20 @@ //! ``` //! -use std::collections::hash_map::DefaultHasher; -use std::fs::File; -use std::hash::Hasher; -use std::os::unix::io::RawFd; -use std::os::unix::net::UnixListener; -use std::path::{Path, PathBuf}; - -use nix::ioctl_write_ptr_bad; +use std::{ + collections::hash_map::DefaultHasher, + fs::File, + hash::Hasher, + os::unix::{io::RawFd, net::UnixListener}, + path::{Path, PathBuf}, +}; pub use containerd_shim_protos as protos; -pub use protos::shim::shim::DeleteResponse; -pub use protos::ttrpc::{context::Context, Result as TtrpcResult}; +use nix::ioctl_write_ptr_bad; +pub use protos::{ + shim::shim::DeleteResponse, + ttrpc::{context::Context, Result as TtrpcResult}, +}; #[cfg(feature = "async")] pub use crate::asynchronous::*; @@ -70,10 +72,11 @@ pub mod util; /// Generated request/response structures. pub mod api { - pub use super::protos::api::Status; - pub use super::protos::shim::oci::Options; - pub use super::protos::shim::shim::*; - pub use super::protos::types::empty::Empty; + pub use super::protos::{ + api::Status, + shim::{oci::Options, shim::*}, + types::empty::Empty, + }; } macro_rules! cfg_not_async { diff --git a/crates/shim/src/logger.rs b/crates/shim/src/logger.rs index 0f12e51..054d172 100644 --- a/crates/shim/src/logger.rs +++ b/crates/shim/src/logger.rs @@ -14,12 +14,14 @@ limitations under the License. */ -use std::borrow::BorrowMut; -use std::fs::{File, OpenOptions}; -use std::io; -use std::io::Write; -use std::path::Path; -use std::sync::Mutex; +use std::{ + borrow::BorrowMut, + fs::{File, OpenOptions}, + io, + io::Write, + path::Path, + sync::Mutex, +}; use log::{Metadata, Record}; diff --git a/crates/shim/src/mount.rs b/crates/shim/src/mount.rs index b6227a3..4e66a59 100644 --- a/crates/shim/src/mount.rs +++ b/crates/shim/src/mount.rs @@ -15,10 +15,12 @@ */ #![allow(unused)] -use std::collections::HashMap; -use std::env; -use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not}; -use std::path::Path; +use std::{ + collections::HashMap, + env, + ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not}, + path::Path, +}; use lazy_static::lazy_static; use log::error; diff --git a/crates/shim/src/synchronous/console.rs b/crates/shim/src/synchronous/console.rs index dde9228..fe1e318 100644 --- a/crates/shim/src/synchronous/console.rs +++ b/crates/shim/src/synchronous/console.rs @@ -14,15 +14,18 @@ limitations under the License. */ -use std::os::unix::net::{UnixListener, UnixStream}; -use std::path::{Path, PathBuf}; +use std::{ + os::unix::net::{UnixListener, UnixStream}, + path::{Path, PathBuf}, +}; use log::warn; use uuid::Uuid; -use crate::util::{mkdir, xdg_runtime_dir}; -use crate::Error; -use crate::Result; +use crate::{ + util::{mkdir, xdg_runtime_dir}, + Error, Result, +}; pub struct ConsoleSocket { pub listener: UnixListener, diff --git a/crates/shim/src/synchronous/mod.rs b/crates/shim/src/synchronous/mod.rs index f56b644..62a863b 100644 --- a/crates/shim/src/synchronous/mod.rs +++ b/crates/shim/src/synchronous/mod.rs @@ -32,35 +32,42 @@ //! ``` //! -use std::convert::TryFrom; -use std::env; -use std::fs; -use std::io::Write; -use std::os::unix::fs::FileTypeExt; -use std::os::unix::io::AsRawFd; -use std::path::Path; -use std::process::{self, Command, Stdio}; -use std::sync::{Arc, Condvar, Mutex}; +use std::{ + convert::TryFrom, + env, fs, + io::Write, + os::unix::{fs::FileTypeExt, io::AsRawFd}, + path::Path, + process::{self, Command, Stdio}, + sync::{Arc, Condvar, Mutex}, +}; use command_fds::{CommandFdExt, FdMapping}; use libc::{SIGCHLD, SIGINT, SIGPIPE, SIGTERM}; pub use log::{debug, error, info, warn}; -use nix::errno::Errno; -use nix::sys::signal::Signal; -use nix::sys::wait::{self, WaitPidFlag, WaitStatus}; -use nix::unistd::Pid; +use nix::{ + errno::Errno, + sys::{ + signal::Signal, + wait::{self, WaitPidFlag, WaitStatus}, + }, + unistd::Pid, +}; use signal_hook::iterator::Signals; - -use crate::protos::protobuf::Message; -use crate::protos::shim::shim_ttrpc::{create_task, Task}; -use crate::protos::ttrpc::{Client, Server}; use util::{read_address, write_address}; -use crate::api::DeleteResponse; -use crate::synchronous::publisher::RemotePublisher; -use crate::Error; -use crate::{args, logger, reap, Result, TTRPC_ADDRESS}; -use crate::{parse_sockaddr, socket_address, start_listener, Config, StartOpts, SOCKET_FD}; +use crate::{ + api::DeleteResponse, + args, logger, parse_sockaddr, + protos::{ + protobuf::Message, + shim::shim_ttrpc::{create_task, Task}, + ttrpc::{Client, Server}, + }, + reap, socket_address, start_listener, + synchronous::publisher::RemotePublisher, + Config, Error, Result, StartOpts, SOCKET_FD, TTRPC_ADDRESS, +}; pub mod monitor; pub mod publisher; diff --git a/crates/shim/src/synchronous/monitor.rs b/crates/shim/src/synchronous/monitor.rs index 5f2f49d..9a8c6fd 100644 --- a/crates/shim/src/synchronous/monitor.rs +++ b/crates/shim/src/synchronous/monitor.rs @@ -14,15 +14,21 @@ limitations under the License. */ -use std::collections::HashMap; -use std::sync::mpsc::{channel, Receiver, Sender}; -use std::sync::Mutex; +use std::{ + collections::HashMap, + sync::{ + mpsc::{channel, Receiver, Sender}, + Mutex, + }, +}; use lazy_static::lazy_static; use log::{error, warn}; -use crate::monitor::{ExitEvent, Subject, Topic}; -use crate::Result; +use crate::{ + monitor::{ExitEvent, Subject, Topic}, + Result, +}; lazy_static! { pub static ref MONITOR: Mutex = { diff --git a/crates/shim/src/synchronous/publisher.rs b/crates/shim/src/synchronous/publisher.rs index 5b3f790..459901c 100644 --- a/crates/shim/src/synchronous/publisher.rs +++ b/crates/shim/src/synchronous/publisher.rs @@ -16,16 +16,19 @@ //! Implements a client to publish events from the shim back to containerd. +use client::{ + protobuf::MessageDyn, + shim::events, + ttrpc::{self, context::Context}, + types::empty, + Client, Events, EventsClient, +}; use containerd_shim_protos as client; -use client::protobuf::MessageDyn; -use client::shim::events; -use client::ttrpc::{self, context::Context}; -use client::types::empty; -use client::{Client, Events, EventsClient}; - -use crate::error::Result; -use crate::util::{connect, convert_to_any, timestamp}; +use crate::{ + error::Result, + util::{connect, convert_to_any, timestamp}, +}; /// Remote publisher connects to containerd's TTRPC endpoint to publish events from shim. pub struct RemotePublisher { @@ -87,15 +90,17 @@ impl Events for RemotePublisher { #[cfg(test)] mod tests { - use std::os::unix::io::AsRawFd; - use std::os::unix::net::UnixListener; - use std::sync::{Arc, Barrier}; + use std::{ + os::unix::{io::AsRawFd, net::UnixListener}, + sync::{Arc, Barrier}, + }; + use client::{ + api::{Empty, ForwardRequest}, + events::task::TaskOOM, + }; use ttrpc::Server; - use client::api::{Empty, ForwardRequest}; - use client::events::task::TaskOOM; - use super::*; struct FakeServer {} diff --git a/crates/shim/src/synchronous/util.rs b/crates/shim/src/synchronous/util.rs index ec8a6f9..8b103c3 100644 --- a/crates/shim/src/synchronous/util.rs +++ b/crates/shim/src/synchronous/util.rs @@ -14,19 +14,22 @@ limitations under the License. */ -use std::fs::{rename, File, OpenOptions}; -use std::io::{Read, Write}; -use std::path::Path; +use std::{ + fs::{rename, File, OpenOptions}, + io::{Read, Write}, + path::Path, +}; +use containerd_shim_protos::shim::oci::Options; use libc::mode_t; use log::warn; use nix::sys::stat::Mode; use oci_spec::runtime::Spec; -use containerd_shim_protos::shim::oci::Options; - -use crate::util::{JsonOptions, OPTIONS_FILE_NAME, RUNTIME_FILE_NAME}; -use crate::Error; +use crate::{ + util::{JsonOptions, OPTIONS_FILE_NAME, RUNTIME_FILE_NAME}, + Error, +}; pub fn read_file_to_str>(filename: P) -> crate::Result { let mut file = File::open(&filename).map_err(io_error!( diff --git a/crates/shim/src/util.rs b/crates/shim/src/util.rs index ac086c7..6583524 100644 --- a/crates/shim/src/util.rs +++ b/crates/shim/src/util.rs @@ -14,21 +14,27 @@ limitations under the License. */ -use std::env; -use std::os::unix::io::RawFd; -use std::time::{SystemTime, UNIX_EPOCH}; +use std::{ + env, + os::unix::io::RawFd, + time::{SystemTime, UNIX_EPOCH}, +}; use serde::{Deserialize, Serialize}; use time::OffsetDateTime; -use crate::api::Options; #[cfg(feature = "async")] pub use crate::asynchronous::util::*; -use crate::error::Result; -use crate::protos::protobuf::well_known_types::{any::Any, timestamp::Timestamp}; -use crate::protos::protobuf::MessageDyn; #[cfg(not(feature = "async"))] pub use crate::synchronous::util::*; +use crate::{ + api::Options, + error::Result, + protos::protobuf::{ + well_known_types::{any::Any, timestamp::Timestamp}, + MessageDyn, + }, +}; pub const CONFIG_FILE_NAME: &str = "config.json"; pub const OPTIONS_FILE_NAME: &str = "options.json"; @@ -96,8 +102,7 @@ impl From for Options { } pub fn connect(address: impl AsRef) -> Result { - use nix::sys::socket::*; - use nix::unistd::close; + use nix::{sys::socket::*, unistd::close}; let unix_addr = UnixAddr::new(address.as_ref())?; diff --git a/crates/snapshots/build.rs b/crates/snapshots/build.rs index 00eee9e..af312d5 100644 --- a/crates/snapshots/build.rs +++ b/crates/snapshots/build.rs @@ -14,9 +14,7 @@ limitations under the License. */ -use std::env; -use std::fs; -use std::io; +use std::{env, fs, io}; const PROTO_FILES: &[&str] = &[ "vendor/github.com/containerd/containerd/api/types/mount.proto", diff --git a/crates/snapshots/examples/snapshotter.rs b/crates/snapshots/examples/snapshotter.rs index f709c73..7fa768d 100644 --- a/crates/snapshots/examples/snapshotter.rs +++ b/crates/snapshots/examples/snapshotter.rs @@ -14,17 +14,14 @@ limitations under the License. */ -use std::collections::HashMap; -use std::env; - -use futures::TryFutureExt; -use log::info; -use tokio::net::UnixListener; +use std::{collections::HashMap, env}; use containerd_snapshots as snapshots; use containerd_snapshots::{api, Info, Usage}; - +use futures::TryFutureExt; +use log::info; use snapshots::tonic::transport::Server; +use tokio::net::UnixListener; #[derive(Default)] struct Example; diff --git a/crates/snapshots/src/convert.rs b/crates/snapshots/src/convert.rs index 025e008..744097a 100644 --- a/crates/snapshots/src/convert.rs +++ b/crates/snapshots/src/convert.rs @@ -21,8 +21,7 @@ use std::convert::{TryFrom, TryInto}; use thiserror::Error; use tonic::Status; -use crate::api::snapshots::v1 as grpc; -use crate::{Info, Kind}; +use crate::{api::snapshots::v1 as grpc, Info, Kind}; impl From for i32 { fn from(kind: Kind) -> i32 { diff --git a/crates/snapshots/src/lib.rs b/crates/snapshots/src/lib.rs index 4b8d39c..4caea92 100644 --- a/crates/snapshots/src/lib.rs +++ b/crates/snapshots/src/lib.rs @@ -49,10 +49,7 @@ //! ``` //! -use std::collections::HashMap; -use std::fmt::Debug; -use std::ops::AddAssign; -use std::time::SystemTime; +use std::{collections::HashMap, fmt::Debug, ops::AddAssign, time::SystemTime}; pub use tonic; diff --git a/crates/snapshots/src/wrap.rs b/crates/snapshots/src/wrap.rs index f206ab5..240bd6c 100644 --- a/crates/snapshots/src/wrap.rs +++ b/crates/snapshots/src/wrap.rs @@ -16,16 +16,17 @@ //! Trait wrapper to server GRPC requests. -use std::convert::TryInto; -use std::fmt::Debug; +use std::{convert::TryInto, fmt::Debug}; use tokio_stream::wrappers::ReceiverStream; -use crate::api::snapshots::v1::{ - snapshots_server::{Snapshots, SnapshotsServer}, - *, +use crate::{ + api::snapshots::v1::{ + snapshots_server::{Snapshots, SnapshotsServer}, + *, + }, + Snapshotter, }; -use crate::Snapshotter; pub struct Wrapper { snapshotter: S,