OpenShift Serverless Functions is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.

For more information about the support scope of Red Hat Technology Preview features, see https://access.redhat.com/support/offerings/techpreview/.

This guide explains how you can get started with creating, building, and deploying a function on an OpenShift Serverless installation.

Prerequisites

Before you can complete the following procedures, you must ensure that you have completed all of the prerequisite tasks in Setting up OpenShift Serverless Functions.

Creating functions

You can create a basic serverless function using the kn CLI.

You can specify the path, runtime, and template as flags on the command line, or use the -c flag to start the interactive experience in the terminal.

Procedure
  • Create a function project:

    $ kn func create <path> -l <runtime> -t <template>
    • Supported runtimes include node, go, python, quarkus, and typescript.

    • Supported templates include http and events.

      Example command
      $ kn func create -l typescript -t events examplefunc
      Example output
      Project path:  /home/user/demo/examplefunc
      Function name: examplefunc
      Runtime:       typescript
      Template:      events
      Writing events to /home/user/demo/examplefunc

Building functions

Before you can run a function, you must build the function project by using the kn func build command. The build command reads the func.yaml file from the function project directory to determine the image name and registry.

Example func.yaml
name: example-function
namespace: default
runtime: node
image: <image_from_registry>
imageDigest: ""
trigger: http
builder: default
builderMap:
  default: quay.io/boson/faas-nodejs-builder
envs: {}

If the image name and registry are not set in the func.yaml file, you must either specify the registry flag, -r when using the kn func build command, or you are prompted to provide a registry value in the terminal when building a function. An image name is then derived from the registry value that you have provided.

Example command using the -r registry flag
$ kn func build [-i <image> -r <registry> -p <path>]
Example output
Building function image
Function image has been built, image: quay.io/username/example-function:latest

This command creates an OCI container image that can be run locally on your computer, or on a Kubernetes cluster.

Example using the registy prompt
$ kn func build
A registry for function images is required (e.g. 'quay.io/boson').

Registry for function images: quay.io/username
Building function image
Function image has been built, image: quay.io/username/example-function:latest

The values for image and registry are persisted to the func.yaml file, so that subsequent invocations do not require the user to specify these again.

Deploying functions

You can deploy a function to your cluster as a Knative service by using the kn func deploy command.

If the targeted function is already deployed, it is updated with a new container image that is pushed to a container image registry, and the Knative service is updated.

Prerequisites
  • You must have already initialized the function that you want to deploy.

Procedure
  • Deploy a function:

    $ kn func deploy [-n <namespace> -p <path> -i <image> -r <registry>]
    Example output
    Function deployed at: http://func.example.com
    • If no namespace is specified, the function is deployed in the current namespace.

    • The function is deployed from the current directory, unless a path is specified.

    • The Knative service name is derived from the project name, and cannot be changed using this command.

Building and deploying functions with OpenShift Container Registry

When building and deploying functions, the resulting container image is stored in an image registry. Usually this will be a public registry, such as Quay. However, you can use the integrated OpenShift Container Registry instead if it has been exposed by a cluster administrator.

Procedure
  • Run the kn func build command, or the kn func deploy command, with the OpenShift Container Registry specified for the -r parameter:

    Example build command
    $ kn func build -r $(oc get route -n openshift-image-registry)
    Example deploy command
    $ kn func deploy -r $(oc get route -n openshift-image-registry)

    You can verify that the function deployed successfully by emitting a test event to it.

Emitting a test event to a deployed function

You can use the kn func emit CLI command to emit a CloudEvent to a function that is either deployed locally or deployed to your OpenShift Container Platform cluster. This command can be used to test that a function is working and able to receive events correctly.

Example command
$ kn func emit

The kn func emit command executes on the local directory by default, and assumes that this directory is a function project.