Skip to content

Commit bf84bcc

Browse files
committed
Happy medium (?) for getting containerd path
Signed-off-by: James Sturtevant <[email protected]>
1 parent 89bb778 commit bf84bcc

File tree

7 files changed

+62
-74
lines changed

7 files changed

+62
-74
lines changed

benches/containerd-shim-benchmarks/benches/webassembly-benchmarks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn run_wasmtime_test_with_spec(
108108

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

111-
let mut cfg = InstanceConfig::new(WasmtimeWasi::new_engine()?, "test_namespace".into(), None);
111+
let mut cfg = InstanceConfig::new(WasmtimeWasi::new_engine()?, "test_namespace".into());
112112
let cfg = cfg
113113
.set_bundle(dir.path().to_str().unwrap().to_string())
114114
.set_stdout(dir.path().join("stdout").to_str().unwrap().to_string());
@@ -169,7 +169,7 @@ fn run_wasmedge_test_with_spec(
169169

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

172-
let mut cfg = InstanceConfig::new(WasmEdgeWasi::new_engine()?, "test_namespace".into(), None);
172+
let mut cfg = InstanceConfig::new(WasmEdgeWasi::new_engine()?, "test_namespace".into());
173173
let cfg = cfg
174174
.set_bundle(dir.path().to_str().unwrap().to_string())
175175
.set_stdout(dir.path().join("stdout").to_str().unwrap().to_string());

crates/containerd-shim-wasm/src/sandbox/containerd.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use client::{
99
with_namespace,
1010
};
1111
use containerd_client as client;
12-
use tonic::Request;
1312
use std::ffi::OsStr;
13+
use tonic::Request;
1414

1515
pub struct SyncContentClient {
1616
inner: ContentClient<Channel>,
@@ -69,10 +69,9 @@ impl SyncContentClient {
6969
}
7070
}
7171

72-
73-
// this is from https://github.com/containerd/rust-extensions/blob/main/crates/shim/src/error.rs
74-
// as it not exported
75-
// TODO export upstream and remove this
72+
// this is from https://github.com/containerd/rust-extensions/blob/main/crates/shim/src/error.rs
73+
// as it not exported
74+
// TODO export upstream and remove this
7675
#[derive(Debug, Default)]
7776
pub struct Flags {
7877
/// Enable debug output in logs.
@@ -121,4 +120,4 @@ pub fn parse<S: AsRef<OsStr>>(args: &[S]) -> Result<Flags> {
121120
}
122121

123122
Ok(flags)
124-
}
123+
}

crates/containerd-shim-wasm/src/sandbox/instance.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
33
use std::sync::mpsc::Sender;
44
use std::sync::{Arc, Condvar, Mutex};
5-
use std::thread;
5+
use std::{env, thread};
66

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

99
use chrono::{DateTime, Utc};
1010

11+
use super::containerd;
1112
use super::error::Error;
1213

1314
type ExitCode = (Mutex<Option<(u32, DateTime<Utc>)>>, Condvar);
@@ -40,8 +41,12 @@ impl<E> InstanceConfig<E>
4041
where
4142
E: Send + Sync + Clone,
4243
{
43-
pub fn new(engine: E, namespace: String, containerd_address: Option<String>) -> Self {
44-
// todo read containerd address
44+
pub fn new(engine: E, namespace: String) -> Self {
45+
let os_args: Vec<_> = env::args_os().collect();
46+
let containerd_address = match containerd::parse(&os_args[1..]) {
47+
Ok(flags) => Some(flags.address),
48+
_ => None,
49+
};
4550
Self {
4651
engine,
4752
namespace,

crates/containerd-shim-wasm/src/sandbox/shim.rs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! the container/sandbox.
44
55
use std::collections::HashMap;
6-
use std::env::{current_dir, self};
6+
use std::env::current_dir;
77
use std::fs::{self, File};
88
use std::fs::{canonicalize, create_dir_all, OpenOptions};
99
use std::ops::Not;
@@ -14,7 +14,7 @@ use std::sync::{Arc, Condvar, Mutex, RwLock};
1414
use std::thread;
1515

1616
use super::instance::{EngineGetter, Instance, InstanceConfig, Nop, Wait};
17-
use super::{oci, Error, SandboxService, containerd};
17+
use super::{oci, Error, SandboxService};
1818
use chrono::{DateTime, Utc};
1919
use containerd_shim::{
2020
self as shim, api,
@@ -339,7 +339,6 @@ where
339339
events: Arc<Mutex<EventSender>>,
340340
exit: Arc<ExitSignal>,
341341
namespace: String,
342-
containerd_address: Option<String>
343342
}
344343

345344
#[cfg(test)]
@@ -425,7 +424,6 @@ mod localtests {
425424
tx,
426425
Arc::new(ExitSignal::default()),
427426
"test_namespace".into(),
428-
Some("/pipe/to/containerd".into()),
429427
));
430428
let mut _wrapped = LocalWithDescrutor::new(local.clone());
431429

@@ -456,7 +454,6 @@ mod localtests {
456454
etx,
457455
exit_signal,
458456
"test_namespace".into(),
459-
Some("/pipe/to/containerd".into()),
460457
));
461458

462459
let mut _wrapped = LocalWithDescrutor::new(local.clone());
@@ -625,7 +622,6 @@ mod localtests {
625622
etx,
626623
exit_signal,
627624
"test_namespace".into(),
628-
Some("/pipe/to/containerd".into()),
629625
));
630626

631627
let mut _wrapped = LocalWithDescrutor::new(local.clone());
@@ -739,7 +735,6 @@ where
739735
tx: Sender<(String, Box<dyn Message>)>,
740736
exit: Arc<ExitSignal>,
741737
namespace: String,
742-
containerd_address: Option<String>,
743738
) -> Self
744739
where
745740
T: Instance<E = E> + Sync + Send,
@@ -752,12 +747,11 @@ where
752747
events: Arc::new(Mutex::new(tx)),
753748
exit,
754749
namespace,
755-
containerd_address,
756750
}
757751
}
758752

