无法通过另一个工作流模板将输出参数从一个工作流模板传递到工作流

Unable to pass output parameters from one workflowTemplate to a workflow via another workflowTemplate

我有两个工作流模板 generate-outputlib-read-outputs 和一个工作流 output-paramter,如下所示

  1. generate-output.yaml
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: generate-output
spec:
  entrypoint: main
  templates:
    - name: main
      dag:
        tasks:

          # Generate Json for Outputs
          - name: read-outputs
            arguments:
              parameters:
                - name: outdata
                  value: |
                    {
                      "version": 4,
                      "terraform_version": "0.14.11",
                      "serial": 0,
                      "lineage": "732322df-5bd43-6e92-8f46-56c0dddwe83cb4",
                      "outputs": {
                        "key_alias_arn": {
                          "value": "arn:aws:kms:us-west-2:123456789:alias/tetsing-key",
                          "type": "string",
                          "sensitive": true
                        },
                        "key_arn": {
                          "value": "arn:aws:kms:us-west-2:123456789:alias/tetsing-key",
                          "type": "string",
                          "sensitive": true
                        }
                      }
                    }
            template: retrieve-outputs

    # Create Json
    - name: retrieve-outputs
      inputs:
        parameters:
          - name: outdata
      script:
        image: python
        command: [python]
        env:
          - name: OUTDATA
            value: "{{inputs.parameters.outdata}}"
        source: |
          import json
          import os
          OUTDATA = json.loads(os.environ["OUTDATA"])
          with open('/tmp/templates_lst.json', 'w') as outfile:
            outfile.write(str(json.dumps(OUTDATA['outputs'])))
        volumeMounts:
          - name: out
            mountPath: /tmp
      volumes:
        - name: out
          emptyDir: { }
      outputs:
        parameters:
          - name: message
            valueFrom:
              path: /tmp/templates_lst.json

  1. lib-read-outputs.yaml
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: lib-read-outputs
spec:
  entrypoint: main
  templates:
    - name: main
      dag:
        tasks:
          # Read Outputs
          - name: lib-wft
            templateRef:
              name: generate-output
              template: main
  1. output-paramter.yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: output-paramter-
spec:
  entrypoint: main
  templates:
    - name: main
      dag:
        tasks:
          # Json Output data task1
          - name: wf
            templateRef:
              name: lib-read-outputs
              template: main

          - name: lib-wf2
            dependencies: [wf]
            arguments:
              parameters:
                - name: outputResult
                  value: "{{tasks.wf.outputs.parameters.message}}"
            template: whalesay

    - name: whalesay
      inputs:
        parameters:
          - name: outputResult
      container:
        image: docker/whalesay:latest
        command: [cowsay]
        args: ["{{inputs.parameters.outputResult}}"]

我正在尝试通过 lib-read-outputs

将工作流模板 generate-output 中生成的输出参数传递给工作流 output-paramter

当我执行它们时,出现以下错误 - Failed: invalid spec: templates.main.tasks.lib-wf2 failed to resolve {{tasks.wf.outputs.parameters.message}}

DAG 和步骤模板默认不生成输出

DAG 和步骤模板不会自动生成其子模板的输出,即使只有一个子模板也是如此。

例如,这里的 no-parameters 模板不会产生输出,即使它调用了一个 有输出的模板。

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
spec:
  templates:
  - name: no-parameters
    dag:
      tasks:
      - name: get-a-parameter
        template: get-a-parameter

如果您考虑具有多个任务的 DAG 模板,这种缺少输出是有意义的:

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
spec:
  templates:
  - name: no-parameters
    dag:
      tasks:
      - name: get-a-parameter
        template: get-a-parameter
      - name: get-another-parameter
        depends: get-a-parameter
        template: get-another-parameter

哪个任务的输出应该no-parameters产生?由于不清楚,DAG 和步骤模板默认情况下根本不产生输出。

您可以将模板视为函数。您不会期望函数隐式 return 它调用的函数的输出。

def get_a_string():
    return "Hello, world!"

def call_get_a_string():
    get_a_string()

print(call_get_a_string())  # This prints nothing.

但是 DAG 或步骤模板可以转发 输出

您可以通过设置其 outputs 字段使 DAG 或步骤模板 转发 输出。

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: get-parameters-wftmpl
spec:
  templates:
  - name: get-parameters
    dag:
      tasks:
      - name: get-a-parameter
        template: get-a-parameter
      - name: get-another-parameter
        depends: get-a-parameter
        template: get-another-parameter
    # This is the critical part!
    outputs:
      parameters:
      - name: parameter-1
        valueFrom: 
          expression: "tasks['get-a-parameter'].outputs.parameters['parameter-name']"
      - name: parameter-2
        valueFrom: 
          expression: "tasks['get-another-parameter'].outputs.parameters['parameter-name']"
---
apiVersion: argoproj.io/v1alpha1
kind: Workflow
spec:
  templates:
  - name: print-parameter
    dag:
      tasks:
      - name: get-parameters
        templateRef:
          name: get-parameters-wftmpl
          template: get-parameters 
      - name: print-parameter
        depends: get-parameters
        template: print-parameter
        arguments:
          parameters:
          - name: parameter
            value: "{{tasks.get-parameters.outputs.parameters.parameter-1}}"

继续Python类比:

def get_a_string():
    return "Hello, world!"

def call_get_a_string():
    return get_a_string()  # Add 'return'.

print(call_get_a_string())  # This prints "Hello, world!".

所以,在你的具体情况下......

  1. outputs 部分添加到 generate-parameter WorkflowTemplate 中的 main 模板以转发来自 retrieve-parameters 模板的输出参数。

    apiVersion: argoproj.io/v1alpha1
    kind: WorkflowTemplate
    metadata:
      name: generate-parameter
    spec:
      entrypoint: main
      templates:
        - name: main
          outputs:
            parameters:
              - name: message
                valueFrom: 
                  expression: "tasks['read-parameters'].outputs.parameters.message"
          dag:
            tasks:
              # ... the rest of the file ...
    
    
  2. outputs 部分添加到 lib-read-parameters WorkflowTemplate 中的 main 模板以转发 generate-parameter 的参数。

    apiVersion: argoproj.io/v1alpha1
    kind: WorkflowTemplate
    metadata:
      name: lib-read-parameters
    spec:
      entrypoint: main
      templates:
        - name: main
          outputs:
            parameters:
              - name: message
                valueFrom:
                  expression: "tasks['lib-wft'].outputs.parameters.message"
          dag:
            tasks:
              # ... the rest of the file ...