ArgoWorkflow教程(六)---无缝实现步骤间参数传递

之前我们分析了,Workflow、WorkflowTemplate 、template 3 者之间如何传递参数。

本文主要分析同一个 Workflow 中的不同 step 之间实现参数传递,比如将上一个步骤的输出作为下一个步骤的结果进行使用(而非以文件方式传递)。

1. 概述

然后就是之前只分析了 Workflow、WorkflowTemplate 、template 3 者之间如何传递参数,今天继续分析一下步骤之间如何传递参数。

要实现步骤间参数传递,需要实现两个功能:

  • 1)导出结果

  • 2)导入参数

基于之前的知识,要实现这两个功能,可以想到的一种方式就是使用 artifact:

  • 导出结果:将参数写入文件,然后以 artifact 保存到 s3
  • 导入参数:下一个 step 下载 artifact 并从中获取参数。

确实可以实现功能,但是有点蹩脚,毕竟 artifact 主要是用于保存文件的。argoworkflow 中也直接提供了对应的 feature 来供大家使用。

2. 步骤间参数传递

  • 将结果导出为 Output Parameter
  • 将上一步的 Output Parameter 导入为当前步骤的 Input Parameter

完整 Demo 如下:

yaml 复制代码
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}}"]

导出结果

yaml 复制代码
- 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 中。

然后就是到处结果了:

yaml 复制代码
    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}} 引用即可。

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

唯一区别在于,这个参数的来源,之前我们都是直接讲参数定义在 Workflow 中的,这里需要改成引用之前步骤导出的 Output Parameter,就像这样:

yaml 复制代码
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 ,可以用于获取容器中的标准输出。

相关推荐
萧曵 丶3 小时前
docker 部署 filebeat 采集日志导入到elasticsearch 设置pipeline
elasticsearch·docker·jenkins·filebeat
it技术分享just_free10 小时前
基于Jenkins+K8S构建DevOps自动化运维管理平台
linux·运维·容器·kubernetes·k8s·jenkins·devops
天草二十六_简村人1 天前
Jenkins新安装的插件ThinBackup,如何恢复之前的备份
运维·jenkins·devops
玩转测试开发2 天前
提升 CI/CD 稳定性:Jenkins 开机自检与推送通知
运维·python·ci/cd·jenkins
Linux运维老纪2 天前
计算机基本组成和工作原理(Basic Components and Working Principles of Computers)
java·windows·python·计算机网络·云计算·devops
大G哥3 天前
ELK日志收集之ES的DSL查询语句
大数据·elk·elasticsearch·搜索引擎·jenkins
苦逼IT运维4 天前
YUM 源与 APT 源的详解及使用指南
linux·运维·ubuntu·centos·devops
henan程序媛5 天前
Jenkins Pipline流水线
运维·pipeline·jenkins
wudinaniya6 天前
使用rsync+jenkins实现服务自动部署全流程
运维·jenkins·自动化部署