Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

Chapter 5. Knative Functions CLI commands


5.1. kn functions commands

You can use the following commands to list, create, and manage Knative functions.

5.1.1. Creating a function by using the Knative CLI

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

Prerequisites

  • You have installed the OpenShift Serverless Operator and Knative Serving on the cluster.
  • You have installed the Knative (kn) CLI.

Procedure

  1. Create a function project:

    $ kn func create -r <repository> -l <runtime> -t <template> <path>
    • Accepted runtime values include quarkus, node, typescript, go, python, springboot, and rust.
    • Accepted template values include http and cloudevents.

      You get an output similar to the following example:

      $ kn func create -l typescript -t cloudevents examplefunc

      You get an output similar to the following example:

      Created typescript function in /home/user/demo/examplefunc
  2. Specify a repository that has a custom template.

    You get an output similar to the following example:

    $ $ kn func create -r <templates_repository> -l node -t hello-world examplefunc

    Replace <templates_repository> with the repository that has the function templates.

    You get an output similar to the following example:

    Created node function in /home/user/demo/examplefunc

5.1.2. Running a function locally

You can run a function locally by using the kn func run command in the current directory or by specifying a directory with the --path flag.

If you run the function for the first time, or if you change the project files, the kn func run command builds the function before running it.

You can run a function in the current directory by using the following command:

$ kn func run

You can run a function in a specific directory by specifying the path with the --path flag:

$ kn func run --path=<directory_path>

You can also force a rebuild of an existing image before running the function, even if there have been no changes to the project files, by using the --build flag:

You can run a function and trigger a build by using the --build flag:

$ kn func run --build

If you set the build flag as false, this disables building of the image, and runs the function by using the previously built image:

You can run a function without building it by using the --build=false flag:

$ kn func run --build=false

You can use the help command to learn more about kn func run command options:

$ kn func help run

5.1.3. Building a function

Before you can run a function, you must build the function project. When you run the kn func run command, the system builds the function automatically. However, you can use the kn func build command to build a function without running it, which can be useful for advanced users or debugging scenarios.

You can run the kn func build command to create an OCI container image that you can run locally on your computer or on an OpenShift Container Platform cluster. This command uses the function project name and the image registry name to construct a fully qualified image name for your function.

5.1.3.1. Image container types

By default, kn func build creates a container image by using Red Hat Source-to-Image (S2I) technology.

You can build a function by using Red Hat Source-to-Image (S2I):

$ kn func build

5.1.3.2. Image registry types

By default, the system stores function images in the OpenShift Container Registry.

You can build a function by using the OpenShift Container Registry:

$ kn func build

You get an output similar to the following example:

Building function image
Function image has been built, image: registry.redhat.io/example/example-function:latest

You can override using OpenShift Container Registry as the default image registry by using the --registry flag:

$ kn func build --registry quay.io/username

You get an output similar to the following example:

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

5.1.3.3. Push flag

You can add the --push flag to a kn func build command to automatically push the function image after it is successfully built:

$ kn func build --push

5.1.3.4. Help command

You can use the help command to learn more about kn func build command options:

$ kn func help build

5.1.4. Deploying a function

You can deploy a function to your cluster as a Knative service by using the kn func deploy command. If you have already deployed the function, the command pushes a new container image to a container image registry and updates the Knative service.

Prerequisites

  • You have installed the OpenShift Serverless Operator and Knative Serving 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 OpenShift Container Platform.
  • You must have already created and initialized the function that you want to deploy.

Procedure

  • Deploy a function by running the following command:

    $ kn func deploy [-n <namespace> -p <path> -i <image>]

    You get an output similar to the following example:

    Function deployed at: http://func.example.com
    • If you do not specify a namespace, the command deploys the function in the current namespace.
    • The command deploys the function from the current directory unless you specify a path.
    • The command derives the Knative service name from the project name. You cannot change the service name by using this command.
Note

You can create a serverless function with a Git repository URL by using Import from Git or Create Serverless Function in the +Add view.

5.1.5. Listing existing functions

You can list existing functions by running kn func list command. To list functions that you deployed as Knative services, run kn service list command.

Procedure

  • List existing functions by running the following command:

    $ kn func list [-n <namespace> -p <path>]

    You get an output similar to the following example:

    NAME           NAMESPACE  RUNTIME  URL                                                                                      READY
    example-function  default    node     http://example-function.default.apps.ci-ln-g9f36hb-d5d6b.origin-ci-int-aws.dev.rhcloud.com  True
  • List functions deployed as Knative services by running the following command:

    $ kn service list -n <namespace>

    You get an output similar to the following example:

    NAME            URL                                                                                       LATEST                AGE   CONDITIONS   READY   REASON
    example-function   http://example-function.default.apps.ci-ln-g9f36hb-d5d6b.origin-ci-int-aws.dev.rhcloud.com   example-function-gzl4c   16m   3 OK / 3     True

5.1.6. Describing a function

The kn func info command prints information about a deployed function, such as the function name, image, namespace, Knative service information, route information, and event subscriptions.

Procedure

  • Describe a function by running the following command:

    $ kn func info [-f <format> -n <namespace> -p <path>]

    You can run the following example command to display information about a function:

    $ kn func info -p function/example-function

    You get an output similar to the following example:

    Function name:
      example-function
    Function is built in image:
      docker.io/user/example-function:latest
    Function is deployed as Knative Service:
      example-function
    Function is deployed in namespace:
      default
    Routes:
      http://example-function.default.apps.ci-ln-g9f36hb-d5d6b.origin-ci-int-aws.dev.rhcloud.com

