4 releases (2 breaking)

0.3.0 Mar 10, 2022
0.2.0 Dec 30, 2021
0.1.1 Dec 10, 2021
0.1.0 Dec 10, 2021

#6 in #shim

Download history 107/week @ 2022-10-10 100/week @ 2022-10-17 21/week @ 2022-10-24 124/week @ 2022-10-31 30/week @ 2022-11-07 81/week @ 2022-11-14 139/week @ 2022-11-21 47/week @ 2022-11-28 30/week @ 2022-12-05 44/week @ 2022-12-12 57/week @ 2022-12-19 54/week @ 2022-12-26 67/week @ 2023-01-02 108/week @ 2023-01-09 81/week @ 2023-01-16 57/week @ 2023-01-23

315 downloads per month
Used in containerd-runc-shim

Apache-2.0

1MB
21K SLoC

Shim extension for containerd

Crates.io docs.rs Crates.io CI

Rust crate to ease runtime v2 shim implementation.

It replicates same shim.Run API offered by containerd's shim v2 runtime implementation written in Go.

Look and feel

The API is very similar to the one offered by Go version:

#[derive(Clone)]
struct Service {
    exit: ExitSignal,
}

impl shim::Shim for Service {
    type Error = shim::Error;
    type T = Service;

    fn new(
        _runtime_id: &str,
        _id: &str,
        _namespace: &str,
        _publisher: shim::RemotePublisher,
        _config: &mut shim::Config,
    ) -> Self {
        Service {
            exit: ExitSignal::default(),
        }
    }

    fn start_shim(&mut self, opts: shim::StartOpts) -> Result<String, shim::Error> {
        let address = shim::spawn(opts, Vec::new())?;
        Ok(address)
    }

    fn wait(&mut self) {
        self.exit.wait();
    }

    fn get_task_service(&self) -> Self::T {
        self.clone()
    }
}

impl shim::Task for Service {
    fn connect(
        &self,
        _ctx: &TtrpcContext,
        _req: api::ConnectRequest,
    ) -> TtrpcResult<api::ConnectResponse> {
        info!("Connect request");
        Ok(api::ConnectResponse {
            version: String::from("example"),
            ..Default::default()
        })
    }

    fn shutdown(&self, _ctx: &TtrpcContext, _req: api::ShutdownRequest) -> TtrpcResult<api::Empty> {
        info!("Shutdown request");
        self.exit.signal(); // Signal to shutdown shim server
        Ok(api::Empty::default())
    }
}

fn main() {
    shim::run::<Service>("io.containerd.empty.v1")
}

How to use with containerd

Note: All operations are in the root directory of rust-extensions.

With shim v2 runtime:

$ cargo build --example skeleton
$ sudo cp ./target/debug/examples/skeleton /usr/local/bin/containerd-shim-skeleton-v1
$ sudo ctr run --rm --runtime io.containerd.skeleton.v1 -t docker.io/library/hello-world:latest hello

Or if on 1.6+

$ cargo build --example skeleton
$ sudo ctr run --rm --runtime ./target/debug/examples/skeleton docker.io/library/hello-world:latest hello

Or manually:

$ touch log

# Run containerd in background
$ sudo TTRPC_ADDRESS="/var/run/containerd/containerd.sock.ttrpc" \
    cargo run --example skeleton -- \
    -namespace default \
    -id 1234 \
    -address /var/run/containerd/containerd.sock \
    -publish-binary ./bin/containerd \
    start
unix:///var/run/containerd/eb8e7d1c48c2a1ec.sock

$ cargo build --example shim-proto-connect
$ sudo ./target/debug/examples/shim-proto-connect unix:///var/run/containerd/eb8e7d1c48c2a1ec.sock
Connecting to unix:///var/run/containerd/eb8e7d1c48c2a1ec.sock...
Sending `Connect` request...
Connect response: version: "example"
Sending `Shutdown` request...
Shutdown response: ""

$ cat log
[INFO] server listen started
[INFO] server started
[INFO] Shim successfully started, waiting for exit signal...
[INFO] Connect request
[INFO] Shutdown request
[INFO] Shutting down shim instance
[INFO] close monitor
[INFO] listener shutdown for quit flag
[INFO] ttrpc server listener stopped
[INFO] listener thread stopped
[INFO] begin to shutdown connection
[INFO] connections closed
[INFO] reaper thread exited
[INFO] reaper thread stopped

Supported Platforms

Currently, following OSs and hardware architectures are supported, and more efforts are needed to enable and validate other OSs and architectures.

  • Linux
  • Mac OS

Dependencies

~5–11MB
~194K SLoC