KOP Environment Manager - Skip Condition - Rafay Product Documentation

Skip Condition

Skip Condition: Controlling Task Execution Dynamically

The skip condition feature allows users to selectively execute tasks or hooks based on specific runtime conditions. By defining a condition script in both Resource Templates and Environment Templates, users can ensure that tasks like notifications, approvals, or workflow handlers are executed only when necessary, improving efficiency and reducing redundant executions.

Did you know?

Skip conditions can be managed using three (3) methods:

Key Capabilities

Managing Hooks with Skip Conditions in Environment and Resource Templates

Scenario

Environment and resource templates are used to automate the deployment and deprovisioning of cloud environments. Hooks are configured to run during the publish workflow but must either be skipped or conditionally bypassed during the destroy workflow to prevent unnecessary operations.

Requirement

Implementation

Skip on Destroy Enabled

Skip Conditions Applied

Outcome

By configuring Skip on Destroy and conditional skipping, workflow automation is optimized by reducing redundant operations while ensuring critical actions execute only when necessary.

Skip Conditions via UI

When defining a skip condition in a hook, a script can be used to dynamically determine whether the hook should execute. The script in the image follows this structure:

## script begin ##
def eval(**ctx):
    return {"skip": ctx["trigger"]["payload"]["action"] == "destroy", "reason": "skip if action is destroy"}
## script end ##

Function Definition (def eval(**ctx):)

Use Case Explanation

This method allows precise control over hook execution, ensuring that workflows only trigger when necessary while allowing conditional skipping based on workflow actions.

Skip Condition via RCTL

Example 1: Skip Condition in a Resource Template

The following example demonstrates how to configure a skip condition in a ResourceTemplate to ensure that a task (task1) is skipped when the action is deploy and triggered by an SSO user.

apiVersion: eaas.envmgmt.io/v1
kind: ResourceTemplate
metadata:
  name: demo-template
  project: defaultproject
spec:
  agents:
  - name: scale-agent1
    provider: custom
    providerOptions:
      custom:
        tasks:
        - agents:
          - name: demoagent
          name: task1
          onFailure: unspecified
          options: {}
          skipConfig:
            condition: |-
              ## script begin ##
              def eval(**ctx):
                 return {
                     "skip": ctx["trigger"]["payload"]["action"] == "deploy" and ctx["trigger"]["payload"]["is_sso_user"] == True,
                     "reason": "Skipping this task during deploy when triggered by an SSO user"
                 }
              ## script end ##
          type: workflowHandler
          workflowHandler:
            name: demoreturnresp
        - agents:
          - name: demoagent
          name: task2
          onFailure: unspecified
          options: {}
          type: workflowHandler
          workflowHandler:
            name: demoreturnresp
  variables:
  - name: testvar
    options:
      override:
        type: allowed
      required: true
    valueType: text
  version: v6
  versionState: draft

Explanation:

Example 2: Skip Condition in an OpenTofu Hook

The following example demonstrates how to configure skip conditions in OpenTofu hooks to:

apiVersion: eaas.envmgmt.io/v1
kind: ResourceTemplate
metadata:
  name: rautort1-904356
  project: defaultproject
spec:
  agents:
  - name: rauto-eaas-agent-904356
  contexts:
  - name: rauto-ec2-config-config-context-904356
  - name: rauto-aws-config-config-context-904356
  - name: rauto-infra-config-config-context-904356
  hooks:
    provider:
      openTofu:
        deploy:
          plan:
            before:
            - name: container
              onFailure: continue
              options:
                container:
                  envvars:
                    DOWNLOAD_TOKEN: sealed://hook.openTofu.deploy.plan.before.container.options.container.env_vars.DOWNLOAD_TOKEN
                    DOWNLOAD_URL: sealed://hook.openTofu.deploy.plan.before.container.options.container.env_vars.DOWNLOAD_URL
                    REPO_PATH: sealed://hook.openTofu.deploy.plan.before.container.options.container.env_vars.REPO_PATH
                  image: registry.dev.rafay-edge.net/rafay/infracost:v1
                  successCondition: "if #status.container.exitCode == 0 { success: true } \nif #status.container.exitCode != 0 { failed: true }"
                  workingDirPath: /infracost/
              skipConfig:
                condition: "## script begin ##\
def eval(**ctx):\
    return {\
        'skip': ctx['trigger']['payload']['action'] == 'destroy'\
    }\
## script end ##"
              timeoutSeconds: 500
              type: container
        destroy:
          plan:
            before:
            - name: destroyhookhttp
              onFailure: continue
              options:
                http:
                  endpoint: https://htjuk.com/get
                  headers:
                    Accept-Language: sealed://hook.openTofu.destroy.plan.before.destroyhookhttp.options.http.headers.Accept-Language
                    accept: sealed://hook.openTofu.destroy.plan.before.destroyhookhttp.options.http.headers.accept
                    test1: sealed://hook.openTofu.destroy.plan.before.destroyhookhttp.options.http.headers.test1
                    test2: sealed://hook.openTofu.destroy.plan.before.destroyhookhttp.options.http.headers.test2
                  method: GET
                  successCondition: |-
                    if #status.http.statusCode == 200 {
                      success: true
                    }

if #status.http.statusCode != 200 {
                      failed: true
                      reason: "url not reachable"
                    }
              skipConfig:
                skipOnDestroy: true
              timeoutSeconds: 500
              type: http
  provider: opentofu
  providerOptions:
    openTofu:
      backendConfigs:
      - tmp/inputs/one.tfvars
      backendType: custom
      lock: false
  repositoryOptions:
    branch: master
    directoryPath: workloads/eaas/terraform-guides/infrastructure-as-code/aws-ec2-instance/
    name: rauto-repo-904356
  secret:
    name: file://artifacts/rautort1-904356/sealed-secret.yaml
  version: version1-904356
  versionState: active

Explanation:

def eval(**ctx):
    return {
        'skip': ctx['trigger']['payload']['action'] == 'destroy'
    }