OpenWhisk Actions on Knative — Web Actions
This is a follow up blog post of How to run OpenWhisk actions on Knative?
In this blog post, we are going to experiment with OpenWhisk Web Action.
The main
function in this action returns a JSON payload with headers
, statusCode
, and body
which are passed along to the HTTP client while terminating the request/response. NodeJS runtime responds with the given status code if exists. Similarly, body is passed along as the body of the response. Content-Type
in the header
plays an important role in determining the type of body. The body is interpreted as application/json
for non-string values and text/html
otherwise in case Content-Type
is not specified in action. When the Content-Type
is defined in action body, the runtime checks if the response is binary data or plain text and decode the string using a base64
decoder if needed. An error is returned in case the body fails to decode.
Let’s try deploying this action on Knative:
Pre-requisites:
- Secret exists on Knative, check with:
$ kubectl get secret dockerhub-user-pass
- Service Account exists on Knative, check with:
$ kubectl get serviceaccount openwhisk-runtime-builder
- Build Template exists on Knative, check with:
$ kubectl get buildtemplate openwhisk-nodejs-runtime
If any of the secret/service account/build template does not exist on your Knative installation, please go back to the How to run OpenWhisk actions on Knative? blog post and run Step 1 through Step 3 depending on what’s missing.
Deploy NodeJS Runtime with Web Action
Configure the build file to point to your Docker Hub repo by replacing DOCKER_USERNAME
with your username.
Deploy NodeJS runtime with action code:
$ kubectl apply -f build.yaml
build.build.knative.dev/nodejs-10-web-action created
Verify the build pod exists:
$ kubectl get build.build.knative.dev/nodejs-10-web-action
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
nodejs-10-web-action True 1m
Serve NodeJS Runtime as a Knative Service
Configure the service template to point to the Docker Hub repo where the OpenWhisk runtime (built in previous step) will be pulled from. Replace ${DOCKER_USERNAME}
and create service.yaml
:
Run Web Action
Depending on your Knative installation, determine how to access your knative gateway and run sequence of curl
:
$ curl -H "Host: nodejs-web-action.default.example.com" -X POST http://<IP_ADDRESS>:<PORT>
{"OK":true}$ curl -H "Host: nodejs-web-action.default.example.com" -X POST http://<IP_ADDRESS>:<PORT> > image.png
This action returns a very low resolution of OpenWhisk logo which was specified in the action.
We just saw a simple web action returning an image, NodeJS runtime support many more use cases including:
Hello World Web Action
Web Action with HTTP Redirect
Web Action Setting a Cookie
Web Action Setting Multiple Cookies
All of these sample web actions are tested on OpenWhisk NodeJS Runtime.
Enjoy! 👍