containerd_shim_wasm/shim/mod.rs
1//! This module contains an API for building WebAssembly shims running on top of containers.
2//! Unlike the `sandbox` module, this module delegates many of the actions to the container runtime.
3//!
4//! This has some advantages:
5//! * Simplifies writing new shims, get you up and running quickly
6//! * The complexity of the OCI spec is already taken care of
7//!
8//! But it also has some disadvantages:
9//! * Runtime overhead in in setting up a container
10//! * Less customizable
11//! * Currently only works on Linux
12//!
13//! ## Key Components
14//!
15//! - [`Shim`]: The trait for implementing the shim entrypoint
16//! - [`Sandbox`](crate::sandbox::Sandbox): The core trait for implementing Wasm runtimes
17//! - [`RuntimeContext`](crate::sandbox::context::RuntimeContext): The context for running WASI modules
18//!
19//! ## Version Information
20//!
21//! The module provides two macros for version information:
22//!
23//! - [`version!()`](crate::shim::version) - Returns the crate version from Cargo.toml and
24//! Git revision hash, if available.
25//!
26//! ## Example Usage
27//!
28//! ```rust
29//! use containerd_shim_wasm::shim::Shim;
30//! use containerd_shim_wasm::sandbox::Sandbox;
31//! use containerd_shim_wasm::sandbox::context::RuntimeContext;
32//! use anyhow::Result;
33//!
34//! struct MyShim;
35//!
36//! #[derive(Default)]
37//! struct MySandbox;
38//!
39//! impl Shim for MyShim {
40//! type Sandbox = MySandbox;
41//!
42//! fn name() -> &'static str {
43//! "my-shim"
44//! }
45//! }
46//!
47//! impl Sandbox for MySandbox {
48//! async fn run_wasi(&self, ctx: &impl RuntimeContext) -> Result<i32> {
49//! let args = ctx.args();
50//! let envs = ctx.envs();
51//! let entrypoint = ctx.entrypoint();
52//!
53//! Ok(0)
54//! }
55//! }
56//! ```
57
58#[allow(clippy::module_inception)]
59mod shim;
60
61pub(crate) use instance::Instance;
62pub use shim::{Compiler, Shim, Version};
63
64use crate::sys::container::instance;
65
66#[cfg(test)]
67mod tests;
68
69// This is used in containerd::Client tests
70#[cfg(test)]
71pub(crate) use shim::NO_COMPILER;
72
73pub(crate) mod cli;
74
75pub use cli::Cli;
76pub use containerd_shimkit::shim_version as version;
77
78/// Config of shim binary options provided by shim implementations
79#[derive(Debug)]
80pub struct Config {
81 /// Disables automatic configuration to use the shim FIFO
82 pub no_setup_logger: bool,
83 /// Sets the the default log level. Default is info
84 pub default_log_level: String,
85}
86
87impl Default for Config {
88 fn default() -> Self {
89 Self {
90 no_setup_logger: false,
91 default_log_level: "info".to_string(),
92 }
93 }
94}