Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support for the synchronous shim #139

Merged
merged 5 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: CI-windows
on:
pull_request:
push:
schedule:
- cron: '0 0 * * *' # Every day at midnight

jobs:
checks:
name: Checks
runs-on: ${{ matrix.os }}
timeout-minutes: 20

strategy:
matrix:
os: [windows-latest]

steps:
- uses: actions/checkout@v3
- uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- run: cargo check --examples --tests -p containerd-shim -p containerd-shim-protos

- run: rustup toolchain install nightly --component rustfmt
- run: cargo +nightly fmt -p containerd-shim -p containerd-shim-protos -- --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
env:
RUSTDOCFLAGS: -Dwarnings

tests:
name: Tests
runs-on: ${{ matrix.os }}
timeout-minutes: 15

strategy:
matrix:
os: [windows-latest]

steps:
- uses: actions/checkout@v3
- uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Tests
run: |
cargo test -p containerd-shim -p containerd-shim-protos

integration:
name: Integration
runs-on: ${{ matrix.os }}
timeout-minutes: 40

strategy:
matrix:
os: [windows-latest]
containerd: [1.7.0]

steps:
- name: Checkout extensions
uses: actions/checkout@v3

- name: Install containerd
run: |
$ErrorActionPreference = "Stop"

# Install containerd https://github.com/containerd/containerd/blob/v1.7.0/docs/getting-started.md#installing-containerd-on-windows
# Download and extract desired containerd Windows binaries
curl.exe -L https://github.com/containerd/containerd/releases/download/v${{ matrix.containerd }}/containerd-${{ matrix.containerd }}-windows-amd64.tar.gz -o containerd-windows-amd64.tar.gz
tar.exe xvf .\containerd-windows-amd64.tar.gz

# Copy and configure
mkdir "$Env:ProgramFiles\containerd"
Copy-Item -Path ".\bin\*" -Destination "$Env:ProgramFiles\containerd" -Recurse -Force
cd $Env:ProgramFiles\containerd\
.\containerd.exe config default | Out-File config.toml -Encoding ascii

# Review the configuration. Depending on setup you may want to adjust:
# - the sandbox_image (Kubernetes pause image)
# - cni bin_dir and conf_dir locations
Get-Content config.toml

# Register and start service
.\containerd.exe --register-service
Start-Service containerd
working-directory: ${{ runner.temp }}
- name: Run integration test
run: |
$ErrorActionPreference = "Stop"

get-service containerd
$env:TTRPC_ADDRESS="\\.\pipe\containerd-containerd.ttrpc"

# run the example
cargo run --example skeleton -- -namespace default -id 1234 -address "\\.\pipe\containerd-containerd" -publish-binary ./bin/containerd start
ps skeleton
cargo run --example shim-proto-connect \\.\pipe\containerd-shim-17630016127144989388-pipe
$skeleton = get-process skeleton -ErrorAction SilentlyContinue
if ($skeleton) { exit 1 }
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
log

.vscode
2 changes: 1 addition & 1 deletion crates/shim-protos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ homepage.workspace = true

[dependencies]
protobuf = "3.1"
ttrpc = "0.7"
ttrpc = "0.8.0"
async-trait = { version = "0.1.48", optional = true }

[build-dependencies]
Expand Down
13 changes: 12 additions & 1 deletion crates/shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ async = ["tokio", "containerd-shim-protos/async", "async-trait", "futures", "sig
name = "skeleton_async"
required-features = ["async"]

[[example]]
name = "windows-log-reader"
path = "examples/windows_log_reader.rs"

[dependencies]
go-flag = "0.1.0"
thiserror = "1.0"
log = { version = "0.4", features = ["std"] }
libc = "0.2.95"
nix = "0.26"
command-fds = "0.2.1"
lazy_static = "1.4.0"
time = { version = "0.3.7", features = ["serde", "std"] }
serde_json = "1.0.78"
Expand All @@ -45,5 +48,13 @@ signal-hook-tokio = { version = "0.3.1", optional = true, features = ["futures-v
[target.'cfg(target_os = "linux")'.dependencies]
cgroups-rs = "0.2.9"

[target.'cfg(unix)'.dependencies]
command-fds = "0.2.1"

[target.'cfg(windows)'.dependencies]
windows-sys = {version = "0.48.0", features = ["Win32_Foundation","Win32_System_WindowsProgramming","Win32_System_Console", "Win32_System_Pipes","Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Threading"]}
mio = { version = "0.8", features = ["os-ext", "os-poll"] }
os_pipe = "1.1.3"

[dev-dependencies]
tempfile = "3.0"
23 changes: 23 additions & 0 deletions crates/shim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,30 @@ $ cat log
[INFO] reaper thread stopped
```

### Running on Windows
```powershell
# 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: ""
```

## 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
- Windows
71 changes: 71 additions & 0 deletions crates/shim/examples/windows_log_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#[cfg(windows)]
use std::error::Error;

#[cfg(windows)]
fn main() -> Result<(), Box<dyn Error>> {
use std::{
env,
fs::OpenOptions,
os::windows::{
fs::OpenOptionsExt,
io::{FromRawHandle, IntoRawHandle},
},
time::Duration,
};

use mio::{windows::NamedPipe, Events, Interest, Poll, Token};
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OVERLAPPED;

let args: Vec<String> = env::args().collect();

let address = args
.get(1)
.ok_or("First argument must be shims address to read logs (\\\\.\\pipe\\containerd-shim-{ns}-{id}-log) ")
.unwrap();

println!("Reading logs from: {}", &address);

let mut opts = OpenOptions::new();
opts.read(true)
.write(true)
.custom_flags(FILE_FLAG_OVERLAPPED);
let file = opts.open(address).unwrap();
let mut client = unsafe { NamedPipe::from_raw_handle(file.into_raw_handle()) };

let mut stdio = std::io::stdout();
let mut poll = Poll::new().unwrap();
poll.registry()
.register(&mut client, Token(1), Interest::READABLE)
.unwrap();
let mut events = Events::with_capacity(128);
loop {
poll.poll(&mut events, Some(Duration::from_millis(10)))
.unwrap();
match std::io::copy(&mut client, &mut stdio) {
Ok(_) => break,
Err(_) => continue,
}
}

Ok(())
}

#[cfg(unix)]
fn main() {
println!("This example is only for Windows");
}
2 changes: 1 addition & 1 deletion crates/shim/src/asynchronous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ where
}
_ => {
if !config.no_setup_logger {
logger::init(flags.debug)?;
logger::init(flags.debug, &flags.namespace, &flags.id)?;
}

let publisher = RemotePublisher::new(&ttrpc_address).await?;
Expand Down
3 changes: 3 additions & 0 deletions crates/shim/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ pub enum Error {
Setup(#[from] log::SetLoggerError),

/// Unable to pass fd to child process (we rely on `command_fds` crate for this).
#[cfg(unix)]
#[error("Failed to pass socket fd to child: {0}")]
FdMap(#[from] command_fds::FdMappingCollision),

#[cfg(unix)]
#[error("Nix error: {0}")]
Nix(#[from] nix::Error),

Expand All @@ -65,6 +67,7 @@ pub enum Error {
#[error("Failed pre condition: {0}")]
FailedPreconditionError(String),

#[cfg(unix)]
#[error("{context} error: {err}")]
MountError {
context: String,
Expand Down
Loading