×

This guide explains how you can get started with creating, building, and deploying a function on an OpenShift Serverless installation. When building and deploying functions, the resulting container image is stored in an image registry.

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

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, template, and repository with the template as flags on the command line, or use the -c flag to start the interactive experience in the terminal.

Prerequisites
  • The OpenShift Serverless Operator and Knative Serving are installed on the cluster.

  • You have installed the kn CLI.

Procedure
  • Create a function project:

    $ kn func create -r <repository> -l <runtime> -t <template> <path>
    • 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
    • Alternatively, you can specify a repository that contains a custom template.

      Example command
      $ kn func create -r https://github.com/boson-project/templates/ -l node -t hello-world examplefunc
      Example output
      Project path:  /home/user/demo/examplefunc
      Function name: examplefunc
      Runtime:       node
      Template:      hello-world
      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. This command creates an OCI container image that can be run locally on your computer or on an Red Hat OpenShift Service on AWS cluster. The build command uses the function project name and the image registry name to construct a fully qualified image name for your function.

The OpenShift Container Registry is used by default as the image registry for storing function images. You can override this by using the -r flag:

Example build command
$ kn func build -r quay.io/username
Example output
Building function image
Function image has been built, image: quay.io/username/example-function:latest

To learn more about kn func build command options, you can use the help command:

Build help command
$ kn func help build

Building and deploying functions on the cluster

You can use the Knative (kn) CLI to initiate a function project build and then deploy the function directly on the cluster. To build a function project in this way, the source code for your function project must exist in a Git repository branch that is accessible to your cluster.

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

Prerequisites
  • Red Hat OpenShift Pipelines must be installed on your cluster.

  • You have installed the OpenShift (oc) CLI.

  • You have installed the Knative (kn) CLI.

Procedure
  1. In each namespace where you want to run Pipelines and deploy a function, you must create the following resources:

    1. Create the functions buildpacks Tekton task to be able to build the function image:

      $ oc apply -f https://raw.githubusercontent.com/openshift-knative/kn-plugin-func/serverless-1.22.0/pipelines/resources/tekton/task/func-buildpacks/0.1/func-buildpacks.yaml
    2. Create the kn func deploy Tekton task to be able to deploy the function in the pipeline:

      $ oc apply -f https://raw.githubusercontent.com/openshift-knative/kn-plugin-func/serverless-1.22.0/pipelines/resources/tekton/task/func-deploy/0.1/func-deploy.yaml
  2. Create a function:

    $ kn func create <function_name> -l <runtime>
  3. After you have created a new function project, you must add the project to a Git repository and ensure that the repository is available to the cluster. Information about this Git repository is used to update the func.yaml file in the next step.

  4. Update the configuration in the func.yaml file for your function project to enable on-cluster builds for the Git repository:

    ...
    build: git (1)
    git:
      url: <git_repository_url> (2)
      revision: main (3)
      contextDir: <directory_path> (4)
    ...
    1 Required. Specify git build type.
    2 Required. Specify the Git repository that contains your function’s source code.
    3 Optional. Specify the Git repository revision to be used. This can be a branch, tag or commit.
    4 Optional. Specify the function’s directory path if the function is not located in the Git repository root folder.
  5. Implement the business logic of your function. Then, use Git to commit and push the changes.

  6. Deploy your function:

    $ kn func deploy

    If you are not logged into the container registry referenced in your function configuration, you are prompted to provide credentials for the remote container registry that hosts the function image:

    Example output and prompts
    🕕 Creating Pipeline resources
    Please provide credentials for image registry used by Pipeline.
    ? Server: https://index.docker.io/v1/
    ? Username: my-repo
    ? Password: ********
       Function deployed at URL: http://test-function.default.svc.cluster.local
  7. To update your function, commit and push new changes by using Git, then run the kn func deploy command 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
  • The OpenShift Serverless Operator and Knative Serving are installed on the cluster.

  • You have installed the Knative (kn) CLI.

  • You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in Red Hat OpenShift Service on AWS.

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

Procedure
  • Deploy a function:

    $ kn func deploy [-n <namespace> -p <path> -i <image>]
    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.

Invoking a deployed function with a test event

You can use the kn func invoke CLI command to send a test request to invoke a function either locally or on your Red Hat OpenShift Service on AWS cluster. This command can be used to test that a function is working and able to receive events correctly.

Example command
$ kn func invoke

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