美文网首页
【Argo Workflow】三、给Workflow添加参数

【Argo Workflow】三、给Workflow添加参数

作者: 别点了填写不了昵称 | 来源:发表于2022-07-24 13:34 被阅读0次

下面给出了一个Parameters的案例。这里需要解释一些关键信息:

  • spec.entrypoint 的作用是标记一个主工作流模板,可以理解为main函数的作用,是整个workflow的入口。
  • spec.templates 是需要你自定义的一系列workflow中的node模板或者是引用其他创建的模板,templates本质上是一个template数组,这个后续我们会讨论到,这里不再过多讲解。这里我们如果需要使用参数功能,就需要使用到template.inputs这个字段。
  • Inputs 对象是在workflow中被指定作为输入参数或者输入资源的一种抽象类型。其中包含inputs.artifactsinputs.parameters两个字段,分别表示输入的参数类型数组和输入的文件类型数组。我们本节只涉及到parameters,artifacts会在后面的文章中介绍。
  • spec.arguments Arguments类型包含和Inputs类型,同样也包含parameters和artifacts两个数组字段。和Inputs不同的是,这个字段是作为整个Workflow的参数声明字段,Inputs是作为Template的参数声明字段。
  • Parameter 是输入输出参数的抽象类型,其中的name字段声明的值作为key,value字段声明的值作为参数值,其中的其他字段后续会进行解释。

下面是摘抄自官网的案例。同样的,我们还是使用Web UI的方式提交我们的yaml,去除掉了apiVersion和kind字段。

metadata:
  generateName: hello-world-parameters-
spec:
  # invoke the whalesay template with
  # "hello world" as the argument
  # to the message parameter
  entrypoint: whalesay
  arguments:
    parameters:
    - name: message
      value: hello parameters

  templates:
  - name: whalesay
    inputs:
      parameters:
      - name: message       # parameter declaration
    container:
      # run cowsay with that message input parameter as args
      image: docker/whalesay
      command: [cowsay]
      args: ["{{inputs.parameters.message}}"]

在template中可以使用{{inputs.parameters.your-paramter-name}}的方式引入我们的参数,前提是你必须要声明你所需要的参数。使用双括号表达式来替换值是Argo Workflow常见的一种方式,后面还有很多使用到此特性的数值和文件传递方式。

为了避免和之前的hello world案例冲突,我们将hello world换成了hello parameters,workflow的输出如下:

W
time="2022-07-24T05:38:19.056Z" level=info msg="capturing logs" argo=true
 _________________
< hello parameters >
 -----------------
    \
     \
      \
                    ##        .
              ## ## ##       ==
           ## ## ## ##      ===
       /""""""""""""""""___/ ===
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
       \______ o          __/
        \    \        __/
          \____\______/
time="2022-07-24T05:38:20.067Z" level=info msg="sub-process exited" argo=true error="<nil>"

小鲸鱼已经输出了我们的 hello parameters

这里补充说明一下,和官方的例子相比,我这里去掉了一些使用中没有用到,但是相当重要的知识点。这个对于学过k8s的不是一件难理解的事情,但是我还是需要解释一下。

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: hello-world-parameters-
spec:
  # invoke the whalesay template with
  # "hello world" as the argument
  # to the message parameter
  entrypoint: whalesay
  arguments:
    parameters:
    - name: message
      value: hello world

  templates:
  - name: whalesay
    inputs:
      parameters:
      - name: message       # parameter declaration
    container:
      # run cowsay with that message input parameter as args
      image: docker/whalesay
      command: [cowsay]
      args: ["{{inputs.parameters.message}}"]

我们在使用的是 Argo Workflow 本质上是通过k8s拓展出来的功能,上面的yaml文件是由Argo Workflow自定义的一些CRD来进行规范的。apiVersionkind是k8s比较重要的知识点,如果了解的小伙伴可以先学习k8s相关的知识。

相关文章

网友评论

      本文标题:【Argo Workflow】三、给Workflow添加参数

      本文链接:https://www.haomeiwen.com/subject/qggiirtx.html