5.1.7. 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 OpenShift Container Platform cluster. You can use this command to test that a function is working and able to receive events correctly. Invoking a function locally is useful for a quick test during function development. Invoking a function on the cluster is useful for testing that is closer to the production environment.

Prerequisites

  • You have installed the OpenShift Serverless Operator and Knative Serving 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 OpenShift Container Platform.
  • You must have already deployed the function that you want to call.

Procedure

  • Start a function by running the following command:

    $ kn func invoke
    • The kn func invoke command only works when there is either a local container image currently running, or when there is a function deployed in the cluster.
    • The kn func invoke command executes on the local directory by default, and assumes that this directory is a function project.

5.1.7.1. kn func invoke optional parameters

You can specify optional parameters for the request by using the following kn func invoke CLI command flags.

Expand
FlagsDescription

-t, --target

Specifies the target instance of the invoked function, for example, local or remote or https://staging.example.com/. The default target is local.

-f, --format

Specifies the format of the message, for example, cloudevent or http.

--id

Specifies a unique string identifier for the request.

-n, --namespace

Specifies the namespace on the cluster.

--source

Specifies sender name for the request. This corresponds to the CloudEvent source attribute.

--type

Specifies the type of request, for example, boson.fn. This corresponds to the CloudEvent type attribute.

--data

Specifies content for the request. For CloudEvent requests, this is the CloudEvent data attribute.

--file

Specifies path to a local file containing data to be sent.

--content-type

Specifies the MIME content type for the request.

-p, --path

Specifies path to the project directory.

-c, --confirm

Enables prompting to interactively confirm all options.

-v, --verbose

Enables printing verbose output.

-h, --help

Prints information on usage of kn func invoke.

5.1.7.1.1. Main parameters

The following parameters define the main properties of the kn func invoke command:

Event target (-t, --target)
The target instance of the invoked function. Accepts the local value for a locally deployed function, the remote value for a remotely deployed function, or a URL for a function deployed to an arbitrary endpoint. If a target is not specified, it defaults to local.
Event message format (-f, --format)
The message format for the event, such as http or cloudevent. This defaults to the format of the template that was used when creating the function.
Event type (--type)
The type of event that is sent. You can find information about the type parameter that is set in the documentation for each event producer. For example, the API server source might set the type parameter of produced events as dev.knative.apiserver.resource.update.
Event source (--source)
The unique event source that produced the event. This might be a URI for the event source, for example https://10.96.0.1/, or the name of the event source.
Event ID (--id)
A random, unique ID that is created by the event producer.
Event data (--data)

Allows you to specify a data value for the event sent by the kn func invoke command. For example, you can specify a --data value such as "Hello World" so that the event contains this data string. By default, no data is included in the events created by kn func invoke.

Note

Functions that have been deployed to a cluster can respond to events from an existing event source that provides values for properties such as source and type. These events often have a data value in JSON format, which captures the domain specific context of the event. By using the CLI flags noted in this document, developers can simulate those events for local testing.

You can also send event data by using the --file flag to provide a local file containing data for the event. In this case, specify the content type by using --content-type.

Data content type (--content-type)
If you are using the --data flag to add data for events, you can use the --content-type flag to specify what type of data is carried by the event. In the previous example, the data is plain text, so you might specify kn func invoke --data "Hello world!" --content-type "text/plain".
5.1.7.1.2. Example commands

This is the general invocation of the kn func invoke command:

$ kn func invoke --type <event_type> --source <event_source> --data <event_data> --content-type <content_type> --id <event_ID> --format <format> --namespace <namespace>

For example, to send a "Hello world!" event, you can run:

$ kn func invoke --type ping --source example-ping --data "Hello world!" --content-type "text/plain" --id example-ID --format http --namespace my-ns
5.1.7.1.2.1. Specifying the file with data

To specify the file on disk that contains the event data, use the --file and --content-type flags:

$ kn func invoke --file <path> --content-type <content-type>

For example, to send JSON data stored in the test.json file, use this command:

$ kn func invoke --file ./test.json --content-type application/json
5.1.7.1.2.2. Specifying the function project

You can specify a path to the function project by using the --path flag:

$ kn func invoke --path <path_to_function>

For example, to use the function project located in the ./example/example-function directory, use this command:

$ kn func invoke --path ./example/example-function
5.1.7.1.2.3. Specifying where the target function is deployed

By default, kn func invoke targets the local deployment of the function:

$ kn func invoke

To use a different deployment, use the --target flag:

$ kn func invoke --target <target>

For example, to use the function deployed on the cluster, use the --target remote flag:

$ kn func invoke --target remote

To use the function deployed at an arbitrary URL, use the --target <URL> flag:

$ kn func invoke --target "https://my-event-broker.example.com"

You can explicitly target the local deployment. In this case, if the function is not running locally, the command fails:

$ kn func invoke --target local

5.1.8. Deleting a function

You can delete a function by using the kn func delete command. This is useful when a function is no longer required, and can help to save resources on your cluster.

Procedure

  • Delete a function by running the following command:

    $ kn func delete [<function_name> -n <namespace> -p <path>]
    • If you do not specify the name or path of the function to delete, the system searches the current directory for a func.yaml file and uses it to decide which function to delete.
    • If the namespace is not specified, it defaults to the namespace value in the func.yaml file.
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2026 Red Hat
Nach oben