首页 > 其他分享 >ArgoWorkflow教程(六)---无缝实现步骤间参数传递

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

时间:2024-10-10 13:21:32浏览次数:1  
标签:参数传递 name parameters --- ArgoWorkflow world message parameter hello

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

之前我们分析了,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 如下:

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

标签:参数传递,name,parameters,---,ArgoWorkflow,world,message,parameter,hello
From: https://www.cnblogs.com/KubeExplorer/p/18456126

相关文章

  • 实时显示和拍照存储,IFD-x让你实时掌握温度信息 非接触式热成像仪器红外线成像
    实时显示和拍照存储,IFD-x让你实时掌握温度信息非接触式热成像仪器红外线成像非接触式热成像仪器,IFD-x采用红外阵列高精度温度传感器和先进的软件算法。它能够对视场范围内的任何物体进行红外成像,成像分辨率达到512*384像素,温度灵敏度为0.1℃,绝对精度为±1.5℃,刷新频率最高可达......
  • 《Programming from the Ground Up》阅读笔记:p217-p238
    《ProgrammingfromtheGroundUp》学习第11天,p217-p238总结,总计22页。一、技术总结1.Ccompilingp216,Ccompilingissplitintotwostages-thepreprocessorandthemaincompiler。注:感觉这个写法不好,因为preprocessor和compiler都是对象,这里应该指动作。应该是:Cco......
  • Vulkan进阶系列0 - Raytracing 基础
    一:概述    Vulkan的光线追踪是一种现代图形技术,用于实现更加逼真的高质量渲染效果。通过使用Vulkan的光线追踪扩展:VK_KHR_ray_tracing_pipeline和VK_KHR_acceleration_structure,程序员可以更加高效的模拟光线的传播,反射和折射,并能够跟踪光线在场景中的传播路径,计......
  • [赛记] csp-s模拟8 && csp-s模拟9
    HZOI大作战15pts赛时暴力跳父亲15pts;正解:发现在树上对于向上找大于这个数的操作具有随意划分性,所以考虑倍增,设$f_{i,j}$表示从$i$这个点向上跳$2^j$个比它大的数能跳到哪里,于是我们只需处理出向上跳一个(也就是$f_{i,0}$)的,然后$ST$表预处理即可;具体地,对于......
  • 线程池监控2-监控线程池状态、线程数量和队列任务数量等
    1.实现原理这篇博文是基于线程池监控1-监控任务执行时间,原理是:创建一个固定时间间隔执行的线程,来记录线程池的池状态、线程数量和队列任务数量等,具体方案:使用单例类缓存所有创建的线程池对象,类创建时启动定时任务线程,定期遍历缓存中线程池,记录线程池信息。2.实现代码packa......
  • PTA 作业三 继承与多态 JAVA 6-1 从抽象类shape类扩展出一个圆形类Circle 面向对象程
    6-1从抽象类shape类扩展出一个圆形类Circle分数25作者 张德慧单位 西安邮电大学请从下列的抽象类shape类扩展出一个圆形类Circle,这个类圆形的半径radius作为私有成员,类中应包含初始化半径的构造方法。publicabstractclassshape{//抽象类publicabstractdoubleg......
  • PTA 作业三 继承与多态 JAVA 面向对象程序设计7-1 周长计算器-1分数 30作者 Ma 单位
    7-1周长计算器-1分数30作者 Ma单位 山东科技大学1、定义一个接口Shape用于表示图形,其包含一个doublelength()的方法用于求周长。2、定义三角形类Triangle、长方形类Rectangle、圆形类Circle分别实现接口Shape3、定义测试类ShapeTest并使用Shape接口定义变......
  • 基于C语言编程实现发票四要素查验-医疗票真伪查验API
    发票四要素查验是指通过发票代码、发票号码、开票日期和校验码/金额这四个关键信息来验证发票的真实性。这些要素是每张发票独有的,伪造者很难复制出完全一样的发票。而发票查验接口、医疗票真伪查验API正是基于这一原理,为用户提供了一种便捷、高效的查验方式。首先,发票查......
  • C#基础知识总结-快速掌握看这一篇就够了
    C#基础知识总结-快速掌握看这一篇就够了目录一、类库:图书馆,命名空间:书架,类:书籍,方法:目录1、类库的引用2、初识类与名称空间3、依赖关系4、类与对象的关系......
  • MSSQL-从字符串转换日期和/或时间时,转换失败
    1、报错的sql为:selectID,Test_timeas时间,fromProcessDatawhereconvert(datetime,test_time,120)betweenconvert(datetime,'2020-10-10',120)andconvert(datetime,'2024-10-11',120)   它是将Test_time转化为datetime格式,再用between进行比较;......