Skip to content

Commit

Permalink
delay creating the engine
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Prendes <[email protected]>
  • Loading branch information
jprendes committed Dec 20, 2024
1 parent 1082ac4 commit 7d16085
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
20 changes: 12 additions & 8 deletions crates/containerd-shim-wasm/src/sys/unix/container/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ enum InnerExecutor {
}

#[derive(Clone)]
pub(crate) struct Executor<E: Engine> {
engine: E,
pub(crate) struct Executor<E: Engine + Default> {
engine: OnceCell<E>,
stdio: Stdio,
inner: OnceCell<InnerExecutor>,
wasm_layers: Vec<WasmLayer>,
platform: Platform,
}

impl<E: Engine> LibcontainerExecutor for Executor<E> {
impl<E: Engine + Default> LibcontainerExecutor for Executor<E> {
#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))]
fn validate(&self, spec: &Spec) -> Result<(), ExecutorValidationError> {
// We can handle linux container. We delegate wasm container to the engine.
Expand All @@ -56,7 +56,7 @@ impl<E: Engine> LibcontainerExecutor for Executor<E> {
}
InnerExecutor::Wasm => {
log::info!("calling start function");
match self.engine.run_wasi(&self.ctx(spec), self.stdio.take()) {
match self.engine().run_wasi(&self.ctx(spec), self.stdio.take()) {
Ok(code) => std::process::exit(code),
Err(err) => {
log::info!("error running start function: {err}");
Expand All @@ -83,10 +83,10 @@ impl<E: Engine> LibcontainerExecutor for Executor<E> {
}
}

impl<E: Engine> Executor<E> {
pub fn new(engine: E, stdio: Stdio, wasm_layers: Vec<WasmLayer>, platform: Platform) -> Self {
impl<E: Engine + Default> Executor<E> {
pub fn new(stdio: Stdio, wasm_layers: Vec<WasmLayer>, platform: Platform) -> Self {
Self {
engine,
engine: Default::default(),
stdio,
inner: Default::default(),
wasm_layers,
Expand All @@ -111,7 +111,7 @@ impl<E: Engine> Executor<E> {
Ok(_) => InnerExecutor::Linux,
Err(err) => {
log::debug!("error checking if linux container: {err}. Fallback to wasm container");
match self.engine.can_handle(ctx) {
match self.engine().can_handle(ctx) {
Ok(_) => InnerExecutor::Wasm,
Err(err) => {
// log an error and return
Expand All @@ -123,6 +123,10 @@ impl<E: Engine> Executor<E> {
}
})
}

fn engine(&self) -> &E {
self.engine.get_or_init(|| E::default())
}
}

fn is_linux_container(ctx: &impl RuntimeContext) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ impl<E: Engine + Default> SandboxInstance for Instance<E> {

#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))]
fn new(id: String, cfg: &InstanceConfig) -> Result<Self, SandboxError> {
let engine = Self::Engine::default();
let bundle = cfg.get_bundle().to_path_buf();
let namespace = cfg.get_namespace();
let rootdir = Path::new(DEFAULT_CONTAINER_ROOT_DIR).join(E::name());
Expand All @@ -55,7 +54,7 @@ impl<E: Engine + Default> SandboxInstance for Instance<E> {
});

let container = ContainerBuilder::new(id.clone(), SyscallType::Linux)
.with_executor(Executor::new(engine, stdio, modules, platform))
.with_executor(Executor::<E>::new(stdio, modules, platform))
.with_root_path(rootdir.clone())?
.as_init(&bundle)
.with_systemd(false)
Expand Down

0 comments on commit 7d16085

Please sign in to comment.