Skip to content

Commit

Permalink
Happy medium (?) for getting containerd path
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <jstur@microsoft.com>
  • Loading branch information
jsturtevant committed Jun 23, 2023
1 parent 89bb778 commit cbf81c8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
11 changes: 8 additions & 3 deletions crates/containerd-shim-wasm/src/sandbox/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<(u32, DateTime<Utc>)>>, Condvar);
Expand Down Expand Up @@ -40,8 +41,12 @@ impl<E> InstanceConfig<E>
where
E: Send + Sync + Clone,
{
pub fn new(engine: E, namespace: String, containerd_address: Option<String>) -> 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,
Expand Down
31 changes: 4 additions & 27 deletions crates/containerd-shim-wasm/src/sandbox/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -339,7 +339,6 @@ where
events: Arc<Mutex<EventSender>>,
exit: Arc<ExitSignal>,
namespace: String,
containerd_address: Option<String>
}

#[cfg(test)]
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -456,7 +454,6 @@ mod localtests {
etx,
exit_signal,
"test_namespace".into(),
Some("/pipe/to/containerd".into()),
));

let mut _wrapped = LocalWithDescrutor::new(local.clone());
Expand Down Expand Up @@ -625,7 +622,6 @@ mod localtests {
etx,
exit_signal,
"test_namespace".into(),
Some("/pipe/to/containerd".into()),
));

let mut _wrapped = LocalWithDescrutor::new(local.clone());
Expand Down Expand Up @@ -739,7 +735,6 @@ where
tx: Sender<(String, Box<dyn Message>)>,
exit: Arc<ExitSignal>,
namespace: String,
containerd_address: Option<String>,
) -> Self
where
T: Instance<E = E> + Sync + Send,
Expand All @@ -752,12 +747,11 @@ where
events: Arc::new(Mutex::new(tx)),
exit,
namespace,
containerd_address,
}
}

fn new_base(&self, id: String) -> InstanceData<T, E> {
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)),
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -1207,21 +1201,13 @@ 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<dyn Message>)>();
forward_events(namespace.clone(), publisher, rx);
Local::<T, E>::new(
engine,
tx.clone(),
Arc::new(ExitSignal::default()),
namespace,
containerd_address,
)
}
}
Expand Down Expand Up @@ -1358,7 +1344,6 @@ where
{
pub engine: E,
namespace: String,
containerd_address: Option<String>,
phantom: std::marker::PhantomData<T>,
exit: Arc<ExitSignal>,
_id: String,
Expand All @@ -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(),
}
Expand Down Expand Up @@ -1507,7 +1485,6 @@ where
tx.clone(),
self.exit.clone(),
self.namespace.clone(),
self.containerd_address.clone(),
)
}

Expand Down
26 changes: 18 additions & 8 deletions crates/containerd-shim-wasmedge/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,6 +18,8 @@ pub struct WasmEdgeExecutor {
pub stdin: Option<RawFd>,
pub stdout: Option<RawFd>,
pub stderr: Option<RawFd>,
pub namespace: String,
pub containerd_address: Option<String>,
}

impl Executor for WasmEdgeExecutor {
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions crates/containerd-shim-wasmedge/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub struct Wasi {
bundle: String,

rootdir: PathBuf,

namespace: String,
containerd_address: Option<String>,
}

fn construct_container_root<P: AsRef<Path>>(root_path: P, container_id: &str) -> Result<PathBuf> {
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
}
Expand Down
8 changes: 2 additions & 6 deletions crates/containerd-shim-wasmtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,11 @@ fn load_spec(bundle: String) -> Result<oci::Spec, Error> {

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);
Expand Down Expand Up @@ -352,7 +349,6 @@ mod wasitest {
Some(&InstanceConfig::new(
Engine::default(),
"test_namespace".into(),
Some("/pipe/to/containerd".into()),
)),
);
i.delete().unwrap();
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit cbf81c8

Please sign in to comment.