-
Notifications
You must be signed in to change notification settings - Fork 49
Add WasmEdge sample #107
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
Merged
Merged
Add WasmEdge sample #107
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [build] | ||
| target="wasm32-wasi" | ||
|
|
||
| [target.wasm32-wasi] | ||
| runner = "wasmedge" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| FROM --platform=$BUILDPLATFORM rust:1.64 AS buildbase | ||
| RUN rustup target add wasm32-wasi | ||
| WORKDIR /src | ||
|
|
||
| FROM --platform=$BUILDPLATFORM buildbase AS buildserver | ||
| COPY server /src | ||
| RUN --mount=type=cache,target=/usr/local/cargo/git/db \ | ||
| --mount=type=cache,target=/usr/local/cargo/registry/cache \ | ||
| --mount=type=cache,target=/usr/local/cargo/registry/index \ | ||
| cargo build --target wasm32-wasi --release | ||
|
|
||
| FROM scratch AS server | ||
| ENTRYPOINT [ "wasmedge_hyper_server.wasm" ] | ||
| COPY --from=buildserver /src/target/wasm32-wasi/release/wasmedge_hyper_server.wasm wasmedge_hyper_server.wasm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Sample Function WasmEdge ( written in Rust ) | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| ### OpenFunction | ||
|
|
||
| You can refer to the [Installation Guide](https://github.com/OpenFunction/OpenFunction#install-openfunction) to setup OpenFunction. | ||
|
|
||
| ## Run it locally | ||
|
|
||
| 1. Build | ||
|
|
||
| ```bash | ||
| cargo build --target wasm32-wasi --release | ||
| ``` | ||
|
|
||
| 2. Run | ||
|
|
||
| ```bash | ||
| wasmedge target/wasm32-wasi/release/wasmedge_hyper_server.wasm | ||
| ``` | ||
|
|
||
| 3. Test | ||
|
|
||
| Run the following from another terminal. | ||
|
|
||
| ```bash | ||
| $ curl http://localhost:8080/echo -X POST -d "WasmEdge" | ||
| WasmEdge | ||
| ``` | ||
|
|
||
| ## Deployment | ||
|
|
||
| > To setup `WasmEdge` workload runtime in kubernetes cluster and push images to a container registry, | ||
| > please refer to the [prerequisites](../../getting-started/Quickstarts/prerequisites) section for more info. | ||
|
|
||
| 1. Create function | ||
|
|
||
| ```shell | ||
| kubectl apply -f wasmedge-http-server.yaml | ||
| ``` | ||
|
|
||
| 2. Check the function status | ||
|
|
||
| ```shell | ||
| kubectl get functions.core.openfunction.io -w | ||
| NAME BUILDSTATE SERVINGSTATE BUILDER SERVING ADDRESS AGE | ||
| wasmedge-http-server Succeeded Running builder-4p2qq serving-lrd8c http://wasmedge-http-server.default.svc.cluster.local/echo 12m | ||
| ``` | ||
|
|
||
| 3. Access function | ||
|
|
||
| Once the `BUILDSTATE` becomes `Succeeded` and the `SERVINGSTATE` becomes `Running`, you can access this function through the address in the `ADDRESS` field: | ||
| You can observe the process of a function with the following command: | ||
|
|
||
| ```shell | ||
| kubectl run curl --image=radial/busyboxplus:curl -i --tty | ||
| curl http://wasmedge-http-server.default.svc.cluster.local/echo -X POST -d "WasmEdge" | ||
| WasmEdge | ||
| ``` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can copy the doc on openfunction.dev here or add a link to the openfunction.dev doc because they're duplicated content and we'd better maintain one single copy of it |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [package] | ||
| name = "wasmedge_hyper_server" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| hyper_wasi = { version = "0.15", features = ["full"]} | ||
| tokio_wasi = { version = "1.21", features = ["rt", "macros", "net", "time", "io-util"]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| use std::net::SocketAddr; | ||
|
|
||
| use hyper::server::conn::Http; | ||
| use hyper::service::service_fn; | ||
| use hyper::{Body, Method, Request, Response, StatusCode}; | ||
| use tokio::net::TcpListener; | ||
|
|
||
| /// This is our service handler. It receives a Request, routes on its | ||
| /// path, and returns a Future of a Response. | ||
| async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> { | ||
| match (req.method(), req.uri().path()) { | ||
| // Serve some instructions at / | ||
| (&Method::GET, "/") => Ok(Response::new(Body::from( | ||
| "Try POSTing data to /echo such as: `curl localhost:8080/echo -XPOST -d 'hello world'`", | ||
| ))), | ||
|
|
||
| // Simply echo the body back to the client. | ||
| (&Method::POST, "/echo") => Ok(Response::new(req.into_body())), | ||
|
|
||
| (&Method::POST, "/echo/reversed") => { | ||
| let whole_body = hyper::body::to_bytes(req.into_body()).await?; | ||
|
|
||
| let reversed_body = whole_body.iter().rev().cloned().collect::<Vec<u8>>(); | ||
| Ok(Response::new(Body::from(reversed_body))) | ||
| } | ||
|
|
||
| // Return the 404 Not Found for other routes. | ||
| _ => { | ||
| let mut not_found = Response::default(); | ||
| *not_found.status_mut() = StatusCode::NOT_FOUND; | ||
| Ok(not_found) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[tokio::main(flavor = "current_thread")] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
| let addr = SocketAddr::from(([0, 0, 0, 0], 8080)); | ||
|
|
||
| let listener = TcpListener::bind(addr).await?; | ||
| println!("Listening on http://{}", addr); | ||
| loop { | ||
| let (stream, _) = listener.accept().await?; | ||
|
|
||
| tokio::task::spawn(async move { | ||
| if let Err(err) = Http::new().serve_connection(stream, service_fn(echo)).await { | ||
| println!("Error serving connection: {:?}", err); | ||
| } | ||
| }); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
functions/knative/wasmedge/http-server/wasmedge-http-server.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| apiVersion: core.openfunction.io/v1beta1 | ||
| kind: Function | ||
| metadata: | ||
| name: wasmedge-http-server | ||
| spec: | ||
| workloadRuntime: wasmedge | ||
| image: openfunctiondev/wasmedge_http_server:0.1.0 | ||
| imageCredentials: | ||
| name: push-secret | ||
| build: | ||
| dockerfile: Dockerfile | ||
| srcRepo: | ||
| revision: main | ||
| sourceSubPath: functions/knative/wasmedge/http-server | ||
| url: https://github.com/OpenFunction/samples | ||
| port: 8080 | ||
| route: | ||
| rules: | ||
| - matches: | ||
| - path: | ||
| type: PathPrefix | ||
| value: /echo | ||
| serving: | ||
| runtime: knative | ||
| scaleOptions: | ||
| minReplicas: 0 | ||
| template: | ||
| containers: | ||
| - command: | ||
| - /wasmedge_hyper_server.wasm | ||
| imagePullPolicy: IfNotPresent | ||
| livenessProbe: | ||
| initialDelaySeconds: 3 | ||
| periodSeconds: 30 | ||
| tcpSocket: | ||
| port: 8080 | ||
| name: function |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about including the steps to setup crun in k8s cluster?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll write a guide in https://openfunction.dev](https://openfunction.dev/