Skip to content

Commit

Permalink
Add support for windows to containerd client
Browse files Browse the repository at this point in the history
Adds support to containerd client and enables running the examples

Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant authored and mxpv committed Apr 24, 2024
1 parent c696ce4 commit c836f13
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: ./scripts/install-protobuf.sh
- run: cargo check --examples --tests -p containerd-shim -p containerd-shim-protos
- run: cargo check --examples --tests -p containerd-shim -p containerd-shim-protos -p containerd-client

- run: rustup toolchain install nightly --component rustfmt
- run: cargo +nightly fmt -p containerd-shim -p containerd-shim-protos -- --check --files-with-diff
- run: cargo +nightly fmt -p containerd-shim -p containerd-shim-protos -p containerd-client -- --check --files-with-diff

- run: cargo clippy -p containerd-shim -p containerd-shim-protos -- -D warnings
- run: cargo doc --no-deps -p containerd-shim -p containerd-shim-protos
- run: cargo doc --no-deps -p containerd-shim -p containerd-shim-protos -p containerd-client
env:
RUSTDOCFLAGS: -Dwarnings

Expand Down Expand Up @@ -80,7 +80,7 @@ jobs:
env:
# runc::tests::test_exec needs $XDG_RUNTIME_DIR to be set
XDG_RUNTIME_DIR: /tmp/dummy-xdr
- run: cargo test -p containerd-shim -p containerd-shim-protos
- run: cargo test -p containerd-shim -p containerd-shim-protos -p containerd-client
if: ${{ contains(matrix.os, 'windows') }}

# Collect build timings
Expand Down Expand Up @@ -219,6 +219,12 @@ jobs:
cargo run -p containerd-shim-protos --example shim-proto-connect \\.\pipe\containerd-shim-17630016127144989388-pipe
$skeleton = get-process skeleton -ErrorAction SilentlyContinue
if ($skeleton) { exit 1 }
- name: Run client
run: |
$ErrorActionPreference = "Stop"
get-service containerd
cargo run -p containerd-client --example version
# Currently Github actions UI supports no masks to mark matrix jobs as required to pass status checks.
# This means that every time version of Go, containerd, or OS is changed, a corresponding job should
Expand Down
3 changes: 3 additions & 0 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ tower = { workspace = true, optional = true }
[build-dependencies]
tonic-build.workspace = true

[dev-dependencies]
tokio = { workspace = true, features = ["rt", "macros"]}

[features]
connect = ["tokio", "tower"]
docs = []
Expand Down
2 changes: 2 additions & 0 deletions crates/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This crate implements a GRPC client to query containerd APIs.

## Example

Run with `cargo run --example version`

```rust
use containerd_client::{connect, services::v1::version_client::VersionClient};

Expand Down
10 changes: 7 additions & 3 deletions crates/client/examples/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ use containerd_client::Client;
/// Make sure you run containerd before running this example.
#[tokio::main(flavor = "current_thread")]
async fn main() {
let client = Client::from_path("/var/run/containerd/containerd.sock")
.await
.expect("Connect failed");
#[cfg(unix)]
let path = "/var/run/containerd/containerd.sock";

#[cfg(windows)]
let path = r"\\.\pipe\containerd-containerd";

let client = Client::from_path(path).await.expect("Connect failed");

let resp = client
.version()
Expand Down
14 changes: 12 additions & 2 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ pub mod events {
pub async fn connect(
path: impl AsRef<std::path::Path>,
) -> Result<tonic::transport::Channel, tonic::transport::Error> {
use tokio::net::UnixStream;
use tonic::transport::Endpoint;

let path = path.as_ref().to_path_buf();
Expand All @@ -91,7 +90,18 @@ pub async fn connect(
let channel = Endpoint::try_from("https://[::]")
.unwrap()
.connect_with_connector(tower::service_fn(move |_| {
UnixStream::connect(path.clone())
#[cfg(unix)]
{
tokio::net::UnixStream::connect(path.clone())
}

#[cfg(windows)]
{
let client = tokio::net::windows::named_pipe::ClientOptions::new()
.open(path.clone())
.map_err(|e| std::io::Error::from(e));
async move { client }
}
}))
.await?;

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.71"
components = ["rustfmt", "clippy", "llvm-tools-preview"]
components = ["rustfmt", "clippy", "llvm-tools"]

0 comments on commit c836f13

Please sign in to comment.