rust-extensions/crates/shim
Liu Jiang 95e8049398 shim: distinguish between runtime id and container id
There's a misuse of runtime id and container id in Shim::new(). Both
runtime id and container id are needed by shim implentions, so add
parameter `container id` to Shim::new().

Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
2021-12-14 13:44:45 +08:00
..
examples shim: distinguish between runtime id and container id 2021-12-14 13:44:45 +08:00
src shim: distinguish between runtime id and container id 2021-12-14 13:44:45 +08:00
Cargo.toml shim: add unit test case for logger 2021-12-13 10:08:10 +08:00
README.md shim: update README.md to run shim examples 2021-12-14 11:37:00 +08:00

README.md

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:

struct Service {
    exit: shim::ExitSignal,
}

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

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

impl shim::Task for Service {
    fn create(
        &self,
        _ctx: &TtrpcContext,
        _req: api::CreateTaskRequest,
    ) -> TtrpcResult<api::CreateTaskResponse> {
        // New task nere...
        Ok(api::CreateTaskResponse::default())
    }

    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 empty_shim
$ sudo cp ./target/debug/examples/empty_shim /usr/local/bin/containerd-shim-empty-v1
$ sudo ctr run --rm --runtime io.containerd.empty.v1 -t docker.io/library/hello-world:latest hello

Or if on 1.6+

$ cargo build --example empty_shim
$ sudo ctr run --rm --runtime ./target/debug/examples/empty_shim 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 empty_shim -- \
    -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
  • x86_64