Creating your own OpenFaas Connector
This post is going to be about how to create your own OpenFaas connector to add new functionality to your deployment. There are some connectors already being worked on which you can find in the docs.
Serverless is a restrictive model, you can only work within the framework provided and extending functionality is almost impossible, in most cases. OpenFaas differs in this respect as from an architecture standpoint, it is just a collection of containers talking with each other through REST. A connector can be viewed as a container running in the cluster alongside other OpenFaas components, and interacting with them.
Any long running task that waits for an external event can be abstracted away as a connector. This includes, but is not limited to, listening to messages in a queueing service, listening to events in real time database, a cron service and maybe even a websocket connection.
Function Annotations and OpenFaas Gateway
Functions deployed on OpenFaas can have annotations, which are a list of key-value pairs to assign extra data to functions. You can set function annotations by specifying them in the yaml file or giving an
--annotations flag to faas-cli. The functions can be queried through OpenFaas gateway's REST api:
GET /system/functions. It returns an output something like this:
Some function annotations have a “topic” key, which is the preferred way of telling a connector “you’ll need this function”, the connector can then invoke that function providing it the required data.
OpenFaas has a connector sdk for fetching a list of functions and invoking functions with a set topic. A dead simple connector that invokes functions every 2 seconds:
Package and deploy
Now, lets get to packaging the connector in a container. The steps involved are:
This is a fairly easy step, you’ll need to install dep and then run
dep init && dep ensure to create a vendor folder.
This will be a multi stage build Dockerfile. We’ll first compile the program at build stage and then copy the executable to alpine. Sample dockerfile looks like the following.
There isn’t much you would have to change to adapt it for your connector. Now, build it and push it to your registry:
docker build -t docker_hub_id/connector . && docker push docker_hub_id/connector
The last part involves creating yaml files for Docker Swarm and/or Kubernetes. The bare essentials that need to be provided to container are gateway url and login credentials. Thankfully, since we are deploying to the same stack in swarm or to the same namespace in kubernetes, accessing these values is easy.
If you’ve pushed the image to docker hub you only need to give a command to swarm or kubernetes to deploy your container.
docker stack deploy func -c ./docker-compose.yml kubectl create -f ./kubernetes --namespace openfaas
Deploy a test function to openfaas using the command:
faas-cli deploy --image=functions/nodeinfo --name=trigger-func --annotation topic =faas-request
In the OpenFaas portal you’ll see the function named
trigger-func and its invocation count incrementing every 2 seconds.
More examples and conclusion
OpenFaas connector pattern spawned from kafka-connector to add new functionality to existing OpenFaas deployment without changing OpenFaas source code. I dwelled into connectors so that I can build a timer trigger for OpenFaas since this functionality is not currently present. You can checkout cron connector repo and even build your own awesome connector.
Fri Jan 18 2019