Search code examples
argoprojargo-events

How to parameterize a workflowTempateRef?


I have a WorkflowTemplate "nyc-test-template" which I trigger via Argo Events and PubSub. So, if I publish a message {} into the PubSub topic "argo-events-nyc" the template specified via a workflowTempateRef is started. That does work just fine. Now I want to parameterize the to be started template.

My not-working draft looks as follows:

apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
  name: pubsub-event-source-nyc
spec:
  template:
    serviceAccountName: argo-events
  pubSub:
    examplex:
      jsonBody: true
      topic: argo-events-nyc
      subscriptionID: argo-events-nyc-sub

---

apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: pubsub-sensor-nyc
spec:
  template:
    serviceAccountName: argo-events-sa
  dependencies:
    - name: pubsub-event-source-dep
      eventSourceName: pubsub-event-source-nyc
      eventName: examplex
  triggers:
    - template:
        name: argo-workflow-trigger
        argoWorkflow:
          group: argoproj.io
          version: v1alpha1
          resource: workflows
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: nyc-test-template-
                namespace: argo
              spec:
                arguments:
                  parameters:
                    - name: wft
                      value: nyc-test-template
                workflowTemplateRef:
                  # I'm pretty sure this inputs block is useless. But leaving it out 
                  # and instead referencing arguments.parameters.wft won't work either.
                  inputs:
                    parameters:
                      - name: wft
                  name: "{{inputs.parameters.wft}}"
          parameters:
            - src:
                dependencyName: pubsub-event-source-dep
                dataKey: body.wft
              dest: spec.arguments.parameters.0.value

What I would like to happen is this:

  • an empty message {} would trigger "nyc-test-template"
  • the message {"wft": "my-template"} would trigger "my-template"

Instead publishing an empty message will cause the Sensor to throw an error:

2021-03-29T15:31:16.386441528Z2021/03/29 15:31:16 Failed to parse workflow: error unmarshaling JSON: while decoding JSON: json: unknown field "inputs"

Frankly speaking - above yaml took crude inspiration from this example. It's not really the result of an educated guess as I still don't understand the mechanics of how parameters, arguments and inputs are interacting.


Solution

  • Credits go to Derek Wang.

    apiVersion: argoproj.io/v1alpha1
    kind: Sensor
    metadata:
      name: pubsub-sensor-nyc
    spec:
      template:
        serviceAccountName: argo-events-sa
      dependencies:
        - name: pubsub-event-source-dep
          eventSourceName: pubsub-event-source-nyc
          eventName: examplex
      triggers:
        - template:
            name: argo-workflow-trigger
            argoWorkflow:
              group: argoproj.io
              version: v1alpha1
              resource: workflows
              operation: submit
              source:
                resource:
                  apiVersion: argoproj.io/v1alpha1
                  kind: Workflow
                  metadata:
                    generateName: nyc-test-template-
                    namespace: argo
                  spec:
                    workflowTemplateRef:
                      name: nyc-test-template
              parameters:
                - src:
                    dependencyName: pubsub-event-source-dep
                    dataKey: body.wft
                    value: nyc-test-template # default value
                  dest: spec.workflowTemplateRef.name # <- this