Stories

Detail Return Return

ArgoWorkflow教程(六)---無縫實現步驟間參數傳遞 - Stories Detail

argoworkflow-6-parameter-passing-between-steps.png

之前我們分析了,Workflow、WorkflowTemplate 、template 3 者之間如何傳遞參數。

本文主要分析同一個 Workflow 中的不同 step 之間實現參數傳遞,比如將上一個步驟的輸出作為下一個步驟的結果進行使用(而非以文件方式傳遞)。

<!--more-->

1. 概述

然後就是之前只分析了 Workflow、WorkflowTemplate 、template 3 者之間如何傳遞參數,今天繼續分析一下步驟之間如何傳遞參數。

要實現步驟間參數傳遞,需要實現兩個功能:

  • 1)導出結果
  • 2)導入參數

基於之前的知識,要實現這兩個功能,可以想到的一種方式就是使用 artifact:

  • 導出結果:將參數寫入文件,然後以 artifact 保存到 s3
  • 導入參數:下一個 step 下載 artifact 並從中獲取參數。

確實可以實現功能,但是有點蹩腳,畢竟 artifact 主要是用於保存文件的。argoworkflow 中也直接提供了對應的 feature 來供大家使用。

2. 步驟間參數傳遞

  • 將結果導出為 Output Parameter
  • 將上一步的 Output Parameter 導入為當前步驟的 Input Parameter

完整 Demo 如下:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: output-parameter-
spec:
  entrypoint: output-parameter
  templates:
  - name: output-parameter
    steps:
    - - name: generate-parameter
        template: whalesay
    - - name: consume-parameter
        template: print-message
        arguments:
          parameters:
          # Pass the hello-param output from the generate-parameter step as the message input to print-message
          - name: message
            value: "{{steps.generate-parameter.outputs.parameters.hello-param}}"

  - name: whalesay
    container:
      image: docker/whalesay:latest
      command: [sh, -c]
      args: ["echo -n hello world > /tmp/hello_world.txt"]  # generate the content of hello_world.txt
    outputs:
      parameters:
      - name: hello-param  # name of output parameter
        valueFrom:
          path: /tmp/hello_world.txt # set the value of hello-param to the contents of this hello-world.txt

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

導出結果

- name: whalesay
    container:
      image: docker/whalesay:latest
      command: [sh, -c]
      args: ["echo -n hello world > /tmp/hello_world.txt"]  # generate the content of hello_world.txt
    outputs:
      parameters:
      - name: hello-param  # name of output parameter
        valueFrom:
          path: /tmp/hello_world.txt # set the value of hello-param to the contents of this hello-world.txt

首先是 step 的內容,這裏為了簡單,就只有一個 echo 命令,將結果(hello world)寫入到文件

/tmp/hello_world.txt 中。

然後就是到處結果了:

    outputs:
      parameters:
      - name: hello-param  # name of output parameter
        valueFrom:
          path: /tmp/hello_world.txt # set the value of hello-param to the contents of this hello-world.txt

定義了一個 output 參數,名為 hello-param,該參數的 value 從 /tmp/hello_world.txt 文件中獲取,最終得到的 value 就是之前寫入的 hello world

至此,我們就講當前步驟的結果導出成了一個 Output Parameter,可以在後續步驟使用了。

導入參數

後續步驟,其實很簡單,和普通步驟一樣的,通過 Input Parameter 定義參數,然後在使用的使用通過語法{{inputs.parameters.name}} 引用即可。

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

唯一區別在於,這個參數的來源,之前我們都是直接講參數定義在 Workflow 中的,這裏需要改成引用之前步驟導出的 Output Parameter,就像這樣:

spec:
  entrypoint: output-parameter
  templates:
  - name: output-parameter
    steps:
    - - name: generate-parameter
        template: whalesay
    - - name: consume-parameter
        template: print-message
        arguments:
          parameters:
          # Pass the hello-param output from the generate-parameter step as the message input to print-message
          - name: message
            value: "{{steps.generate-parameter.outputs.parameters.hello-param}}"

在 arguments.parameters 中直接引用了之前步驟的 Output Parameter,語法為 {{steps.$stepName.outputs.parameters.$parameterName}}

之前我們導出結果的步驟名為 generate-parameter,然後導出的參數名為 hello-param,因此這裏就使用{{steps.generate-parameter.outputs.parameters.hello-param}} 來引用該參數。

內置的 result 參數

除了我們手動導出的參數之外,ArgoWorkflow 還會默認生成一個 Output Parameter,他就是 result。

和其他 Output Parameter 一樣,可以通過 {{steps.$stepName.outputs.parameters.$parameterName}} 語法進行引用。

這個 result 參數會捕獲最大 256KB 的標準輸出作為 value,因此他可以包含以下內容:

  • 1)script 的運行結果
  • 2)容器的標準輸出
  • 3)...

只要是在容器中輸出到標準輸出的,內容都可以被 result 捕獲。


【ArgoWorkflow 系列】持續更新中,搜索公眾號【探索雲原生】訂閲,閲讀更多文章。


3. 小結

本文主要分析了 Argo 中的 Workflow 中怎麼傳遞參數還是比較簡單的:

  • 1)通過 Output Parameter 導出參數
  • 2)在 arguments.parameters 中引用上一步導出的參數

最後介紹了一下內置的 result Output Parameter ,可以用於獲取容器中的標準輸出。

user avatar tongbo Avatar code4world Avatar cloudpilot-ai Avatar yunxiao0816 Avatar mangrandechangjinglu Avatar vistart Avatar tyltr Avatar songzihuan Avatar yangrd Avatar sayornottt Avatar east4ming Avatar headofhouchang Avatar
Favorites 14 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.