containerd_shim_wasm/shim/
cli.rs

1//! Command line interface for the containerd shim.
2//!
3//! The CLI provides the interface between containerd and the Wasm runtime.
4//! It handles commands like start and delete from containerd's shim API.
5//!
6//! ## Usage
7//!
8//! The shim binary should be named `containerd-shim-<engine>-v1` and installed in $PATH.
9//! containerd will call the shim with various commands.
10//!
11//! ## Configuration
12//!
13//! The shim can be configured using the [`Config`] struct:
14//!
15//! ```rust, no_run
16//! use containerd_shim_wasm::shim::Config;
17//!
18//! let config = Config {
19//!     // Disable automatic logger setup
20//!     no_setup_logger: false,
21//!     // Set default log level
22//!     default_log_level: "info".to_string(),
23//! };
24//! ```
25//!
26//! ## Example usage:
27//!
28//! ```rust, no_run
29//! use containerd_shim_wasm::{
30//!     shim::{Shim, Cli, Config, Version, version},
31//!     sandbox::Sandbox,
32//!     sandbox::context::RuntimeContext,
33//! };
34//! use anyhow::Result;
35//!
36//! struct MyShim;
37//!
38//! #[derive(Default)]
39//! struct MySandbox;
40//!
41//! impl Shim for MyShim {
42//!     type Sandbox = MySandbox;
43//!
44//!     fn name() -> &'static str {
45//!         "my-shim"
46//!     }
47//!
48//!     fn version() -> Version {
49//!         version!()
50//!     }
51//! }
52//!
53//! impl Sandbox for MySandbox {
54//!     async fn run_wasi(&self, ctx: &impl RuntimeContext) -> Result<i32> {
55//!         Ok(0)
56//!     }
57//! }
58//!
59//! let config = Config {
60//!     default_log_level: "error".to_string(),
61//!     ..Default::default()
62//! };
63//!
64//! MyShim::run(config);
65//! ```
66//!
67//! When the `opentelemetry` feature is enabled, additional runtime config
68//! is available through environment variables:
69//!
70//! - `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`: Enable OpenTelemetry tracing
71//! - `OTEL_EXPORTER_OTLP_ENDPOINT`: Enable OpenTelemetry tracing as above
72//! - `OTEL_SDK_DISABLED`: Disable OpenTelemetry SDK
73//!
74
75use crate::shim::{Config, Instance, Shim};
76
77mod private {
78    pub trait Sealed {}
79}
80
81impl<S: Shim> private::Sealed for S {}
82
83pub trait Cli: Shim + private::Sealed {
84    /// Main entry point for the shim.
85    ///
86    /// If the `opentelemetry` feature is enabled, this function will start the shim with OpenTelemetry tracing.
87    ///
88    /// It parses OTLP configuration from the environment and initializes the OpenTelemetry SDK.
89    fn run(config: impl Into<Option<Config>>);
90}
91
92impl<S: Shim> Cli for S {
93    fn run(config: impl Into<Option<Config>>) {
94        let config = config.into().unwrap_or_default();
95        let config = containerd_shimkit::Config {
96            no_setup_logger: config.no_setup_logger,
97            default_log_level: config.default_log_level,
98            no_reaper: false,
99            no_sub_reaper: false,
100        };
101        containerd_shimkit::sandbox::cli::shim_main::<Instance<S>>(
102            S::name(),
103            S::version(),
104            Some(config),
105        )
106    }
107}