How to pass or parse a resource event source body generated from an argo workflow?

Issue

I have a resource sensor to trigger a workflow that reports on the original workflow that led to the event – a kind of logger.
In the parameters of the sensor, I want to grab the body of the original workflow in order to extract certain values from it – currently I was planning to simply pass the whole JSON as an input string.

The problem arises when the workflow is non-trivial and contains variable files, e.g.:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: test-es-workflow-
  labels:
    class: dummy-in
spec:
  entrypoint: whalesay
  arguments:
    parameters:
      - name: message
        value: hello world
  templates:
    - name: whalesay
      inputs:
        parameters:
          - name: message
      container:
        image: docker/whalesay:latest
        command: [cowsay]
        args: ["{{inputs.parameters.message}}"]

When this happens the triggered workflow:

apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: demo-sensor
spec:
  template:
    serviceAccountName: operate-workflow-sa
  dependencies:
    - name: test-dep-sensor
      eventSourceName: demo-wf-submit
      eventName: demo-log
  triggers:
    - template:
        name: argo-workflow
        k8s:
          operation: create
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: demo-log-
              spec:
                entrypoint: demolog
                arguments:
                  parameters:
                    - name: body
                      value: hello world
                templates:
                - name: demolog
                  inputs:
                    parameters:
                      - name: body
                  container:
                    image: demolog:latest
                    imagePullPolicy: "Always"
                    command: [/app/demoapp.py]
                    args: ["-j", "{{inputs.parameters.body}}"]
          parameters:
            - src:
                dependencyName: test-dep-sensor
                dataKey: body
                value: wow! a default value
              dest: spec.arguments.parameters.0.value
      retryStrategy:
        steps: 3

fails with the message Message: invalid spec: templates.demolog: failed to resolve {{inputs.parameters.message}}

If the workflow does not contain any variables (nothing enclosed in {{}}), then the triggered workflow executes as anticipated.

I would be happy with figuring out how to get to any or all of the following solutions:

  1. Warn that a variable failed to resolve and substitute null
  2. Do not resolve variables within the body of the event, i.e. render it as a plain string
  3. Resolve the variables at the time of creating the event such that the body may be parsed

Environment:
Kubernetes: v1.20.15;
Argo: v3.2.9;
Argo Events: v1.6.3

Solution

I developed an answer of type 2, that is to render the variables enclosed in double curly braces.
It’s not elegant, but it appears to work:

Patch the dependencies section of the sensor to replace the double curly braces with something else (I chose "%%") using jq

  dependencies:
    - name: test-dep-sensor
      eventSourceName: demo-wf-submit
      eventName: demo-log
      transform:
        jq: 'walk( if type=="string" then gsub("{{";"%%") else . end ) | walk( if type=="string" then gsub("}}";"%%") else . end )'

Additional information:

Argo Events Transformations in sensors

jq manual

Answered By – DoctorSoup

Answer Checked By – Robin (GoLangFix Admin)

Leave a Reply

Your email address will not be published.