Module cli

Source
Expand description

Command line interface for the containerd shim.

The CLI provides the interface between containerd and the Wasm runtime. It handles commands like start and delete from containerd’s shim API.

§Usage

The shim binary should be named containerd-shim-<engine>-v1 and installed in $PATH. containerd will call the shim with various commands.

§Configuration

The shim can be configured using the Config struct:

use containerd_shimkit::Config;

let config = Config {
    // Disable automatic logger setup
    no_setup_logger: false,
    // Set default log level
    default_log_level: "info".to_string(),
    // Disable child process reaping
    no_reaper: false,
    // Disable subreaper setting
    no_sub_reaper: false,
};

§Version Information

The module provides two macros for version information:

  • version!() - Returns the crate version from Cargo.toml
  • revision!() - Returns the Git revision hash, if available

§Example usage:

use containerd_shimkit::{
    shim_version,
    sandbox::{Instance, InstanceConfig, Result},
    sandbox::cli::shim_main,
    sandbox::sync::WaitableCell,
    Config,
};
use tokio::time::sleep;
use chrono::{DateTime, Utc};
use std::time::Duration;

#[derive(Clone, Default)]
struct MyInstance {
    exit_code: WaitableCell<(u32, DateTime<Utc>)>,
};

impl Instance for MyInstance {
    async fn new(id: String, cfg: &InstanceConfig) -> Result<Self> {
        let exit_code = WaitableCell::new();
        Ok(Self { exit_code })
    }
    async fn start(&self) -> Result<u32> {
        let exit_code = self.exit_code.clone();
        tokio::spawn(async move {
            sleep(Duration::from_millis(100)).await;
            let _ = exit_code.set((0, Utc::now()));
        });
        Ok(42) // some id for our task, usually a PID
    }
    async fn kill(&self, signal: u32) -> Result<()> {
        Ok(()) // no-op
    }
    async fn delete(&self) -> Result<()> {
        Ok(()) // no-op
    }
    async fn wait(&self) -> (u32, DateTime<Utc>) {
        *self.exit_code.wait().await
    }
}

let config = Config {
    default_log_level: "error".to_string(),
    ..Default::default()
};

shim_main::<MyInstance>(
    "my-engine",
    shim_version!(),
    Some(config),
);

When the opentelemetry feature is enabled, additional runtime config is available through environment variables:

  • OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: Enable OpenTelemetry tracing
  • OTEL_EXPORTER_OTLP_ENDPOINT: Enable OpenTelemetry tracing as above
  • OTEL_SDK_DISABLED: Disable OpenTelemetry SDK

Modules§

impl

Macros§

revision
Get the Git revision hash, if available.
version
Get the crate version from Cargo.toml.

Structs§

Version

Functions§

shim_main
Main entry point for the shim.