9个版本 (破坏性更新)

0.7.1 2024年3月19日
0.7.0 2024年3月14日
0.6.0 2023年10月30日
0.5.0 2023年8月24日
0.1.1 2021年12月10日

#77 in 异步

Download history 739/week @ 2024-05-04 913/week @ 2024-05-11 1162/week @ 2024-05-18 894/week @ 2024-05-25 663/week @ 2024-06-01 844/week @ 2024-06-08 1032/week @ 2024-06-15 979/week @ 2024-06-22 903/week @ 2024-06-29 1253/week @ 2024-07-06 1007/week @ 2024-07-13 924/week @ 2024-07-20 715/week @ 2024-07-27 523/week @ 2024-08-03 798/week @ 2024-08-10 594/week @ 2024-08-17

每月2,774次下载
用于 2 crates

Apache-2.0

200KB
4K SLoC

containerd的Shim扩展

Crates.io docs.rs Crates.io CI

Rust crate,简化containerd的runtime v2 Shim实现。

它复制了containerd的Shim v2运行时实现(用Go编写)提供的相同shim.Run API。

运行时

Runtime v2引入了一个首类Shim API,以便运行时作者与containerd集成。Shim API是最小化的,并且仅限于容器的执行生命周期。

此crate简化了containerd的Shim v2运行时开发。它处理常见的任务,如命令行解析、设置Shim的TTRPC服务器、日志记录、事件等。

客户端预期实现[Shim]和[Task]特质,并具有任务处理例程。这通常复制了Go版本中的相同API。

一旦实现,Shim的引导代码就像这样简单

shim::run::<Service>("io.containerd.empty.v1")

外观和感觉

API与Go版本提供的API非常相似

use std::sync::Arc;

use async_trait::async_trait;
use containerd_shim::{
    asynchronous::{run, spawn, ExitSignal, Shim},
    publisher::RemotePublisher,
    Config, Error, Flags, StartOpts, TtrpcResult,
};
use containerd_shim_protos::{
    api, api::DeleteResponse, shim_async::Task, ttrpc::r#async::TtrpcContext,
};
use log::info;

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

#[async_trait]
impl Shim for Service {
    type T = Service;

    async fn new(_runtime_id: &str, _args: &Flags, _config: &mut Config) -> Self {
        Service {
            exit: Arc::new(ExitSignal::default()),
        }
    }

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

    async fn delete_shim(&mut self) -> Result<DeleteResponse, Error> {
        Ok(DeleteResponse::new())
    }

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

    async fn create_task_service(&self, _publisher: RemotePublisher) -> Self::T {
        self.clone()
    }
}

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

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

#[tokio::main]
async fn main() {
    run::<Service>("io.containerd.empty.v1", None).await;
}

如何与containerd一起使用

注意:所有操作均在rust-extensions的根目录中。

使用shim v2运行时

$ 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

或者在1.6+上

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

或手动

$ 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

在Windows上运行

# Run containerd in background
$env:TTRPC_ADDRESS="\\.\pipe\containerd-containerd.ttrpc"

$ cargo run --example skeleton -- -namespace default -id 1234 -address "\\.\pipe\containerd-containerd" start
\\.\pipe\containerd-shim-17630016127144989388-pipe

# (Optional) Run the log collector in a separate command window
# note: log reader won't work if containerd is connected to the named pipe, this works when running manually to help debug locally
$ cargo run --example windows-log-reader \\.\pipe\containerd-shim-default-1234-log
Reading logs from: \\.\pipe\containerd-shim-default-1234-log
<logs will appear after next command>

$ cargo run --example shim-proto-connect \\.\pipe\containerd-shim-17630016127144989388-pipe
Connecting to \\.\pipe\containerd-shim-17630016127144989388-pipe...
Sending `Connect` request...
Connect response: version: "example"
Sending `Shutdown` request...
Shutdown response: ""

支持的平台

目前,以下操作系统和硬件架构受到支持,并且需要更多努力来启用和验证其他操作系统和架构。

  • Linux
  • Mac OS
  • Windows

依赖关系

~8–24MB
~303K SLoC