diff --git a/benches/containerd-shim-benchmarks/benches/webassembly-benchmarks.rs b/benches/containerd-shim-benchmarks/benches/webassembly-benchmarks.rs index 80f71613c..7079b442e 100644 --- a/benches/containerd-shim-benchmarks/benches/webassembly-benchmarks.rs +++ b/benches/containerd-shim-benchmarks/benches/webassembly-benchmarks.rs @@ -108,7 +108,7 @@ fn run_wasmtime_test_with_spec( spec.save(dir.path().join("config.json"))?; - let mut cfg = InstanceConfig::new(WasmtimeWasi::new_engine()?, "test_namespace".into(), None); + let mut cfg = InstanceConfig::new(WasmtimeWasi::new_engine()?, "test_namespace".into()); let cfg = cfg .set_bundle(dir.path().to_str().unwrap().to_string()) .set_stdout(dir.path().join("stdout").to_str().unwrap().to_string()); @@ -169,7 +169,7 @@ fn run_wasmedge_test_with_spec( spec.save(dir.path().join("config.json"))?; - let mut cfg = InstanceConfig::new(WasmEdgeWasi::new_engine()?, "test_namespace".into(), None); + let mut cfg = InstanceConfig::new(WasmEdgeWasi::new_engine()?, "test_namespace".into()); let cfg = cfg .set_bundle(dir.path().to_str().unwrap().to_string()) .set_stdout(dir.path().join("stdout").to_str().unwrap().to_string()); diff --git a/crates/containerd-shim-wasm/src/sandbox/instance.rs b/crates/containerd-shim-wasm/src/sandbox/instance.rs index 3e76b77aa..cd0a7a0ff 100644 --- a/crates/containerd-shim-wasm/src/sandbox/instance.rs +++ b/crates/containerd-shim-wasm/src/sandbox/instance.rs @@ -2,12 +2,13 @@ use std::sync::mpsc::Sender; use std::sync::{Arc, Condvar, Mutex}; -use std::thread; +use std::{thread, env}; use libc::{SIGINT, SIGKILL, SIGTERM}; use chrono::{DateTime, Utc}; +use super::containerd; use super::error::Error; type ExitCode = (Mutex)>>, Condvar); @@ -40,8 +41,12 @@ impl InstanceConfig where E: Send + Sync + Clone, { - pub fn new(engine: E, namespace: String, containerd_address: Option) -> Self { - // todo read containerd address + pub fn new(engine: E, namespace: String) -> Self { + let os_args: Vec<_> = env::args_os().collect(); + let containerd_address = match containerd::parse(&os_args[1..]) { + Ok(flags) => Some(flags.address), + _ => None + }; Self { engine, namespace, diff --git a/crates/containerd-shim-wasm/src/sandbox/shim.rs b/crates/containerd-shim-wasm/src/sandbox/shim.rs index c962c4e4f..00dee5805 100644 --- a/crates/containerd-shim-wasm/src/sandbox/shim.rs +++ b/crates/containerd-shim-wasm/src/sandbox/shim.rs @@ -3,7 +3,7 @@ //! the container/sandbox. use std::collections::HashMap; -use std::env::{current_dir, self}; +use std::env::{current_dir}; use std::fs::{self, File}; use std::fs::{canonicalize, create_dir_all, OpenOptions}; use std::ops::Not; @@ -14,7 +14,7 @@ use std::sync::{Arc, Condvar, Mutex, RwLock}; use std::thread; use super::instance::{EngineGetter, Instance, InstanceConfig, Nop, Wait}; -use super::{oci, Error, SandboxService, containerd}; +use super::{oci, Error, SandboxService}; use chrono::{DateTime, Utc}; use containerd_shim::{ self as shim, api, @@ -339,7 +339,6 @@ where events: Arc>, exit: Arc, namespace: String, - containerd_address: Option } #[cfg(test)] @@ -425,7 +424,6 @@ mod localtests { tx, Arc::new(ExitSignal::default()), "test_namespace".into(), - Some("/pipe/to/containerd".into()), )); let mut _wrapped = LocalWithDescrutor::new(local.clone()); @@ -456,7 +454,6 @@ mod localtests { etx, exit_signal, "test_namespace".into(), - Some("/pipe/to/containerd".into()), )); let mut _wrapped = LocalWithDescrutor::new(local.clone()); @@ -625,7 +622,6 @@ mod localtests { etx, exit_signal, "test_namespace".into(), - Some("/pipe/to/containerd".into()), )); let mut _wrapped = LocalWithDescrutor::new(local.clone()); @@ -739,7 +735,6 @@ where tx: Sender<(String, Box)>, exit: Arc, namespace: String, - containerd_address: Option, ) -> Self where T: Instance + Sync + Send, @@ -752,12 +747,11 @@ where events: Arc::new(Mutex::new(tx)), exit, namespace, - containerd_address, } } fn new_base(&self, id: String) -> InstanceData { - let cfg = InstanceConfig::new(self.engine.clone(), self.namespace.clone(), self.containerd_address.clone()); + let cfg = InstanceConfig::new(self.engine.clone(), self.namespace.clone()); InstanceData { instance: None, base: Some(Nop::new(id, None)), @@ -948,7 +942,7 @@ where } let engine = self.engine.clone(); - let mut builder = InstanceConfig::new(engine, self.namespace.clone(), self.containerd_address.clone()); + let mut builder = InstanceConfig::new(engine, self.namespace.clone()); builder .set_stdin(req.get_stdin().into()) .set_stdout(req.get_stdout().into()) @@ -1207,13 +1201,6 @@ where { type Instance = T; fn new(namespace: String, _id: String, engine: E, publisher: RemotePublisher) -> Self { - - let os_args: Vec<_> = env::args_os().collect(); - let containerd_address = match containerd::parse(&os_args[1..]) { - Ok(flags) => Some(flags.address), - _ => None - }; - let (tx, rx) = channel::<(String, Box)>(); forward_events(namespace.clone(), publisher, rx); Local::::new( @@ -1221,7 +1208,6 @@ where tx.clone(), Arc::new(ExitSignal::default()), namespace, - containerd_address, ) } } @@ -1358,7 +1344,6 @@ where { pub engine: E, namespace: String, - containerd_address: Option, phantom: std::marker::PhantomData, exit: Arc, _id: String, @@ -1373,17 +1358,10 @@ where fn new(_runtime_id: &str, id: &str, namespace: &str, _config: &mut shim::Config) -> Self { // Ideally this function passes in either the containerd address or the flags from the cli - let os_args: Vec<_> = env::args_os().collect(); - let containerd_address = match containerd::parse(&os_args[1..]) { - Ok(flags) => Some(flags.address), - _ => None - }; - Cli { engine: I::new_engine().unwrap(), phantom: std::marker::PhantomData, namespace: namespace.to_string(), - containerd_address: containerd_address, exit: Arc::new(ExitSignal::default()), _id: id.to_string(), } @@ -1507,7 +1485,6 @@ where tx.clone(), self.exit.clone(), self.namespace.clone(), - self.containerd_address.clone(), ) } diff --git a/crates/containerd-shim-wasmedge/src/executor.rs b/crates/containerd-shim-wasmedge/src/executor.rs index f6ad23a70..ed6aed541 100644 --- a/crates/containerd-shim-wasmedge/src/executor.rs +++ b/crates/containerd-shim-wasmedge/src/executor.rs @@ -6,7 +6,7 @@ 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, env}; +use std::{os::unix::io::RawFd}; use wasmedge_sdk::{ config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions}, params, VmBuilder, @@ -18,6 +18,8 @@ pub struct WasmEdgeExecutor { pub stdin: Option, pub stdout: Option, pub stderr: Option, + pub namespace: String, + pub containerd_address: Option, } impl Executor for WasmEdgeExecutor { @@ -65,14 +67,22 @@ impl Executor for WasmEdgeExecutor { let vm = match oci::get_oci_artifact(spec) { Some(oci_module) => { debug!("loading module from annotations"); - let os_args: Vec<_> = env::args_os().collect(); - let flags = containerd::parse(&os_args[1..]).map_err(|err| ExecutorError::Execution(err.into()))?; - let mut ctrd_client = containerd::SyncContentClient::connect( - flags.address, - ) - .map_err(|err| ExecutorError::Execution(err.into()))?; + let containerd_address = match &self.containerd_address { + Some(addr) => addr.clone(), + None => { + return Err(ExecutorError::Execution( + anyhow::Error::msg( + "no containerd address provided, cannot load module from containerd", + ) + .into(), + )) + } + }; + + let mut ctrd_client = containerd::SyncContentClient::connect(containerd_address) + .map_err(|err| ExecutorError::Execution(err.into()))?; let module = ctrd_client - .read_content(oci_module, &flags.namespace) + .read_content(oci_module, &self.namespace) .map_err(|err| ExecutorError::Execution(err.into()))?; vm.register_module_from_bytes("main", module) diff --git a/crates/containerd-shim-wasmedge/src/instance.rs b/crates/containerd-shim-wasmedge/src/instance.rs index 52dbe5f5c..c5a774cb8 100644 --- a/crates/containerd-shim-wasmedge/src/instance.rs +++ b/crates/containerd-shim-wasmedge/src/instance.rs @@ -53,6 +53,9 @@ pub struct Wasi { bundle: String, rootdir: PathBuf, + + namespace: String, + containerd_address: Option, } fn construct_container_root>(root_path: P, container_id: &str) -> Result { @@ -207,12 +210,14 @@ impl Instance for Wasi { let namespace = cfg.get_namespace(); Wasi { id, - rootdir: determine_rootdir(bundle.as_str(), namespace).unwrap(), + rootdir: determine_rootdir(bundle.as_str(), namespace.clone()).unwrap(), exit_code: Arc::new((Mutex::new(None), Condvar::new())), stdin: cfg.get_stdin().unwrap_or_default(), stdout: cfg.get_stdout().unwrap_or_default(), stderr: cfg.get_stderr().unwrap_or_default(), bundle, + namespace: namespace.clone(), + containerd_address: cfg.get_containerd_address(), } } @@ -328,6 +333,8 @@ impl Wasi { stdin, stdout, stderr, + namespace: self.namespace.clone(), + containerd_address: self.containerd_address.clone(), })])? .with_root_path(self.rootdir.clone())? .as_init(&self.bundle) @@ -438,7 +445,7 @@ mod wasitest { spec.save(dir.path().join("config.json"))?; - let mut cfg = InstanceConfig::new(Wasi::new_engine()?, "test_namespace".into(), Some("/test/pipe".into())); + let mut cfg = InstanceConfig::new(Wasi::new_engine()?, "test_namespace".into()); let cfg = cfg .set_bundle(dir.path().to_str().unwrap().to_string()) .set_stdout(dir.path().join("stdout").to_str().unwrap().to_string()); @@ -474,7 +481,7 @@ mod wasitest { let vm = VmBuilder::new().with_config(config).build().unwrap(); let i = Wasi::new( "".to_string(), - Some(&InstanceConfig::new(vm, "test_namespace".into(), Some("".into()))), + Some(&InstanceConfig::new(vm, "test_namespace".into())), ); i.delete().unwrap(); } diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index e2a17548e..cdd6f4f44 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -86,14 +86,11 @@ fn load_spec(bundle: String) -> Result { impl Wasi { fn prepare_module(&self, spec: &oci::Spec) -> Result<(WasiCtx, Module, String), WasmtimeError> { - let stdin_path = self.stdin.clone(); let stdout_path = self.stdout.clone(); - let stderr_path = self.stderr.clone(); - + let stderr_path = self.stderr.clone(); let engine = self.engine.clone(); - debug!("opening rootfs"); let rootfs = oci_wasmtime::get_rootfs(spec)?; let args = oci::get_args(spec); @@ -352,7 +349,6 @@ mod wasitest { Some(&InstanceConfig::new( Engine::default(), "test_namespace".into(), - Some("/pipe/to/containerd".into()), )), ); i.delete().unwrap(); @@ -403,7 +399,7 @@ mod wasitest { } fn run_module(dir: tempfile::TempDir) -> Result<(), Error> { - let mut cfg = InstanceConfig::new(Engine::default(), "test_namespace".into(), Some("/pipe/to/containerd".into())); + let mut cfg = InstanceConfig::new(Engine::default(), "test_namespace".into()); let cfg = cfg .set_bundle(dir.path().to_str().unwrap().to_string()) .set_stdout(dir.path().join("stdout").to_str().unwrap().to_string());