Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
407b883
Update youki to the latest main branch and oci-spec to 0.6.1.
Mossaka Jun 12, 2023
8ca83f2
Merge remote-tracking branch 'upstream/main' into oci-0.6.1
Mossaka Jun 13, 2023
c555e3f
Removed some uses of unsafe in wasmedge shim
Mossaka Jun 13, 2023
fc1bb2e
Use youki's libcontainer APIs to implement wasmtime shim.
Mossaka Jun 13, 2023
83ed1a2
Fix some clippy warnings
Mossaka Jun 14, 2023
d8e4c1a
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka Jun 14, 2023
d3b7f69
Passed the test_delete_after_create test
Mossaka Jun 21, 2023
d998250
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka Jun 21, 2023
feb93fc
This commit adds a new crate called "containerd-shim-common".
Mossaka Jun 21, 2023
47ce17d
Apply suggestions from code review
Mossaka Jun 23, 2023
363f1cd
Resolved some review comments
Mossaka Jun 23, 2023
6882dd5
Removed common crate and moved the library to containerd-shim-wasm and
Mossaka Jun 23, 2023
fc33358
Rollback accidentally deleted deps
Mossaka Jun 23, 2023
b606b25
Removed the libcontainer feature
Mossaka Jun 23, 2023
8c9e43d
Fixed the test_wasi test
Mossaka Jun 28, 2023
11c2e20
Add trace to "cargo test" in CI
Mossaka Jun 28, 2023
839442a
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka Jun 28, 2023
80cd742
Temp commit to run tests on ubuntu 22.04 only
Mossaka Jun 28, 2023
ebad282
Rollback ubuntu 20.02 test in CI
Mossaka Jun 29, 2023
560356f
Use the same determine root logic from wasmedge shim
Mossaka Jun 29, 2023
4b106b2
Fixed clippy issues
Mossaka Jun 29, 2023
51182c6
Reset stdio at the end of the tests
Mossaka Jun 29, 2023
7ca42a1
Need rollback: commenting out the stdio thing
Mossaka Jun 29, 2023
8d5c858
Use inherit_stdio()
Mossaka Jun 29, 2023
65177c3
Setup CI build env for wasmtimie e2e
Mossaka Jun 30, 2023
bb1e940
Add inpsection step to e2e-wasmtime
Mossaka Jun 30, 2023
e120093
Add deps to kind nodes that run k8s tests
Mossaka Jul 5, 2023
76fa9cb
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka Jul 5, 2023
f7472fb
Handle Result types from dup and dup2 in both wasmtime and wasmedge s…
Mossaka Jul 6, 2023
d807601
Small refactoring
Mossaka Jul 6, 2023
3f31397
Update crates/containerd-shim-wasmtime/src/executor.rs
Mossaka Jul 6, 2023
2c64834
Merge branch 'wasmtime-youki' of https://github.com/Mossaka/runwasi i…
Mossaka Jul 6, 2023
729c3b4
Add a log to when Errno::ECHILD was returned
Mossaka Jul 6, 2023
a28fad9
Merge remote-tracking branch 'upstream/main' into wasmtime-youki
Mossaka Jul 7, 2023
aaaea73
Resolve comments
Mossaka Jul 9, 2023
dfc680d
Rustfmt
Mossaka Jul 9, 2023
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 2 additions & 13 deletions crates/containerd-shim-wasmedge/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use containerd_shim_wasm::sandbox::oci;
use nix::unistd::{dup, dup2};
use oci_spec::runtime::Spec;

