docs/code-samples/serving/cloudevents/cloudevents-rust
kahirokunn 8e917ed012
chore: multi-arch support with buildx in all Dockerfiles (#5273)
* chore: use buildx to build grpc-ping-go

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support grpc-ping-go multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-clojure multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-dart multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-deno multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-elixir multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-haskell multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-java-micronaut multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-java-quarkus multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-r multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-rserver multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-rust multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-swift multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-vertx multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support iris-classifier multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-go multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-python multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support node-heartbeat-source multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support cloudevents-dotnet multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support cloudevents-go multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support cloudevents-nodejs multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support cloudevents-rust multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support gitwebhook-go multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-csharp multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-go multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-java-spark multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-java-spring multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-kotlin multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-nodejs multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-php multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-python multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-shell multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support knative-routing-go multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support kong-routing-go multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support multi-container multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support secrets-go multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support knative-docs multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-r Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support helloworld-go Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support cloudevents-go Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support gitwebhook-go Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support knative-routing-go Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support kong-routing-go Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support servingcontainer Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support sidecarcontainer Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support secrets-go Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

* chore: support multi-container Dockerfile multiple architecture

Signed-off-by: kahirokunn <okinakahiro@gmail.com>

---------

Signed-off-by: kahirokunn <okinakahiro@gmail.com>
2023-09-06 13:28:13 +00:00
..
src Move code samples to GitHub (#4565) 2021-12-14 04:20:51 -08:00
.gitignore Move code samples to GitHub (#4565) 2021-12-14 04:20:51 -08:00
Cargo.lock Bump futures-util in /code-samples/serving/cloudevents/cloudevents-rust (#5601) 2023-06-22 18:15:35 +00:00
Cargo.toml Move code samples to GitHub (#4565) 2021-12-14 04:20:51 -08:00
Dockerfile Move code samples to GitHub (#4565) 2021-12-14 04:20:51 -08:00
README.md chore: multi-arch support with buildx in all Dockerfiles (#5273) 2023-09-06 13:28:13 +00:00
service.yaml Move code samples to GitHub (#4565) 2021-12-14 04:20:51 -08:00

README.md

Cloud Events - Rust

A simple web app written in Rust using Actix web that can receive CloudEvents. It supports running in two modes:

  1. The default mode has the app reply to your input events with the output event, which is simplest for demonstrating things working in isolation, but is also the model for working for the Knative Eventing Broker concept. The input event is modified assigning a new source and type attribute.

  2. K_SINK mode has the app send events to the destination encoded in $K_SINK, which is useful to demonstrate how folks can synthesize events to send to a Service or Broker when not initiated by a Broker invocation (e.g. implementing an event source). The input event is modified assigning a new source and type attribute.

The application will use $K_SINK-mode whenever the environment variable is specified.

Do the following steps to create the sample code and then deploy the app to your cluster. You can also download a working copy of the sample by running the following commands:

git clone https://github.com/knative/docs.git knative-docs
cd knative-docs/code-samples/serving/cloudevents/cloudevents-rust

Before you begin

  • A Kubernetes cluster with Knative installed and DNS configured. See Install Knative Serving.
  • Docker installed and running on your local machine, and a Docker Hub account configured (we'll use it for a container registry).
  • This guide uses Rust musl toolchain to build the image in order to create really small docker images. To install the Rust toolchain: rustup. To install musl support: MUSL support for fully static binaries.

Build and deploy the sample

To build the binary, run:

cargo build --target x86_64-unknown-linux-musl --release

This will build a statically linked binary, in order to create an image from scratch. Now build the docker image:

# Build and push the container on your local machine.
docker buildx build --platform linux/arm64,linux/amd64 -t "<image>" --push .

yaml

To deploy the Knative Service, look in the service.yaml and replace <image> with the deployed image name. Then run:

kubectl apply -f service.yaml

kn

If using kn to deploy:

kn service create cloudevents-rust --image=<image>

Testing the sample

Get the URL for your Service with:

$ kubectl get ksvc
NAME                URL                                            LATESTCREATED             LATESTREADY               READY   REASON
cloudevents-rust    http://cloudevents-rust.sslip.io                 cloudevents-rust-vl8fq    cloudevents-rust-vl8fq    True

Then send a CloudEvent to it with:

$ curl \
   -X POST -v \
   -H "content-type: application/json"  \
   -H "ce-specversion: 1.0"  \
   -H "ce-source: http://curl-command"  \
   -H "ce-type: curl.demo"  \
   -H "ce-id: 123-abc"  \
   -d '{"name":"Dave"}' \
   http://cloudevents-rust.sslip.io

You can also send CloudEvents spawning a temporary curl pod in your cluster with:

$ kubectl run curl \
   --image=curlimages/curl --rm=true --restart=Never -ti -- \
   -X POST -v \
   -H "content-type: application/json"  \
   -H "ce-specversion: 1.0"  \
   -H "ce-source: http://curl-command"  \
   -H "ce-type: curl.demo"  \
   -H "ce-id: 123-abc"  \
   -d '{"name":"Dave"}' \
   http://cloudevents-rust.default.svc

You'll get as result:

> POST / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.69.1
> Accept: */*
> content-type: application/json
> ce-specversion: 1.0
> ce-source: http://curl-command
> ce-type: curl.demo
> ce-id: 123-abc
> Content-Length: 15
>
< HTTP/1.1 200 OK
< content-length: 15
< content-type: application/json
< ce-specversion: 1.0
< ce-id: 123-abc
< ce-type: dev.knative.docs.sample
< ce-source: https://github.com/knative/docs/code-samples/serving/cloudevents/cloudevents-rust
< date: Sat, 23 May 2020 09:00:01 GMT
<
{"name":"Dave"}

Removing the sample app deployment

To remove the sample app from your cluster, delete the service:

yaml

kubectl delete --filename service.yaml

kn

kn service delete cloudevents-rust