Function Build

Currently, OpenFunction builds function images with Cloud Native Buildpacks. The traditional Dockerfile build method will be supported in the future.

Build functions by adding a build section in the function definition

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/v1beta1
  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 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