Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: group imports #251

Merged
merged 3 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
- name: Setup build env
run: ./scripts/setup.sh
shell: bash
- run:
# needed to run rustfmt in nightly toolchain
rustup toolchain install nightly --component rustfmt
- name: Run checks
run: make check
build:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ build:

.PHONY: check
check:
cargo fmt --all -- --check
cargo +nightly fmt --all -- --check
cargo clippy --all --all-targets -- -D warnings

.PHONY: fix
fix:
cargo fmt --all
cargo +nightly fmt --all
cargo clippy --fix --all --all-targets -- -D warnings

.PHONY: test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::sandbox::oci;
use std::fs::OpenOptions;
use std::io::Read;
use std::os::fd::RawFd;
use std::path::PathBuf;

use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use libcontainer::workload::default::DefaultExecutor;
use libcontainer::workload::{Executor, ExecutorError};
use nix::unistd::{dup, dup2};

use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use oci_spec::runtime::Spec;
use std::io::Read;
use std::{fs::OpenOptions, os::fd::RawFd, path::PathBuf};

use crate::sandbox::oci;

#[derive(Default)]
pub struct LinuxContainerExecutor {
Expand Down
28 changes: 11 additions & 17 deletions crates/containerd-shim-wasm/src/libcontainer_instance/instance.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
//! Abstractions for running/managing a wasm/wasi instance that uses youki's libcontainer library.

use std::path::PathBuf;
use std::thread;

use anyhow::Context;
use chrono::Utc;
use libc::{SIGINT, SIGKILL};
use libcontainer::{
container::{Container, ContainerStatus},
signal::Signal,
};
use libcontainer::container::{Container, ContainerStatus};
use libcontainer::signal::Signal;
use log::error;
use nix::{
errno::Errno,
sys::wait::{waitid, Id as WaitID, WaitPidFlag, WaitStatus},
};

use crate::sandbox::{
instance::ExitCode,
instance_utils::{get_instance_root, instance_exists},
};

use crate::sandbox::InstanceConfig;
use std::{path::PathBuf, thread};
use nix::errno::Errno;
use nix::sys::wait::{waitid, Id as WaitID, WaitPidFlag, WaitStatus};

use crate::sandbox::{error::Error, instance::Wait, Instance};
use crate::sandbox::error::Error;
use crate::sandbox::instance::{ExitCode, Wait};
use crate::sandbox::instance_utils::{get_instance_root, instance_exists};
use crate::sandbox::{Instance, InstanceConfig};

/// LibcontainerInstance is a trait that gets implemented by a WASI runtime that
/// uses youki's libcontainer library as the container runtime.
Expand Down
3 changes: 1 addition & 2 deletions crates/containerd-shim-wasm/src/sandbox/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use std::sync::mpsc::Sender;
use std::sync::{Arc, Condvar, Mutex};
use std::thread;

use libc::{SIGINT, SIGKILL, SIGTERM};

use chrono::{DateTime, Utc};
use libc::{SIGINT, SIGKILL, SIGTERM};

use super::error::Error;

Expand Down
12 changes: 6 additions & 6 deletions crates/containerd-shim-wasm/src/sandbox/instance_utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Common utilities for the containerd shims.
use crate::sandbox::error::Error;
use std::fs::{self, File, OpenOptions};
use std::io::ErrorKind;
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};
use std::{
fs::{self, File, OpenOptions},
io::ErrorKind,
path::{Path, PathBuf},
};

use crate::sandbox::error::Error;

/// Return the root path for the instance.
///
Expand Down
21 changes: 8 additions & 13 deletions crates/containerd-shim-wasm/src/sandbox/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@ use std::env::current_dir;
use std::fs::File;
use std::os::unix::io::AsRawFd;
use std::path::Path;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::{Arc, RwLock};
use std::thread;

use anyhow::Context;
use containerd_shim::{
self as shim, api,
error::Error as ShimError,
protos::shim::shim_ttrpc::{create_task, Task},
protos::ttrpc::{Client, Server},
protos::TaskClient,
publisher::RemotePublisher,
TtrpcContext, TtrpcResult,
};
use containerd_shim::error::Error as ShimError;
use containerd_shim::protos::shim::shim_ttrpc::{create_task, Task};
use containerd_shim::protos::ttrpc::{Client, Server};
use containerd_shim::protos::TaskClient;
use containerd_shim::publisher::RemotePublisher;
use containerd_shim::{self as shim, api, TtrpcContext, TtrpcResult};
use nix::sched::{setns, unshare, CloneFlags};
use oci_spec::runtime;
use shim::Flags;
use ttrpc::context;

