KeptnTaskDefinition

Define tasks that can be run pre- or post-deployment

A KeptnTaskDefinition defines tasks that are run by the Keptn Lifecycle Toolkit as part of the pre- and post-deployment phases of a KeptnApp or KeptnWorkload.

Yaml Synopsis

apiVersion: lifecycle.keptn.sh/v?alpha?
kind: KeptnTaskDefinition
metadata:
  name: <task-name>
spec:
  function:
    inline | httpRef | functionRef | ConfigMapRef
    parameters:
      map:
        textMessage: "This is my configuration"
    secureParameters:
      secret: slack-token

Fields

  • apiVersion – API version being used. `

  • kind – Resource type. Must be set to KeptnTaskDefinition

  • metadata

  • spec

    • function – Code to be executed, expressed as a Deno script. Refer to function runtime for more information about the runtime.

      The function can be defined as one of the following:

      • inline - Include the actual executable code to execute. This can be written as a full-fledged Deno script that is included in this file. For example:

        function:
          inline:
            code: |
                    console.log("Deployment Task has been executed");
        
      • httpRef - Specify a Deno script to be executed at runtime from the remote webserver that is specified. For example:

        name: hello-keptn-http
          spec:
              function:
                httpRef:
                  url: "https://www.example.com/yourscript.js"
        
      • functionRef – Execute one or more KeptnTaskDefinition resources that have been defined. Populate this field with the value(s) of the name field for the KeptnTaskDefinition(s) to be called. This is commonly used to call a general function that is used in multiple places, possibly with different parameters. An example is:

        spec:
          function:
            functionRef:
              name: slack-notification
        

        This can also be used to group a set of tasks into a single KeptnTaskDefinition, such as defining a KeptnTaskDefinition for testing. In this case, it calls other, existing KeptnTaskDefinitions for each type of test to be run, specifying each by the value of the name field.

      • ConfigMapRef - Specify the name of a ConfigMap resource that contains the function to be executed.

    • parameters - An optional field to supply input parameters to a function. The Lifecycle Toolkit passes the values defined inside the map field as a JSON object. For example:

        spec:
          parameters:
            map:
              textMessage: "This is my configuration"
      

      See Parameterized functions for more information.

    • secureParameters – An optional field used to pass a Kubernetes secret. The secret value is the Kubernetes secret name that is mounted into the runtime and made available to functions using the SECURE_DATA environment variable. For example:

      secureParameters:
        secret: slack-token
      

      Note that, currently, only one secret can be passed.

      See Create secret text for details.

Usage

A Task executes the TaskDefinition of a KeptnApp or [KeptnWorkload]. The execution is done by spawning a Kubernetes Job to handle a single Task. In its state, it tracks the current status of this Kubernetes Job.

The function is coded in JavaScript and executed in Deno, which is a lightweight runtime environment that executes in your namespace. Note that Deno has tighter restrictions for permissions and importing data so a script that works properly elsewhere may not function out of the box when run in Deno.

A task can be executed either pre-deployment or post-deployment as specified in the Deployment resource; see Pre- and post-deployment tasks for details. Note that the annotation identifies the task by name. This means that you can modify the function code in the resource definition and the revised code is picked up without additional changes.

Examples

Example 1: inline script

This example defines a full-fledged Deno script within the KeptnTaskDefinition YAML file:

apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
  name: hello-keptn-inline
spec:
  function:
    inline:
      code: |
        let text = Deno.env.get("DATA");
        let data;
        let name;
        data = JSON.parse(text);

        name = data.name
        console.log("Hello, " + name + " new");        

Example 2: httpRef script

This example fetches the Deno script from a remote webserver at runtime:

apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
  name: hello-keptn-http
spec:
  function:
    httpRef:
      url: "https://www.example.com/yourscript.js"

For another example, see the sample-app.

See the sample-app/version-1 PodtatoHead example for a more complete example.

Example 3: functionRef

This example calls another defined task, illustrating how one KeptnTaskDefinition can build on top of other KeptnTaskDefinitions. In this case, it calls slack-notification-dev, passing parameters and secureParameters to that other task:

apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
  name: slack-notification-dev
spec:
  function:
    functionRef:
      name: slack-notification
    parameters:
      map:
        textMessage: "This is my configuration"
    secureParameters:
      secret: slack-token

Example 4: ConfigMapRef

This example references a ConfigMap by the name of dev-configmap that contains the code for the function to be executed.

apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
  name: keptntaskdefinition-sample
spec:
  function:
    configMapRef:
      name: dev-configmap

Example 5: ConfigMap

This example illustrates the use of both a ConfigMapRef and a ConfigMap:

apiVersion: lifecycle.keptn.sh/v1alpha2
kind: KeptnTaskDefinition
metadata:
  name: scheduled-deployment
spec:
  function:
    configMapRef:
      name: scheduled-deployment-cm-1
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: scheduled-deployment-1
data:
  code: |
    let text = Deno.env.get("DATA");
    let data;
    if (text != "") {
        data = JSON.parse(text);
    }
    let targetDate = new Date(data.targetDate)
    let dateTime = new Date();
    if(targetDate < dateTime) {
        console.log("Date has passed - ok");
        Deno.exit(0);
    } else {
        console.log("It's too early - failing");
        Deno.exit(1);
    }
    console.log(targetDate);    

More examples

See the operator/config/samples directory for more example KeptnTaskDefinition YAML files.

Files

API Reference:

Differences between versions

The KeptnTaskDefinition is the same for all v1alpha? library versions.

See also

Last modified 2023-06-21: releasing documentation v0.8.0 (24f0a46)