Note: This was originally posted at martinheinz.dev

In the last blog post about OpenFaaS function we explored what we can do with OpenFaaS — we created our first OpenFaaS function in Golang, we wrote unit test for it, set up CI/CD with Travis and automated common tasks with Taskfile. Now, it’s time to go little deeper.

In this blog post, we will look into how we can build custom templates, optimize them, build functions from them and finally, we will also incorporate Go modules into our new template. So, let’s do it!

TL;DR: Full source code for this post, including docs can be found here: https://github.com/MartinHeinz/openfaas-templates

Deeper Look at Templates

If we want to build custom templates, then we first need to understand how they work. Let’s first look at what files the template consists of:

Template Manifest — the template.yml is a file that describes the template itself, so that OpenFaaS knows how to create and deploy functions from it. In this file you can find information such as language , required packages or fprocess

is a file that describes the template itself, so that OpenFaaS knows how to create and deploy functions from it. In this file you can find information such as , required or Entrypoint — Next, for the function to be able to work, we need some file (entrypoint) which either listens to HTTP requests or reads STDIO and forwards these request to function itself. What this entrypoint file is, depends on the language, so you can have e.g. main.go , index.py , index.js , etc.

, , , etc. Handler — Now, that we have an entrypoint, which passes requests to our function, we need to somehow handle it. For that we have handler.{go,js,py...} which does the actual work and returns response from the function

which does the actual work and returns response from the function Dependencies — Most of the time, your function will use some packages. These can be specified in language specific way, for example using package.json for JavaScript or using go.mod for Golang

for JavaScript or using for Golang Dockerfile - Finally, to package all these files into a function we have Dockerfile , which both builds, tests and creates final runner image that can be deployed to OpenFaaS.

Now that we know what the function consists of, how does it actually work? There are two versions of OpenFaaS templates — classic ( watchdog ) and new beta ( of-watchdog ) templates. Both of them operate by creating tiny Golang webserver, which marshals a HTTP request accepted on the API Gateway, which is then forwarded to invoked function. That's that for similarities, now let's look at the differences:

watchdog : Classic templates operate by forking one process per request. Once your process is forked the watchdog passes in the HTTP request via stdin and reads a HTTP response via stdout .

of-watchdog : On the other hand, the new templates fork a process when watchdog starts, we then forward any request incoming to the watchdog to a HTTP port within the container.

Images above taken from OpenFaaS Architecture Docs — https://docs.openfaas.com/architecture/watchdog/

If you want to delve deeper into how the watchdog (both old and new) works, then go to here or here

Creating Template

First of all, templates have specific file/directory structure, which we need to follow, this is how it looks:

Using the structure above — the template directory is the place where all templates reside. In it, we have one template named my-template and inside it are the files necessary for it to work - all of those we already described in previous section. This is very minimal example and we will be adding more files to that as we go.

That’s it for structure, now I think it’s finally time to create a template, let’s start with “classic watchdog style" template. We can create one by just copying one from official template store here. Considering that this is guide for Golang templates, I will take the basic go template (it's redundant to copy it here, you can see the most up-to-date version of the template here).

When we have it copied into template folder, we should check if it actually works, right? So let's build it:

Looks good, now let’s deploy the function, shall we?

And just like that, we have created our own template (well, we really just borrowed it from official store) and deployed function made from it. The whole process however, feels quite clunky, as we had to use quite a few commands to run it as well as call docker commands directly instead of e.g. faas-cli build and faas-cli push . We will simplify this process with Taskfile and few scripts in the next blog post. For the time being though, let's optimize the template, that we have...

What Could We Improve?

The classic template from previous section is nice and all, but there are quite few issues with it:

No Go modules: This template doesn’t use Golang module system introduced in 1.11, which means that impractical and outdated dependency management has to be used.

Non-optimal Dockerfile : Dockerfile for this template has quite a few unnecessary layers making it slow to build and little thicker then needed.

: for this template has quite a few unnecessary layers making it slow to build and little thicker then needed. of-watchdog is faster: As mentioned above, it's a classic template, so it doesn't use of-watchdog . Therefore we can improve it's performance by switching to of-watchdog and using HTTP mode.

So, let’s fix those!

Note: Here is a good time to mention, that OpenFaaS community already did a lot of work on these templates. Classic one is legacy template and you should be using middleware or http versions of it and possibly applying tweaks and changes presented below.

Some of those issues can be solved by just using openfaas-incubator/golang-http-template here, but we can push it a little further, so let's take the golang-http from here and build on top of that.

Here I will just show you the final Dockerfile I created using this Dockerfile as a base and I will walk you through all the changes and rationale behind them:

Note: You can view the Dockerfile in the repository here

We start with 2 FROM commands as we need both Golang image for building the function as well as watchdog binary. Next, we turn Go modules on and disable CGO, which would allow us to call C code which we don't need here. Following line installs git into the builder image as we need it to download Golang packages during build. On next 2 lines, we copy watchdog binary and make it executable. Now it's time to build our function binary - we first copy the source code to /go/src/handler , then we run 2 commands against it - test and build. For testing, we use test.sh script included in template. Let's see what it does:

Note: You can view the test.sh file in the repository here

First it collects test targets, which is our source code from function directory. Then it runs go test against these target files, if tests pass, then it's time to check formatting and look for suspicious constructs in code, that's what gofmt and go vet are for. At that point - after tests pass successfully - going back to Dockerfile - we build the binary called handler .

Final part of the Dockerfile is runner image. Here, commands that we run really start to matter. We want to reduce all calls to RUN , COPY , and ADD as those create extra layers and would bloat the final image. So, first we use single RUN command to add CA, add user called app under which the function will run and move to it's home directory. Next we COPY all the needed files from builder image to runner directory from which function will run in single layer, this includes Golang binary, watchdog binary and everything in function directory. Finally, we set our user, set environment variables and set watchdog binary as default startup command.

Now, that we have Dockerfile out of the way, we can have a look at the source code. For the sake of simplicity, I just use both main.go and handler.go from previously mentioned golang-http, as there is not much to change when it comes to template code. What is missing though, is some unit test template, so let's see what we can do about that...

Time For Testing

If you read any of my previous posts, you already know what’s coming — unit testing. The template can include any code — which includes tests, so I think it’s appropriate to add simple test template to have some starting point for testing your functions. So, here is example test:

This test lives in handler_test.go file right next to handler.go . This test covers the basic checks that one would expect when doing HTTP request/response, that being - status code check and body of response validation. So, first we create expected value using handler.Response , next we do the request to our function represented by Handle function, passing in our request, and finally we check it's response against expected values.

Bringing It All Together

We finally have all the source code needed for our template, but we can’t use it quite yet. We are missing template.yml which we need to build functions. Here it is:

Not much to talk about here. We specify language, binary that will be ran as fprocess and a simple welcome message. We could also include build_options and packages here, but in this case it is not necessary.

Next thing we need to complete the template are the dependencies and considering that we are using Go modules, all we need to do is create go.mod file alongside main.go , that looks like this

And then run go mod tidy which will populate the file with all our dependencies ( openfaas-incubator/go-function-sdk and stretchr/testify )

Only thing left to do now is to build it, push it and deploy it:

Conclusion

And just like that, we created custom template that we can use to build and deploy OpenFaaS functions. With that said, there are few things left to do… In the next blog post we will build personal template store, where we can put all our custom templates, then add automatic validation of templates and include it in CI/CD pipeline, and finally we will simplify all the tasks related to building and running templates with Taskfile like we did with functions in previous post. For a sneak peek at source code, see my repository here and feel free to leave feedback or just star the repository if you like these kinds of posts. 😉