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

Include cli implementations for adapter components #723

Merged
merged 2 commits into from
Nov 22, 2024
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
Binary file not shown.
7 changes: 6 additions & 1 deletion crates/containerd-shim-wasmtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,21 @@ where
// This is a adapter logic that converts wasip1 `_start` function to wasip2 `run` function.
let status = match target {
ComponentTarget::HttpProxy => {
log::info!("Found HTTP proxy target");
let mut linker = component::Linker::new(&self.engine);
wasmtime_wasi_http::add_to_linker_async(&mut linker)?;
wasmtime_wasi::add_to_linker_async(&mut linker)?;
wasmtime_wasi_http::add_only_http_to_linker_async(&mut linker)?;
jprendes marked this conversation as resolved.
Show resolved Hide resolved

let pre = linker.instantiate_pre(&component)?;
log::info!("pre-instantiate_pre");
let instance = ProxyPre::new(pre)?;

log::info!("starting HTTP server");
let cancel = self.cancel.clone();
serve_conn(ctx, instance, cancel).await
}
ComponentTarget::Command => {
log::info!("Found command target");
let wasi_ctx = WasiPreview2Ctx::new(ctx)?;
let (mut store, linker) = store_for_context(&self.engine, wasi_ctx)?;

Expand All @@ -273,6 +277,7 @@ where
})
}
ComponentTarget::Core(func) => {
log::info!("Found Core target");
let wasi_ctx = WasiPreview2Ctx::new(ctx)?;
let (mut store, linker) = store_for_context(&self.engine, wasi_ctx)?;

Expand Down
39 changes: 36 additions & 3 deletions crates/containerd-shim-wasmtime/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,35 @@ fn test_wasip2_component_http_proxy() -> anyhow::Result<()> {
Ok(())
}

// The wasm component is built using componentize-dotnet as illustrated in the following example::
// https://bytecodealliance.org/articles/simplifying-components-for-dotnet-developers-with-componentize-dotnet
// this ensures we are able to use wasm built from other languages https://github.com/containerd/runwasi/pull/723
#[test]
#[serial]
fn test_wasip2_component_http_proxy_csharp() -> anyhow::Result<()> {
let srv = WasiTest::<WasiInstance>::builder()?
.with_wasm(HELLO_WASI_HTTP_CSHARP)?
.with_host_network()
.build()?;

let srv = srv.start()?;

// dotnet takes a bit longer to start up
// Todo: find out why this doesn't happen in wasmtime directly
let response = http_get_with_backoff_secs(2);

let response = response.expect("Server did not start in time");
assert!(response.status().is_success());

let body = response.text().unwrap();
assert_eq!(body, "Hello, from C#!");

let (exit_code, _, _) = srv.ctrl_c()?.wait(Duration::from_secs(5))?;
assert_eq!(exit_code, 0);

Ok(())
}

// Test that the shim can terminate component targeting wasi:http/proxy by sending SIGTERM.
#[test]
#[serial]
Expand All @@ -308,10 +337,14 @@ fn test_wasip2_component_http_proxy_force_shutdown() -> anyhow::Result<()> {
Ok(())
}

// Helper method to make a `GET` request
fn http_get() -> reqwest::Result<reqwest::blocking::Response> {
http_get_with_backoff_secs(1)
}

// Helper method to make a `GET` request
fn http_get_with_backoff_secs(backoff: u64) -> reqwest::Result<reqwest::blocking::Response> {
const MAX_ATTEMPTS: u32 = 10;
const BACKOFF_DURATION: Duration = Duration::from_secs(1);
let backoff_duration: Duration = Duration::from_secs(backoff);

let mut attempts = 0;

Expand All @@ -320,7 +353,7 @@ fn http_get() -> reqwest::Result<reqwest::blocking::Response> {
Ok(resp) => break Ok(resp),
Err(err) if attempts == MAX_ATTEMPTS => break Err(err),
Err(_) => {
std::thread::sleep(BACKOFF_DURATION);
std::thread::sleep(backoff_duration);
attempts += 1;
}
}
Expand Down
Loading