3

我有一个通过 Argo Events 和 PubSub 触发的 WorkflowTemplate “nyc-test-template”。因此,如果我将消息发布{}到 PubSub 主题“argo-events-nyc”,则通过 a 指定的模板workflowTempateRef将启动。这确实工作得很好。现在我想参数化要启动的模板。

我的不工作草稿如下所示:

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

我想要发生的是这样的:

  • 一条空消息{}将触发“nyc-test-template”
  • 该消息{"wft": "my-template"}将触发“我的模板”

相反,发布空消息会导致传感器抛出错误:

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"

坦白说,上面的 yaml 就是从这个例子中得到了粗略的启发。这并不是有根据的猜测的结果,因为我仍然不了解参数、参数和输入如何交互的机制。

4

2 回答 2

2

您可以when根据参数切换要使用的模板。

假设我有两个简单的 WorkflowTemplate,如下所示:

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: t1
spec:
  templates:
    - name: whalesay-template
      container:
        image: docker/whalesay
        command: [cowsay]
        args: [t1]

---

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: t2
spec:
  templates:
    - name: whalesay-template
      container:
        image: docker/whalesay
        command: [cowsay]
        args: [t2]

我可以选择从 WorkflowTemplates 执行一个或另一个模板,具体取决于传递给 Workflow 的参数(手动或从 argo-events 设置)。

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: switch-
spec:
  entrypoint: pick
  arguments:
    parameters:
      - name: which
  templates:
    - name: pick
      steps:
        - - name: t1
            when: "{{workflow.parameters.which}} == t1"
            templateRef:
              name: t1
              template: whalesay-template
          - name: t2
            when: "{{workflow.parameters.which}} == t2"
            templateRef:
              name: t2
              template: whalesay-template

对于工作流的顶级参数,您可以使用workflow.parameters.SOMETHING.

在上述基础上,您可以使用 JSON 解析工具jq来检索切换值,然后根据该值选择您的模板。

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: switch-
spec:
  entrypoint: pick
  arguments:
    parameters:
      - name: json
  templates:
    - name: pick
      steps:
        - - name: parse
            template: parse
        - - name: t1
            when: "{{steps.parse.outputs.result}} == a"
            templateRef:
              name: t1
              template: whalesay-template
          - name: t2
            when: "{{steps.parse.outputs.result}} == b"
            templateRef:
              name: t2
              template: whalesay-template
    - name: parse
      container:
        image: jorgeandrada/alpine-jq
        command: [sh, -c]
        env:
          - name: JSON
            value: "{{workflow.parameters.json}}"
        args: [echo "$JSON" | jq -j '.test']

我应该提一下,使用jq有点笨拙。在 Argo (3.1+) 的未来版本中,将会有更直接地检查 JSON 的工具。但是这个解决方案很好地反向兼容。

于 2021-03-29T17:47:32.793 回答
2

归功于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
于 2021-03-29T18:32:45.907 回答