Este contenido no está disponible en el idioma seleccionado.
Chapter 9. Building and deploying functions on the cluster
Instead of building a function locally, you can build a function directly on the cluster. When using this workflow on a local development machine, you only need to work with the function source code. This is useful, for example, when you cannot install on-cluster function building tools, such as docker or Podman.
9.1. Building and deploying a function on the cluster Copiar enlaceEnlace copiado en el portapapeles!
You can use the Knative (kn) CLI to start 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.
Prerequisites
- You have installed the Red Hat OpenShift Pipelines on your cluster.
-
You have installed the OpenShift CLI (
oc). -
You have installed the Knative (
kn) CLI.
Procedure
Create a function by running the following command:
$ kn func create <function_name> -l <runtime>- Implement the business logic of your function. Then, use Git to commit and push the changes.
Deploy your function by running the following command:
$ kn func deploy --remoteIf you are not logged into the container registry referenced in your function configuration, you are prompted to give credentials for the remote container registry that hosts the function image:
You get an output similar to the following example:
🕕 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-
To update your function, commit and push new changes by using Git, then run the
kn func deploy --remotecommand again. Optional. You can configure your function to be built on the cluster after every Git push by using pipelines-as-code:
Generate the Tekton
PipelinesandPipelineRunsconfiguration for your function by running the following command:$ kn func config git setApart from generating configuration files, this command connects to the cluster and verifies the pipeline installation. By using the token, it also creates a webhook on the function repository on behalf of the user. That webhook triggers the pipeline on the cluster every time you push changes to the repository.
You need to have a valid GitHub personal access token with the repository access to use this command.
Commit and push the generated
.tekton/pipeline.yamland.tekton/pipeline-run.yamlfiles by running the following commands:$ git add .tekton/pipeline.yaml .tekton/pipeline-run.yaml $ git commit -m 'Add the Pipelines and PipelineRuns configuration' $ git push- After you make a change to your function, commit and push it. The function is rebuilt automatically by using the created pipeline.
9.2. Specifying function revision Copiar enlaceEnlace copiado en el portapapeles!
When building and deploying a function on the cluster, you must specify the location of the function code by specifying the Git repository, branch, and subdirectory within the repository. You do not need to specify the branch if you use the main branch. Similarly, you do not need to specify the subdirectory if your function is at the root of the repository. You can specify these parameters in the func.yaml configuration file, or by using flags with the kn func deploy command.
Prerequisites
- You have installed the Red Hat OpenShift Pipelines on your cluster.
-
You have installed the OpenShift (
oc) CLI. -
You have installed the Knative (
kn) CLI.
Procedure
Deploy your function:
$ kn func deploy --remote \ --git-url <repo_url> \ [--git-branch <branch>] \ [--git-dir <function_dir>]--remote-
With the
--remoteflag, the build runs remotely. git-url <repo_url>-
Substitute
<repo_url>with the URL of the Git repository. --git-branch <branch>-
Substitute
<branch>with the Git branch, tag, or commit. If using the latest commit on themainbranch, you can skip this flag. --git-dir <function_dir>-
Substitute
<function_dir>with the directory containing the function if it is different from the repository root directory.
Similar to the following example:
$ kn func deploy --remote \ --git-url https://example.com/alice/myfunc.git \ --git-branch my-feature \ --git-dir functions/example-func/
9.3. Setting custom volume size Copiar enlaceEnlace copiado en el portapapeles!
For projects that require a volume with a larger size to build, you might need to customize the persistent volume claim (PVC) when building on the cluster. The default PVC size is 256 mebibytes.
Prerequisites
- You have installed the Red Hat OpenShift Pipelines on your cluster.
-
You have installed the OpenShift (
oc) CLI. -
You have installed the Knative (
kn) CLI.
Procedure
Deploy your function with the
--pvc-sizeflag and PVC size specification by running the following command:$ kn func deploy --remote --pvc-size='2Gi'In this example, PVC is set to two gibibytes.
9.4. Testing a function in the web console Copiar enlaceEnlace copiado en el portapapeles!
You can test a deployed serverless function by invoking it in the OpenShift Container Platform web console.
Prerequisites
- You have installed the OpenShift Serverless Operator and Knative Serving on your OpenShift Container Platform cluster.
- You have logged in to the web console.
- You have created and deployed a function.
Procedure
- Navigate to Topology.
- Click a function, then click Test Serverless Function from the Actions drop-down list in the Details panel. This opens the Test Serverless Function dialog box.
In the Test Serverless Function dialog box, change the settings for your test as required:
- Choose the Format for your test. This can be either CloudEvent or HTTP.
-
The Content-Type defaults to the
Content-TypeHTTP header value. -
You can use the Advanced Settings to change the Type or Source for
CloudEventtests, or to add optional headers. - You can change the input data for the test.
- Click Test to run your test.
- After the test is complete, the Test Serverless Function dialog box displays a status code and a message that informs you whether your test was succesful.
- Click Back to perform another test, or Close to close the testing dialog box.