use super::error::Error;
use super::instance::Instance;
use super::oci;
use super::sandbox;
use super::{oci, sandbox};
use crate::services::sandbox_ttrpc::{Manager, ManagerClient};

/// Sandbox wraps an Instance and is used with the `Service` to manage multiple instances.
Expand Down
17 changes: 10 additions & 7 deletions crates/containerd-shim-wasm/src/sandbox/oci.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
//! Generic helpers for working with OCI specs that can be consumed by any runtime.

use std::collections::HashMap;
use std::fs::File;
use std::io::{ErrorKind, Write};
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process;

use super::error::Result;
use anyhow::Context;
use nix::{sys::signal, unistd::Pid};
use nix::sys::signal;
use nix::unistd::Pid;
pub use oci_spec::runtime::Spec;
use serde_json as json;
use std::collections::HashMap;
use std::io::{ErrorKind, Write};
use std::os::unix::process::CommandExt;
use std::process;

use super::error::Result;

pub fn load(path: &str) -> Result<Spec> {
let spec = Spec::load(path)?;
Expand Down Expand Up @@ -145,9 +147,10 @@ pub fn setup_prestart_hooks(hooks: &Option<oci_spec::runtime::Hooks>) -> Result<

#[cfg(test)]
mod oci_tests {
use super::*;
use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder};

use super::*;

#[test]
fn test_get_args() -> Result<()> {
let spec = SpecBuilder::default()
Expand Down
35 changes: 15 additions & 20 deletions crates/containerd-shim-wasm/src/sandbox/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,46 @@

use std::collections::HashMap;
use std::env::current_dir;
use std::fs::{self, File};
use std::fs::{canonicalize, create_dir_all, OpenOptions};
use std::fs::{self, canonicalize, create_dir_all, File, OpenOptions};
use std::ops::Not;
use std::os::unix::io::AsRawFd;
use std::path::Path;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Arc, Condvar, Mutex, RwLock};
use std::thread;

use super::instance::{Instance, InstanceConfig, Nop, Wait};
use super::{oci, Error, SandboxService};
use cgroups_rs::cgroup::get_cgroups_relative_paths_by_pid;
use cgroups_rs::hierarchies::{self};
use cgroups_rs::{Cgroup, Subsystem};
use chrono::{DateTime, Utc};
use containerd_shim::{
self as shim, api,
error::Error as ShimError,
event::Event,
mount::mount_rootfs,
protos::events::task::{TaskCreate, TaskDelete, TaskExit, TaskIO, TaskStart},
protos::protobuf::well_known_types::timestamp::Timestamp,
protos::protobuf::{MessageDyn, MessageField},
protos::shim::shim_ttrpc::Task,
protos::types::task::Status,
publisher::RemotePublisher,
util::IntoOption,
util::{timestamp as new_timestamp, write_address},
warn, ExitSignal, TtrpcContext, TtrpcResult,
};
use containerd_shim::error::Error as ShimError;
use containerd_shim::event::Event;
use containerd_shim::mount::mount_rootfs;
use containerd_shim::protos::events::task::{TaskCreate, TaskDelete, TaskExit, TaskIO, TaskStart};
use containerd_shim::protos::protobuf::well_known_types::timestamp::Timestamp;
use containerd_shim::protos::protobuf::{MessageDyn, MessageField};
use containerd_shim::protos::shim::shim_ttrpc::Task;
use containerd_shim::protos::types::task::Status;
use containerd_shim::publisher::RemotePublisher;
use containerd_shim::util::{timestamp as new_timestamp, write_address, IntoOption};
use containerd_shim::{self as shim, api, warn, ExitSignal, TtrpcContext, TtrpcResult};
use log::{debug, error};
use nix::mount::{mount, MsFlags};
use nix::sched::{setns, unshare, CloneFlags};
use nix::sys::stat::Mode;
use nix::unistd::mkdir;
use oci_spec::runtime;
use shim::api::{StatsRequest, StatsResponse};

use shim::protos::cgroups::metrics::{
CPUStat, CPUUsage, MemoryEntry, MemoryStat, Metrics, PidsStat, Throttle,
};
use shim::util::convert_to_any;
use shim::Flags;
use ttrpc::context::Context;

use super::instance::{Instance, InstanceConfig, Nop, Wait};
use super::{oci, Error, SandboxService};

type InstanceDataStatus = (Mutex<Option<(u32, DateTime<Utc>)>>, Condvar);

struct InstanceData<T: Instance> {
Expand Down
3 changes: 2 additions & 1 deletion crates/containerd-shim-wasm/src/sandbox/testutil.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Testing utilities used across different modules

use super::{Error, Result};
use std::process::{Command, Stdio};

use super::{Error, Result};

fn normalize_test_name(test: &str) -> Result<&str> {
let closure_removed = test.trim_end_matches("::{{closure}}");

Expand Down
16 changes: 7 additions & 9 deletions crates/containerd-shim-wasmedge/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::os::unix::io::RawFd;
use std::path::PathBuf;

use anyhow::Result;
use containerd_shim_wasm::sandbox::oci;
use nix::unistd::{dup, dup2};
use oci_spec::runtime::Spec;

use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use libcontainer::workload::{Executor, ExecutorError};
use log::debug;
use std::{os::unix::io::RawFd, path::PathBuf};

use wasmedge_sdk::{
config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions},
params, VmBuilder,
};
use nix::unistd::{dup, dup2};
use oci_spec::runtime::Spec;
use wasmedge_sdk::config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions};
use wasmedge_sdk::{params, VmBuilder};

const EXECUTOR_NAME: &str = "wasmedge";

Expand Down
34 changes: 14 additions & 20 deletions crates/containerd-shim-wasmedge/src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
use anyhow::Context;
use anyhow::Result;
use containerd_shim_wasm::libcontainer_instance::LibcontainerInstance;
use containerd_shim_wasm::libcontainer_instance::LinuxContainerExecutor;
use containerd_shim_wasm::sandbox::error::Error;
use containerd_shim_wasm::sandbox::instance::ExitCode;
use containerd_shim_wasm::sandbox::instance_utils::maybe_open_stdio;
use containerd_shim_wasm::sandbox::InstanceConfig;
use nix::unistd::close;
use serde::{Deserialize, Serialize};
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::io::ErrorKind;
use std::os::fd::IntoRawFd;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Condvar, Mutex};

