Skip to content

Commit

Permalink
Attempt at mapping it all the way through
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant committed Jun 23, 2023
1 parent 95d5955 commit 89bb778
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 83 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());
let mut cfg = InstanceConfig::new(WasmtimeWasi::new_engine()?, "test_namespace".into(), None);
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());
let mut cfg = InstanceConfig::new(WasmEdgeWasi::new_engine()?, "test_namespace".into(), None);
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: 10 additions & 1 deletion crates/containerd-shim-wasm/src/sandbox/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@ where
bundle: Option<String>,
/// Namespace for containerd
namespace: String,
// /// GRPC address back to main containerd
containerd_address: Option<String>,
}

impl<E> InstanceConfig<E>
where
E: Send + Sync + Clone,
{
pub fn new(engine: E, namespace: String) -> Self {
pub fn new(engine: E, namespace: String, containerd_address: Option<String>) -> Self {
// todo read containerd address
Self {
engine,
namespace,
containerd_address,
stdin: None,
stdout: None,
stderr: None,
Expand Down Expand Up @@ -102,6 +106,11 @@ where
pub fn get_namespace(&self) -> String {
self.namespace.clone()
}

/// get the containerd address for the instance
pub fn get_containerd_address(&self) -> Option<String> {
self.containerd_address.clone()
}
}

/// Represents a wasi module(s).
Expand Down
32 changes: 28 additions & 4 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;
use std::env::{current_dir, self};
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};
use super::{oci, Error, SandboxService, containerd};
use chrono::{DateTime, Utc};
use containerd_shim::{
self as shim, api,
Expand Down Expand Up @@ -339,6 +339,7 @@ where
events: Arc<Mutex<EventSender>>,
exit: Arc<ExitSignal>,
namespace: String,
containerd_address: Option<String>
}

#[cfg(test)]
Expand Down Expand Up @@ -424,6 +425,7 @@ 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 @@ -454,6 +456,7 @@ mod localtests {
etx,
exit_signal,
"test_namespace".into(),
Some("/pipe/to/containerd".into()),
));

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

let mut _wrapped = LocalWithDescrutor::new(local.clone());
Expand Down Expand Up @@ -735,6 +739,7 @@ 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 @@ -747,11 +752,12 @@ 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());
let cfg = InstanceConfig::new(self.engine.clone(), self.namespace.clone(), self.containerd_address.clone());
InstanceData {
instance: None,
base: Some(Nop::new(id, None)),
Expand Down Expand Up @@ -942,7 +948,7 @@ where
}

let engine = self.engine.clone();
let mut builder = InstanceConfig::new(engine, self.namespace.clone());
let mut builder = InstanceConfig::new(engine, self.namespace.clone(), self.containerd_address.clone());
builder
.set_stdin(req.get_stdin().into())
.set_stdout(req.get_stdout().into())
Expand Down Expand Up @@ -1201,13 +1207,21 @@ 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 @@ -1344,6 +1358,7 @@ where
{
pub engine: E,
namespace: String,
containerd_address: Option<String>,
phantom: std::marker::PhantomData<T>,
exit: Arc<ExitSignal>,
_id: String,
Expand All @@ -1357,10 +1372,18 @@ where
type T = Local<I, E>;

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 @@ -1484,6 +1507,7 @@ where
tx.clone(),
self.exit.clone(),
self.namespace.clone(),
self.containerd_address.clone(),
)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/containerd-shim-wasmedge/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ mod wasitest {

spec.save(dir.path().join("config.json"))?;

let mut cfg = InstanceConfig::new(Wasi::new_engine()?, "test_namespace".into());
let mut cfg = InstanceConfig::new(Wasi::new_engine()?, "test_namespace".into(), Some("/test/pipe".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 +474,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(&InstanceConfig::new(vm, "test_namespace".into(), Some("".into()))),
);
i.delete().unwrap();
}
Expand Down
Loading

0 comments on commit 89bb778

Please sign in to comment.