Simply put, the official Knative quickstart tutorial has quite a few gaps explaining things and it took me a while to piece things together. How can your app reply events to the broker?
This example deploys two services (cloudevents-player
and our custom app kn-app
) with two triggers. If you send an CloudEvent with type my-event
from cloudevents-player
, kn-app
will reply an CloudEvent with type app-event
which will in turn be sent back to cloudevents-player
.
The app is written in Golang using CloudEvents SDK, modified from the second official serving example. Both incoming and outputing events has a JSON payload with a single message
field:
{
"message": "something"
}
Install Docker and download binaries of kn, kn-quickstart and kind.
Start the demo environment:
./kn quickstart kind
docker login
docker build . -t <your docker hub name>/kn-app -f Dockerfile
docker push <your docker hub name>/kn-app
Then modify the image source in kn-app.yaml
so that it can find your image:
containers:
- image: <your docker hub name>/kn-app:latest
kubectl apply -f kn-app.yaml
./kn service list
Open the URL of CloudEvents player (for example, http://cloudevents-player.default.127.0.0.1.sslip.io
) and send an event with type my-event
. The message is a JSON object with one field message
. You should see a second event with type app-event
appear shortly.
The event with ID 42
is the one replied by the app. You can modify the eveent ID, source and message in the code.
In kn-app.yaml
the my-app
has a environment variable K_SINK
, which will tell the code where is the event target. Here we set it with the URL of example-broker, which is the built-in broker in the Knative quickstart environment:
http://broker-ingress.knative-eventing.svc.cluster.local/default/example-broker
If K_SINK
is not set (target is empty) and you send an direct CloudEvent-compatible HTTP request to the app, the code will reply an event directly as a HTTP response. This also works if you run the app as a local Docker container.