use std::{
fs,
path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use containerd_shim_wasm::libcontainer_instance::{LibcontainerInstance, LinuxContainerExecutor};
use containerd_shim_wasm::sandbox::error::Error;
use containerd_shim_wasm::sandbox::instance::ExitCode;
use containerd_shim_wasm::sandbox::instance_utils::maybe_open_stdio;
use containerd_shim_wasm::sandbox::InstanceConfig;
use libcontainer::container::builder::ContainerBuilder;
use libcontainer::container::Container;
use libcontainer::syscall::syscall::create_syscall;
use nix::unistd::close;
use serde::{Deserialize, Serialize};

use crate::executor::WasmEdgeExecutor;

Expand Down Expand Up @@ -147,14 +142,12 @@ mod wasitest {
use containerd_shim_wasm::sandbox::Instance;
use libc::{dup2, SIGKILL, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder};
use tempfile::{tempdir, TempDir};

use serial_test::serial;
use tempfile::{tempdir, TempDir};
use wasmedge_sdk::wat2wasm;

use super::*;

use wasmedge_sdk::wat2wasm;

static mut STDIN_FD: Option<RawFd> = None;
static mut STDOUT_FD: Option<RawFd> = None;
static mut STDERR_FD: Option<RawFd> = None;
Expand Down Expand Up @@ -340,9 +333,10 @@ mod wasitest {
mod rootdirtest {
use std::fs::OpenOptions;

use super::*;
use tempfile::tempdir;

use super::*;

#[test]
fn test_determine_rootdir_with_options_file() -> Result<(), Error> {
let namespace = "test_namespace";
Expand Down
7 changes: 4 additions & 3 deletions crates/containerd-shim-wasmtime/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use nix::unistd::{dup, dup2};
use std::{fs::OpenOptions, os::fd::RawFd, path::PathBuf};
use std::fs::OpenOptions;
use std::os::fd::RawFd;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use containerd_shim_wasm::sandbox::oci;
use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use libcontainer::workload::{Executor, ExecutorError};
use nix::unistd::{dup, dup2};
use oci_spec::runtime::Spec;

use wasmtime::{Engine, Linker, Module, Store};
use wasmtime_wasi::WasiCtxBuilder;

Expand Down
Loading