759753
fn new_base(&self, id: String) -> InstanceData<T, E> {
760-
let cfg = InstanceConfig::new(self.engine.clone(), self.namespace.clone(), self.containerd_address.clone());
754+
let cfg = InstanceConfig::new(self.engine.clone(), self.namespace.clone());
761755
InstanceData {
762756
instance: None,
763757
base: Some(Nop::new(id, None)),
@@ -948,7 +942,7 @@ where
948942
}
949943

950944
let engine = self.engine.clone();
951-
let mut builder = InstanceConfig::new(engine, self.namespace.clone(), self.containerd_address.clone());
945+
let mut builder = InstanceConfig::new(engine, self.namespace.clone());
952946
builder
953947
.set_stdin(req.get_stdin().into())
954948
.set_stdout(req.get_stdout().into())
@@ -1207,21 +1201,13 @@ where
12071201
{
12081202
type Instance = T;
12091203
fn new(namespace: String, _id: String, engine: E, publisher: RemotePublisher) -> Self {
1210-
1211-
let os_args: Vec<_> = env::args_os().collect();
1212-
let containerd_address = match containerd::parse(&os_args[1..]) {
1213-
Ok(flags) => Some(flags.address),
1214-
_ => None
1215-
};
1216-
12171204
let (tx, rx) = channel::<(String, Box<dyn Message>)>();
12181205
forward_events(namespace.clone(), publisher, rx);
12191206
Local::<T, E>::new(
12201207
engine,
12211208
tx.clone(),
12221209
Arc::new(ExitSignal::default()),
12231210
namespace,
1224-
containerd_address,
12251211
)
12261212
}
12271213
}
@@ -1358,7 +1344,6 @@ where
13581344
{
13591345
pub engine: E,
13601346
namespace: String,
1361-
containerd_address: Option<String>,
13621347
phantom: std::marker::PhantomData<T>,
13631348
exit: Arc<ExitSignal>,
13641349
_id: String,
@@ -1373,17 +1358,10 @@ where
13731358

13741359
fn new(_runtime_id: &str, id: &str, namespace: &str, _config: &mut shim::Config) -> Self {
13751360
// Ideally this function passes in either the containerd address or the flags from the cli
1376-
let os_args: Vec<_> = env::args_os().collect();
1377-
let containerd_address = match containerd::parse(&os_args[1..]) {
1378-
Ok(flags) => Some(flags.address),
1379-
_ => None
1380-
};
1381-
13821361
Cli {
13831362
engine: I::new_engine().unwrap(),
13841363
phantom: std::marker::PhantomData,
13851364
namespace: namespace.to_string(),
1386-
containerd_address: containerd_address,
13871365
exit: Arc::new(ExitSignal::default()),
13881366
_id: id.to_string(),
13891367
}
@@ -1507,7 +1485,6 @@ where
15071485
tx.clone(),
15081486
self.exit.clone(),
15091487
self.namespace.clone(),
1510-
self.containerd_address.clone(),
15111488
)
15121489
}
15131490

crates/containerd-shim-wasmedge/src/executor.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use oci_spec::runtime::Spec;
66
use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
77
use libcontainer::workload::{Executor, ExecutorError};
88
use log::debug;
9-
use std::{os::unix::io::RawFd, env};
9+
use std::os::unix::io::RawFd;
1010
use wasmedge_sdk::{
1111
config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions},
1212
params, VmBuilder,
@@ -18,6 +18,8 @@ pub struct WasmEdgeExecutor {
1818
pub stdin: Option<RawFd>,
1919
pub stdout: Option<RawFd>,
2020
pub stderr: Option<RawFd>,
21+
pub namespace: String,
22+
pub containerd_address: Option<String>,
2123
}
2224

2325
impl Executor for WasmEdgeExecutor {
@@ -65,14 +67,20 @@ impl Executor for WasmEdgeExecutor {
6567
let vm = match oci::get_oci_artifact(spec) {
6668
Some(oci_module) => {
6769
debug!("loading module from annotations");
68-
let os_args: Vec<_> = env::args_os().collect();
69-
let flags = containerd::parse(&os_args[1..]).map_err(|err| ExecutorError::Execution(err.into()))?;
70-
let mut ctrd_client = containerd::SyncContentClient::connect(
71-
flags.address,
72-
)
73-
.map_err(|err| ExecutorError::Execution(err.into()))?;
70+
let containerd_address = match &self.containerd_address {
71+
Some(addr) => addr.clone(),
72+
None => return Err(ExecutorError::Execution(
73+
anyhow::Error::msg(
74+
"no containerd address provided, cannot load module from containerd",
75+
)
76+
.into(),
77+
)),
78+
};
79+
80+
let mut ctrd_client = containerd::SyncContentClient::connect(containerd_address)
81+
.map_err(|err| ExecutorError::Execution(err.into()))?;
7482
let module = ctrd_client
75-
.read_content(oci_module, &flags.namespace)
83+
.read_content(oci_module, &self.namespace)
7684
.map_err(|err| ExecutorError::Execution(err.into()))?;
7785

7886
vm.register_module_from_bytes("main", module)

crates/containerd-shim-wasmedge/src/instance.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ pub struct Wasi {
5353
bundle: String,
5454

5555
rootdir: PathBuf,
56+
57+
namespace: String,
58+
containerd_address: Option<String>,
5659
}
5760

5861
fn construct_container_root<P: AsRef<Path>>(root_path: P, container_id: &str) -> Result<PathBuf> {
@@ -207,12 +210,14 @@ impl Instance for Wasi {
207210
let namespace = cfg.get_namespace();
208211
Wasi {
209212
id,
210-
rootdir: determine_rootdir(bundle.as_str(), namespace).unwrap(),
213+
rootdir: determine_rootdir(bundle.as_str(), namespace.clone()).unwrap(),
211214
exit_code: Arc::new((Mutex::new(None), Condvar::new())),
212215
stdin: cfg.get_stdin().unwrap_or_default(),
213216
stdout: cfg.get_stdout().unwrap_or_default(),
214217
stderr: cfg.get_stderr().unwrap_or_default(),
215218
bundle,
219+
namespace,
220+
containerd_address: cfg.get_containerd_address(),
216221
}
217222
}
218223

@@ -328,6 +333,8 @@ impl Wasi {
328333
stdin,
329334
stdout,
330335
stderr,
336+
namespace: self.namespace.clone(),
337+
containerd_address: self.containerd_address.clone(),
331338
})])?
332339
.with_root_path(self.rootdir.clone())?
333340
.as_init(&self.bundle)
@@ -438,7 +445,7 @@ mod wasitest {
438445

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

441-
let mut cfg = InstanceConfig::new(Wasi::new_engine()?, "test_namespace".into(), Some("/test/pipe".into()));
448+
let mut cfg = InstanceConfig::new(Wasi::new_engine()?, "test_namespace".into());
442449
let cfg = cfg
443450
.set_bundle(dir.path().to_str().unwrap().to_string())
444451
.set_stdout(dir.path().join("stdout").to_str().unwrap().to_string());
@@ -474,7 +481,7 @@ mod wasitest {
474481
let vm = VmBuilder::new().with_config(config).build().unwrap();
475482
let i = Wasi::new(
476483
"".to_string(),
477-
Some(&InstanceConfig::new(vm, "test_namespace".into(), Some("".into()))),
484+
Some(&InstanceConfig::new(vm, "test_namespace".into())),
478485
);
479486
i.delete().unwrap();
480487
}

0 commit comments

Comments
 (0)