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 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.
Prerequisites
- Red Hat OpenShift Pipelines must be installed on your cluster.
-
You have installed the OpenShift CLI (
oc). -
You have installed the Knative (
kn) CLI.
Procedure
Create the following resources:
Create the
s2iTekton task to be able to use Source-to-Image in the pipeline:oc apply -f https://raw.githubusercontent.com/openshift-knative/kn-plugin-func/serverless-1.29.0/pkg/pipelines/resources/tekton/task/func-s2i/0.1/func-s2i.yaml
$ oc apply -f https://raw.githubusercontent.com/openshift-knative/kn-plugin-func/serverless-1.29.0/pkg/pipelines/resources/tekton/task/func-s2i/0.1/func-s2i.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
kn funcdeploy 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.29.0/pkg/pipelines/resources/tekton/task/func-deploy/0.1/func-deploy.yaml
$ oc apply -f https://raw.githubusercontent.com/openshift-knative/kn-plugin-func/serverless-1.29.0/pkg/pipelines/resources/tekton/task/func-deploy/0.1/func-deploy.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Create a function:
kn func create <function_name> -l <runtime>
$ kn func create <function_name> -l <runtime>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Implement the business logic of your function. Then, use Git to commit and push the changes.
Deploy your function:
kn func deploy --remote
$ kn func deploy --remoteCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
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:kn func config git set
$ kn func config git setCopy to Clipboard Copied! Toggle word wrap Toggle overflow Apart from generating configuration files, this command connects to the cluster and validates that the pipeline is installed. By using the token, it also creates, on behalf of the user, a webhook on the function repository. That webhook triggers the pipeline on the cluster every time changes are pushed 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:git add .tekton/pipeline.yaml .tekton/pipeline-run.yaml git commit -m 'Add the Pipelines and PipelineRuns configuration' git push
$ git add .tekton/pipeline.yaml .tekton/pipeline-run.yaml $ git commit -m 'Add the Pipelines and PipelineRuns configuration' $ git pushCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 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
- 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
Deploy your function:
kn func deploy --remote \ --git-url <repo-url> \ [--git-branch <branch>] \ [--git-dir <function-dir>]$ kn func deploy --remote \1 --git-url <repo-url> \2 [--git-branch <branch>] \3 [--git-dir <function-dir>]4 Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- With the
--remoteflag, the build runs remotely. - 2
- Substitute
<repo-url>with the URL of the Git repository. - 3
- Substitute
<branch>with the Git branch, tag, or commit. If using the latest commit on themainbranch, you can skip this flag. - 4
- Substitute
<function-dir>with the directory containing the function if it is different than the repository root directory.
For example:
kn func deploy --remote \ --git-url https://example.com/alice/myfunc.git \ --git-branch my-feature \ --git-dir functions/example-func/$ kn func deploy --remote \ --git-url https://example.com/alice/myfunc.git \ --git-branch my-feature \ --git-dir functions/example-func/Copy to Clipboard Copied! Toggle word wrap Toggle overflow