Skip to content

Commit

Permalink
Make examples compilable
Browse files Browse the repository at this point in the history
Signed-off-by: Maksym Pavlenko <[email protected]>
  • Loading branch information
mxpv committed Sep 28, 2023
1 parent 65e5ddf commit 0c034e8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 42 deletions.
14 changes: 9 additions & 5 deletions crates/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ This crate implements a GRPC client to query containerd APIs.
## Example

```rust
// Launch containerd at /run/containerd/containerd.sock
let channel = connect("/run/containerd/containerd.sock").await?;
use containerd_client::{connect, services::v1::version_client::VersionClient};

let mut client = VersionClient::new(channel);
let resp = client.version(()).await?;
async fn query_version() {
// Launch containerd at /run/containerd/containerd.sock
let channel = connect("/run/containerd/containerd.sock").await.unwrap();

println!("Response: {:?}", resp.get_ref());
let mut client = VersionClient::new(channel);
let resp = client.version(()).await.unwrap();

println!("Response: {:?}", resp.get_ref());
}
```
8 changes: 3 additions & 5 deletions crates/runc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ You can build runc client with `RuncConfig` in method chaining style.
Call `build()` or `build_async()` to get client.
Note that async client depends on [tokio](https://github.com/tokio-rs/tokio), then please use it on tokio runtime.

```rust
use runc;

```rust,ignore
#[tokio::main]
async fn main() {
let config = runc::Config::new()
let config = runc::GlobalOpts::new()
.root("./new_root")
.debug(false)
.log("/path/to/logfile.json")
Expand All @@ -45,4 +43,4 @@ async fn main() {
- delete
- Exec is **not** available in `RuncAsyncClient` now.
- Console utilites are **not** available
- see [Go version](https://github.com/containerd/go-runc/blob/main/console.go)
- see [Go version](https://github.com/containerd/go-runc/blob/main/console.go)
10 changes: 6 additions & 4 deletions crates/shim-protos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@ containerd-shim-protos = "0.4"

Basic client code looks as follows:

```rust
let client = client::Client::connect(socket_path)?;
```rust,no_run
use containerd_shim_protos as client;
let client = client::Client::connect("unix:///containerd-shim/shim.sock").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,
id: String::from("1"),
..Default::default()
};
let resp = task_client.connect(context, &req)?;
let resp = task_client.connect(context, &req).expect("Connect request failed");
```

## Example
Expand Down
67 changes: 43 additions & 24 deletions crates/shim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,65 @@ Clients are expected to implement [Shim] and [Task] traits with task handling ro
This generally replicates same API as in Go [version](https://github.com/containerd/containerd/blob/main/runtime/v2/example/cmd/main.go).

Once implemented, shim's bootstrap code is as easy as:
```rust
```text
shim::run::<Service>("io.containerd.empty.v1")
```

## Look and feel

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

```rust
```rust,no_run
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: ExitSignal,
exit: Arc<ExitSignal>,
}
impl shim::Shim for Service {
type Error = shim::Error;
#[async_trait]
impl Shim for Service {
type T = Service;
fn new(
_runtime_id: &str,
_id: &str,
_namespace: &str,
_publisher: shim::RemotePublisher,
_config: &mut shim::Config,
) -> Self {
async fn new(_runtime_id: &str, _args: &Flags, _config: &mut Config) -> Self {
Service {
exit: ExitSignal::default(),
exit: Arc::new(ExitSignal::default()),
}
}
fn start_shim(&mut self, opts: shim::StartOpts) -> Result<String, shim::Error> {
let address = shim::spawn(opts, Vec::new())?;
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)
}
fn wait(&mut self) {
self.exit.wait();
async fn delete_shim(&mut self) -> Result<DeleteResponse, Error> {
Ok(DeleteResponse::new())
}
async fn wait(&mut self) {
self.exit.wait().await;
}
fn get_task_service(&self) -> Self::T {
async fn create_task_service(&self, _publisher: RemotePublisher) -> Self::T {
self.clone()
}
}
impl shim::Task for Service {
fn connect(
#[async_trait]
impl Task for Service {
async fn connect(
&self,
_ctx: &TtrpcContext,
_req: api::ConnectRequest,
Expand All @@ -79,16 +92,22 @@ impl shim::Task for Service {
})
}
fn shutdown(&self, _ctx: &TtrpcContext, _req: api::ShutdownRequest) -> TtrpcResult<api::Empty> {
async fn shutdown(
&self,
_ctx: &TtrpcContext,
_req: api::ShutdownRequest,
) -> TtrpcResult<api::Empty> {
info!("Shutdown request");
self.exit.signal(); // Signal to shutdown shim server
self.exit.signal();
Ok(api::Empty::default())
}
}
fn main() {
shim::run::<Service>("io.containerd.empty.v1")
#[tokio::main]
async fn main() {
run::<Service>("io.containerd.empty.v1", None).await;
}
```

## How to use with containerd
Expand Down
13 changes: 9 additions & 4 deletions crates/snapshots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,19 @@ $ ctr i pull --snapshotter custom docker.io/library/hello-world:latest
Snapshotters are required to implement `Snapshotter` trait (which is very similar to containerd's
[Snapshotter](https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go) interface).

```rust
```rust,ignore
use std::collections::HashMap;
use containerd_snapshots as snapshots;
use containerd_snapshots::{api, Info, Usage};
use log::info;
#[derive(Default)]
struct Example;
#[snapshots::tonic::async_trait]
impl snapshots::Snapshotter for Example {
type Error = ();
type Error = snapshots::tonic::Status;
async fn stat(&self, key: String) -> Result<Info, Self::Error> {
info!("Stat: {}", key);
Expand All @@ -73,8 +79,7 @@ impl snapshots::Snapshotter for Example {
The library provides `snapshots::server` for convenience to wrap the implementation into a GRPC server, so it can
be used with `tonic` like this:

```rust

```rust,ignore
use snapshots::tonic::transport::Server;
Server::builder()
Expand Down

0 comments on commit 0c034e8

Please sign in to comment.