Expand All @@ -22,7 +23,7 @@ pub struct WasmEdgeExecutor {
impl Executor for WasmEdgeExecutor {
fn exec(&self, spec: &Spec) -> Result<(), ExecutorError> {
// parse wasi parameters
let args = get_args(spec);
let args = oci::get_args(spec);
if args.is_empty() {
return Err(ExecutorError::InvalidArg);
}
Expand Down Expand Up @@ -91,18 +92,6 @@ impl Executor for WasmEdgeExecutor {
}
}

fn get_args(spec: &Spec) -> &[String] {
let p = match spec.process() {
None => return &[],
Some(p) => p,
};

match p.args() {
None => &[],
Some(args) => args.as_slice(),
}
}

fn env_to_wasi(spec: &Spec) -> Vec<String> {
let default = vec![];
let env = spec
Expand Down
3 changes: 3 additions & 0 deletions crates/containerd-shim-wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ oci-spec = { workspace = true, features = ["runtime"] }
thiserror = { workspace = true }
serde_json = { workspace = true }
nix = { workspace = true }
libcontainer = { workspace = true }
serde = { workspace = true }
libc = { workspace = true }

[dev-dependencies]
tempfile = "3.0"
Expand Down
133 changes: 133 additions & 0 deletions crates/containerd-shim-wasmtime/src/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use std::{fs::OpenOptions, os::fd::RawFd, path::PathBuf};

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

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

use crate::oci_wasmtime::{self, wasi_dir, wasi_file};

const EXECUTOR_NAME: &str = "wasmtime";

static mut STDIN_FD: Option<RawFd> = None;
static mut STDOUT_FD: Option<RawFd> = None;
static mut STDERR_FD: Option<RawFd> = None;

pub struct WasmtimeExecutor {
pub stdin: Option<RawFd>,
pub stdout: Option<RawFd>,
pub stderr: Option<RawFd>,
pub engine: Engine,
Comment thread
Mossaka marked this conversation as resolved.
}

impl Executor for WasmtimeExecutor {
fn exec(&self, spec: &Spec) -> Result<(), ExecutorError> {
let args = oci::get_args(spec);
if args.is_empty() {
return Err(ExecutorError::InvalidArg);
}
Comment thread
Mossaka marked this conversation as resolved.

let (mut store, f) = self
.prepare_function(spec, args)
.map_err(|err| ExecutorError::Other(format!("failed to prepare function: {}", err)))?;

log::info!("calling start function");
match f.call(&mut store, &[], &mut []) {
Ok(_) => std::process::exit(0),
Err(_) => std::process::exit(137),
};
}

fn can_handle(&self, _spec: &Spec) -> bool {
true
}

fn name(&self) -> &'static str {
EXECUTOR_NAME
}
}

impl WasmtimeExecutor {
fn prepare_function(
Comment thread
Mossaka marked this conversation as resolved.
Outdated
&self,
spec: &Spec,
args: &[String],
) -> anyhow::Result<(Store<wasi_common::WasiCtx>, wasmtime::Func)> {
// already in the cgroup
let env = oci_wasmtime::env_to_wasi(spec);
log::info!("setting up wasi");

let path = wasi_dir(".", OpenOptions::new().read(true))?;
let mut wasi_builder = WasiCtxBuilder::new()
.args(args)?
Comment thread
Mossaka marked this conversation as resolved.
.envs(env.as_slice())?
.preopened_dir(path, "/")?;
Comment thread
Mossaka marked this conversation as resolved.

if let Some(stdin) = self.stdin {
unsafe {
STDIN_FD = Some(dup(STDIN_FILENO));
dup2(stdin, STDIN_FILENO);
}
}
Comment thread
Mossaka marked this conversation as resolved.
if let Some(stdout) = self.stdout {
unsafe {
STDOUT_FD = Some(dup(STDOUT_FILENO));
dup2(stdout, STDOUT_FILENO);
}
}
if let Some(stderr) = self.stderr {
unsafe {
STDERR_FD = Some(dup(STDERR_FILENO));
dup2(stderr, STDERR_FILENO);
}
}
log::info!("opening stdin");
let stdin_path = PathBuf::from("/dev/stdin");
let stdin_wasi_file = wasi_file(stdin_path, OpenOptions::new().read(true))?;
wasi_builder = wasi_builder.stdin(Box::new(stdin_wasi_file));

log::info!("opening stdout");
let stdout_path = PathBuf::from("/dev/stdout");
let stdout_wasi_file = wasi_file(stdout_path, OpenOptions::new().write(true))?;
wasi_builder = wasi_builder.stdout(Box::new(stdout_wasi_file));

log::info!("opening stderr");
let stderr_path = PathBuf::from("/dev/stderr");
let stderr_wasi_file = wasi_file(stderr_path, OpenOptions::new().write(true))?;
wasi_builder = wasi_builder.stderr(Box::new(stderr_wasi_file));

log::info!("building wasi context");
let wctx = wasi_builder.build();

log::info!("wasi context ready");
let start = args[0].clone();
let mut iterator = start.split('#');
Comment thread
Mossaka marked this conversation as resolved.
Outdated
let mut cmd = iterator.next().unwrap().to_string();
let stripped = cmd.strip_prefix(std::path::MAIN_SEPARATOR);
if let Some(strpd) = stripped {
cmd = strpd.to_string();
}
let method = iterator.next().unwrap_or("_start");
let mod_path = cmd;

log::info!("loading module from file");
let module = Module::from_file(&self.engine, mod_path)?;
let mut linker = Linker::new(&self.engine);

wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
let mut store = Store::new(&self.engine, wctx);

log::info!("instantiating instance");
let i = linker.instantiate(&mut store, &module)?;

log::info!("getting start function");
let f = i
.get_func(&mut store, method)
.ok_or_else(|| anyhow!("module does not have a wasi start function".to_string()))?;
Ok((store, f))
Comment thread
Mossaka marked this conversation as resolved.
Outdated
}
}
Loading