diff --git a/crates/shim-protos/Cargo.toml b/crates/shim-protos/Cargo.toml index 0a78ef5..38cb488 100644 --- a/crates/shim-protos/Cargo.toml +++ b/crates/shim-protos/Cargo.toml @@ -9,7 +9,9 @@ description = "TTRPC bindings for containerd shim interfaces" [dependencies] protobuf = "2.23.0" -ttrpc = "0.5.1" +# Needs `Client::connect` to be published on crates.io +# PR: https://github.com/containerd/ttrpc-rust/pull/93 +ttrpc = { git = "https://github.com/containerd/ttrpc-rust.git", rev = "da67137a8b89d6b796cb7e826ba2f6db2eed8f9e" } [build-dependencies] ttrpc-codegen = "0.2" diff --git a/crates/shim-protos/README.md b/crates/shim-protos/README.md index 246f0dd..5d95143 100644 --- a/crates/shim-protos/README.md +++ b/crates/shim-protos/README.md @@ -1,3 +1,30 @@ # Shim protos TTRPC bindings for containerd's shim events and interfaces. + +## Look and feel + +Basic client code looks as follows: + +```rust +let client = client::Client::connect(socket_path)?; +let task_client = client::TaskClient::new(client); + +let context = client::ttrpc::context::with_timeout(0); + +let req = client::api::ConnectRequest { + id: pid, + ..Default::default() +}; + +let resp = task_client.connect(context, &req)?; +``` + +## Example + +Have a look on example [here](./examples/connect.rs). + +```bash +$ cargo build --example connect +$ sudo ./connect unix:///containerd-shim/shim_socket_path.sock +``` diff --git a/crates/shim-protos/examples/connect.rs b/crates/shim-protos/examples/connect.rs new file mode 100644 index 0000000..d5cb4e5 --- /dev/null +++ b/crates/shim-protos/examples/connect.rs @@ -0,0 +1,34 @@ +use std::env; + +use containerd_shim_protos as client; + +fn main() { + let args: Vec = env::args().collect(); + + let socket_path = args + .get(1) + .ok_or_else(|| "First argument must be shim socket path") + .unwrap(); + + let pid = args + .get(2) + .and_then(|str| Some(str.to_owned())) + .unwrap_or(String::new()); + + let client = client::Client::connect(socket_path).expect("Failed to connect to shim"); + + let task_client = client::TaskClient::new(client); + + let context = client::ttrpc::context::with_timeout(0); + + let req = client::api::ConnectRequest { + id: pid, + ..Default::default() + }; + + let resp = task_client + .connect(context, &req) + .expect("Connect request failed"); + + println!("Resp: {:?}", resp); +} diff --git a/crates/shim-protos/src/lib.rs b/crates/shim-protos/src/lib.rs index a3251fd..23ad413 100644 --- a/crates/shim-protos/src/lib.rs +++ b/crates/shim-protos/src/lib.rs @@ -8,3 +8,7 @@ pub use ttrpc; pub mod events; #[rustfmt::skip] pub mod shim; + +pub use shim::shim as api; +pub use shim::shim_ttrpc::{Task, TaskClient}; +pub use ttrpc::Client;