Function Build

Currently, OpenFunction supports building function images using Cloud Native Buildpacks without the need to create a Dockerfile.

In the meantime, you can also use OpenFunction to build Serverless Applications with Dockerfile.

Build functions by defining a build section

You can build your functions or applications from the source code in a git repo or from the source code stored locally.

Build functions from source code in a git repo

You can build a function image by simply adding a build section in the Function definition like below. If there is a serving section defined as well, the function will be launched as soon as the build completes.

  1. apiVersion: core.openfunction.io/v1beta2
  2. kind: Function
  3. metadata:
  4. name: logs-async-handler
  5. spec:
  6. version: "v2.0.0"
  7. image: openfunctiondev/logs-async-handler:v1
  8. imageCredentials:
  9. name: push-secret
  10. build:
  11. builder: openfunction/builder-go:latest
  12. env:
  13. FUNC_NAME: "LogsHandler"
  14. FUNC_CLEAR_SOURCE: "true"
  15. ## Customize functions framework version, valid for functions-framework-go for now
  16. ## Usually you needn't to do so because the builder will ship with the latest functions-framework
  17. # FUNC_FRAMEWORK_VERSION: "v0.4.0"
  18. ## Use FUNC_GOPROXY to set the goproxy
  19. # FUNC_GOPROXY: "https://goproxy.cn"
  20. srcRepo:
  21. url: "https://github.com/OpenFunction/samples.git"
  22. sourceSubPath: "functions/async/logs-handler-function/"
  23. revision: "main"

To push the function image to a container registry, you have to create a secret containing the registry’s credential and add the secret to imageCredentials. You can refer to the prerequisites for more info.

Build functions from local source code

To build functions or applications from local source code, you’ll need to package your local source code into a container image and push this image to a container registry.

Suppose your source code is in the samples directory, you can use the following Dockerfile to build a source code bundle image.

  1. FROM scratch
  2. WORKDIR /
  3. COPY samples samples/

Then you can build the source code bundle image like this:

  1. docker build -t <your registry name>/sample-source-code:latest -f </path/to/the/dockerfile> .
  2. docker push <your registry name>/sample-source-code:latest

It’s recommended to use the empty image scratch as the base image to build the source code bundle image, a non-empty base image may cause the source code copy to fail.

Unlike defining the spec.build.srcRepo.url field for the git repo method, you’ll need to define the spec.build.srcRepo.bundleContainer.image field instead.

  1. apiVersion: core.openfunction.io/v1beta2
  2. kind: Function
  3. metadata:
  4. name: logs-async-handler
  5. spec:
  6. build:
  7. srcRepo:
  8. bundleContainer:
  9. image: openfunctiondev/sample-source-code:latest
  10. sourceSubPath: "/samples/functions/async/logs-handler-function/"

The sourceSubPath is the absolute path of the source code in the source code bundle image.

Build functions with the pack CLI

Usually it’s necessary to build function images directly from local source code especially for debug purpose or for offline environment. You can use the pack CLI for this.

Pack is a tool maintained by the Cloud Native Buildpacks project to support the use of buildpacks. It enables the following functionality:

  • Build an application using buildpacks.
  • Rebase application images created using buildpacks.
  • Creation of various components used within the ecosystem.

Follow the instructions here to install the pack CLI tool. You can find more details on how to use the pack CLI here.

To build OpenFunction function images from source code locally, you can follow the steps below:

Download function samples

  1. git clone https://github.com/OpenFunction/samples.git
  2. cd samples/functions/knative/hello-world-go

Build the function image with the pack CLI

  1. pack build func-helloworld-go --builder openfunction/builder-go:v2.4.0-1.17 --env FUNC_NAME="HelloWorld" --env FUNC_CLEAR_SOURCE=true

Launch the function image locally

  1. docker run --rm --env="FUNC_CONTEXT={\"name\":\"HelloWorld\",\"version\":\"v1.0.0\",\"port\":\"8080\",\"runtime\":\"Knative\"}" --env="CONTEXT_MODE=self-host" --name func-helloworld-go -p 8080:8080 func-helloworld-go

Visit the function

  1. curl http://localhost:8080

Output example:

  1. hello, world!

OpenFunction Builders

To build a function image with Cloud Native Buildpacks, a builder image is needed.

Here you can find builders for popular languages maintained by the OpenFunction community:

Builders
Goopenfunction/builder-go:v2.4.0 (openfunction/builder-go:latest)
Nodejsopenfunction/builder-node:v2-16.15 (openfunction/builder-node:latest)
Javaopenfunction/builder-java:v2-11, openfunction/builder-java:v2-16, openfunction/builder-java:v2-17, openfunction/builder-java:v2-18
Pythonopenfunction/gcp-builder:v1
DotNetopenfunction/gcp-builder:v1