ArgoWorkflow教程(二)---快速构建流水线:Workflow & Template 概念

ArgoCD打造GitOPS流水线 Argo(https://argocdproj.github.com/projects/argo)项目是一组Kubernetes原生工具集合,用于运行和管理Kubernetes上作业和应用程序。Argo提供了一种在Kubernetes上创建应用的模式。所有的Argo工具都实现为控制器和自定义资源。为什么选择ArgoCD应用程序定义、配置和环境应该是声明性的,并受版本控制。应用程序部署和生命周期管理应该是自动化的、可审计的、易于理解的。 阅读详情
AI摘要

本文深入解析ArgoWorkflow中的核心概念,包括Workflow、WorkflowTemplate及Template,探讨它们之间的关系及最佳实践。

argoworkflow-2-model-analyze.png

上一篇我们部署了 ArgoWorkflow,并创建了一个简单的流水线做了个 Demo。本篇主要分析 ArgoWorkflow 中流水线相关的概念,了解概念后才能更好使用 ArgoWorkflow。

本文主要分析以下问题:

  • 1)如何创建流水线? Workflow 中各参数含义
  • 2)WorkflowTemplate 流水线模版如何使用,
  • 3)Workflow、WorkflowTemplate、template 之间的引用关系
  • 4)ArgoWorkflow 流水线最佳实践

**【ArgoWorkflow 系列】**持续更新中,搜索公众号【探索云原生】订阅,阅读更多文章。


1.基本概念

ArgoWorkflow 中包括以下几个概念:

  • Workflow:流水线,真正运行的流水线实例,类似于 Tekton 中的 pipelinerun
  • WorkflowTemplate:流水线模板,可以基于模板创建流水线,类似于 Tekton 中的 pipeline
    • ClusterWorkflowTemplate:集群级别的流水线模板,和 WorkflowTemplate 的关系类似于 K8s 中的 Role 和 ClusterRole
  • templates:Workflow 或者 WorkflowTemplate/ClusterWorkflowTemplate 的最小组成单位,流水线由多个 template 组成,可以理解为流水线中的某一个步骤。

WorkflowTemplate 和 ClusterWorkflowTemplate 暂时统称为 Template。

Workflow、Template(大写)、template(小写)之间的关系如下:

arg-workflow-template-ref.png

三者间关系比较复杂,官方也有提到这块因为一些历史遗留问题导致命名上比较混乱

个人感觉下面这种方式比较好理解:

  • template(小写):为 Template(大写)的基本组成单位,可以理解为流水线中的步骤
  • Template(大写):一条完整的流水线,一般由多个 template(小写) 组成
  • Workflow:真正运行的流水线实例,一般由 Template 直接创建,类似于流水线运行记录,每一条记录就是一个 Workflow

理清基本概念之后,接下来就看下看具体对象的分析。

2.Workflow

Workflow 是Argo中最重要的资源,具有两个重要功能:

  • 1)工作流定义
  • 2)工作流状态存储

先看下 Workflow 是怎么样的,以下是一个简单的 Workflow 例子:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: steps-
spec:
  entrypoint: hello           # We reference our first "template" here
  templates:
  - name: hello               # The first "template" in this Workflow, it is referenced by "entrypoint"
    steps:                    # The type of this "template" is "steps"
    - - name: hello
        template: whalesay    # We reference our second "template" here
        arguments:
          parameters: [{name: message, value: "hello"}]

  - name: whalesay             # The second "template" in this Workflow, it is referenced by "hello"
    inputs:
      parameters:
      - name: message
    container:                # The type of this "template" is "container"
      image: docker/whalesay
      command: [cowsay]
      args: ["{{inputs.parameters.message}}"]

整个 Workflow 对象核心内容分为以下三部分:

  • templates:模板列表,此处定义了流水线中的所有步骤以及步骤之间的先后顺序。
  • entrypoint: 流水线入口,类似于代码中的 main 方法,此处一般引用某一个 template invocators 即可。
  • parameters:流水线中用到的参数,包括 arguments 块中的全局参数和 inputs 块中的局部参数两种

entrypoint

Workflow 中必须要指定 entrypoint,entrypoint 作为任务的执行起点,类似于程序中的 main 方法。

templates

ArgoWorkflow 当前支持 6 种 template,接下来挨个分析一下。

container

和 Kubernetes container spec 是一致的,这个类型的 template 就是启动一个 container,用户可以指定image、command、args 等信息来控制具体执行的动作。

  - name: whalesay
    container:
      image: docker/whalesay
      command: [cowsay]
      args: ["hello world"]
script

script 实际上是 container 的封装,spec 和 container 一致,同时增加了 source 字段,用于定义一个脚本,脚本的运行结果会记录到{{tasks.<NAME>.outputs.result}} or {{steps.<NAME>.outputs.result}}。

script 可以理解为简化了使用 container 来执行脚本的配置

  - name: gen-random-int
    script:
      image: python:alpine3.6
      command: [python]
      source: |
        import random
        i = random.randint(1, 100)
        print(i)
resource

Resource 类型的 template 用于操作集群中的资源,action 参数表示具体的动作,支持 get, create, apply, delete, replace, patch。

  - name: k8s-owner-reference
    resource:
      action: create
      manifest: |
        apiVersion: v1
        kind: ConfigMap
        metadata:
          generateName: owned-eg-
        data:
          some: value
suspend

Suspend 类型的 template 比较简单,就是用于暂停流水线执行。

默认会一直阻塞直到用户通过argo resume命令手动恢复,或者通过duration 参数指定暂停时间,到时间后会自动恢复。

  - name: delay
    suspend:
      duration: "20s"
steps

Steps 用于处理模版之间的关系,具体包含两方面:

  • 1)哪些任务需要运行
  • 2)这些任务按照什么先后顺序运行

看下面这个例子:

  - name: hello-hello-hello
    steps:
    - - name: step1
        template: prepare-data
    - - name: step2a
        template: run-data-first-half
      - name: step2b
        template: run-data-second-half

哪些任务需要运行?

该 steps 则定义了要运行 step1、step2a、step2b 3 个 template。

这些任务按照什么先后顺序运行?

steps 中元素定义的先后顺序就是各个任务的执行先后顺序,在这里就是 step1 先运行,然后 step2a、step2b 并行运行。

注意:仔细看 yaml 中 step2a 和 step2b 是同一个元素中的,steps 是一个二维数组, 定义如下:

type Template struct {
    Steps []ParallelSteps `json:"steps,omitempty" protobuf:"bytes,11,opt,name=steps"`
}
type ParallelSteps struct {
    Steps []WorkflowStep `json:"-" protobuf:"bytes,1,rep,name=steps"`
}

转换为 json 形式就像这样:

{
    "steps": [
        ["step1"],
        ["step2a", "step2b"]
    ]
}

这样应该比较清晰了,先后顺序一目了然

dag

Dag template 的作用和 steps 是一样的。

这里的 DAG 就是 Directed Acyclic Graph 这个 DAG。

DAG 和 Steps 区别在于任务先后顺序的定义上:

  • Steps 以定义先后顺序作为 template 执行先后顺序
  • DAG 则可以定义任务之间的依赖,由 argo 根据依赖自行生成最终的运行的先后顺序

看下面这个例子:

  - name: diamond
    dag:
      tasks:
      - name: A
        template: echo
      - name: B
        dependencies: [A]
        template: echo
      - name: C
        dependencies: [A]
        template: echo
      - name: D
        dependencies: [B, C]
        template: echo

DAG 中新增了 dependencies 字段,可以指定当前步骤依赖的的依赖。

哪些任务需要运行?

该 steps 则定义了要运行 A、B、C、D 4 个任务。

这些任务按照什么先后顺序运行?

不如 Steps 那么直接,需要根据 dependencies 分析依赖关系。

A 没有依赖,因此最先执行,B、C 都只依赖于 A,因此会再 A 后同时执行,D 则依赖于 B、C,因此会等B、C都完成后才执行。

转换为 json 形式如下:

{
    "steps": [
        ["A"],
        ["B", "C"],
        ["D"]
    ]
}

ps:相比之下 steps 方式更为直接,任务先后顺序一目了然。如果整个 Workflow 中所有任务先后顺序理清楚了就推荐使用 steps,如果很复杂,只知道每个任务之间的依赖关系那就直接用 DAG,让 ArgoWorkflow 计算。

template definitions & template invocators

大家可以发现,steps、dag 模板和另外 4 个不一样,他们都是可以指定多个 template 的。

前面分别介绍了 ArgoWorkflow 中的 6 种 template,实际上可以按照具体作用将这 6 个 template 分为 **template definitions(模板定义)**以及 **template invocators(模板调用器)**两种。

  • template definitions(模板定义):该类型 template 用于定义具体步骤要执行的内容,例子中的 whalesay 模板就是该类型
    • 包含 container, script, resource, suspend 等类型
  • template invocators(模板调用器):该类型 template 用于组合其他 template definitions(模版定义) ,定义步骤间的执行顺序等,例子中的 hello 模板就是该类型。
  • 一般 entrypoint 指向的就是该类型的模板
  • 包含dag 和 steps 两种类型,例子中的 hello 模板就是 steps 类型。

吐槽一下:template 这里有点绕,如果能将 模板定义 、模板调用器 拆分为两个不同的对象就比较清晰。

了解完 template 分类之后再回头看之前的 Workflow 例子就比较清晰了:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: steps-
spec:
  entrypoint: hello           # We reference our first "template" here
  templates:
  - name: hello               # The first "template" in this Workflow, it is referenced by "entrypoint"
    steps:                    # The type of this "template" is "steps"
    - - name: hello
        template: whalesay    # We reference our second "template" here
        arguments:
          parameters: [{name: message, value: "hello"}]

  - name: whalesay             # The second "template" in this Workflow, it is referenced by "hello"
    inputs:
      parameters:
      - name: message
    container:                # The type of this "template" is "container"
      image: docker/whalesay
      command: [cowsay]
      args: ["{{inputs.parameters.message}}"]
  • 1)首先 whalesay 模板是一个 container 类型的 template,因此是 template definitions(模板定义)
  • 2)其次 hello 是一个 steps 类型的 template,因此是 template invocators(模板调用器)
    • 在该调用器中 steps 字段中定义了一个名为 hello 的步骤,该步骤引用的就是 whalesay template
  • 3)entrypoint 指定的是 hello 这个 template invocators(模板调用器)

接下来就是 Workflow 中另一重要对象 entrypoit。

entrypoint

entrypoint 作为任务的执行起点,类似于程序中的 main 方法,每个 Workflow 中都必须要指定 entrypoint。

注意:只有被 entrypoint 指定的任务才会运行,因此,entrypoint 一般只会指定 Steps 和 DAG 类型的 template,也就是template invocators(模板调用器)。然后由 Steps 中的 steps 或者 DAG中的 tasks 来指定多个任务。

因此,并不是 Workflow 中写了的 templates 都会执行。

看下面这个例子:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: steps-
spec:
  entrypoint: hello           # We reference our first "template" here
  templates:
  - name: hello               # The first "template" in this Workflow, it is referenced by "entrypoint"
    steps:                    # The type of this "template" is "steps"
    - - name: hello
        template: whalesay    # We reference our second "template" here
        arguments:
          parameters: [{name: message, value: "hello"}]
  - name: whalesay             # The second "template" in this Workflow, it is referenced by "hello"
    inputs:
      parameters:
      - name: message
    container:                # The type of this "template" is "container"
      image: docker/whalesay
      command: [cowsay]
      args: ["{{inputs.parameters.message}}"]

Entrypoint 指定 hello,然后 hello 是一个 steps 类型的 template,也就是**template invocators。**然后在 hello template 的 steps 中指定了 whalesay 这个 template,最终 whalesay template 为 container 类型,也就是 **template definitions。**这里就是最终要运行的任务。

当然,entrypoint 也可以指定 **template definitions(模板定义)**类型的 template,不过这样只能运行一个任务,就像这样:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: steps-
spec:
  entrypoint: whalesay
  templates:
  - name: whalesay    
    container:
      image: docker/whalesay
      command: [cowsay]
      args: ["hello"]

至此,我们应该基本搞清楚了 Workflow 对象(参数部分除外)。接下来就看一下最后一部分,parameters。

Demo

列出几个复杂一点点的 Workflow,看一下是不是真的搞懂 Workflow 了。

下面是一个包含 4个任务的 Workflow:

  • 1)首先打印 hello
  • 2)然后执行一段 python 脚本,生成随机数
  • 3)sleep 20s
  • 4)创建一个 Configmap

提供了 steps 和 dag 两种写法,可以对比下

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: steps-
spec:
  entrypoint: hello
  templates:
  - name: hello              
    steps:          
    - - name: hello
        template: whalesay
        arguments:
          parameters: [{name: message, value: "hello"}]
    - - name: runscript
        template: gen-random-int
    - - name: sleep
        template: delay
    - - name: create-cm
        template: k8s-owner-reference
  # - name: diamond
  #   dag:
  #     tasks:
  #     - name: hello
  #       template: whalesay
  #       arguments:
  #         parameters: [{name: message, value: "hello"}]
  #     - name: runscript
  #       dependencies: [hello]
  #       template: gen-random-int
  #     - name: sleep
  #       template: delay
  #       dependencies: [runscript]
  #     - name: create-cm
  #       template: k8s-owner-reference
  #       dependencies: [sleep]
  - name: whalesay
    inputs:
      parameters:
      - name: message
    container:
      image: docker/whalesay
      command: [cowsay]
      args: ["{{inputs.parameters.message}}"]
  - name: gen-random-int
    script:
      image: python:alpine3.6
      command: [python]
      source: |
        import random
        i = random.randint(1, 100)
        print(i)
  - name: k8s-owner-reference
    resource:
      action: create
      manifest: |
        apiVersion: v1
        kind: ConfigMap
        metadata:
          generateName: owned-eg-
        data:
          host: lixueduan.com
          wx: 探索云原生
  - name: delay
    suspend:
      duration: "20s"

parameters

Workflow 中的参数可以分为以下两种:

  • 形参:在 template(template definitions) 中通过 inputs 字段定义需要哪些参数,可以指定默认值
  • 实参:在 template(template invocators) 中通过 arguments 字段为参数赋值,覆盖 inputs 中的默认值

以上仅为个人理解

inputs 形式参数

template 中可以使用 spec.templates[*].inputs 字段来指定形式参数,在 template 中可以通过{{inputs.parameters.$name}} 语法来引用参数。

下面这个例子则是声明了 template 需要一个名为 message 的参数,这样调用方在使用该 template 时就知道需要传哪些参数过来。

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

当然也可以指定默认值

  templates:
    - name: whalesay-template
      inputs:
        parameters:
          - name: message
            value: "default message"
      container:
        image: docker/whalesay
        command: [cowsay]
        args: ["{{inputs.parameters.message}}"]

注意:如果未指定默认值,则调用该 template 时必须指定该参数,有默认值则可以不指定。

arguments 实际参数

spec.arguments用于定义要传递的实际参数,这部分参数在当前 Workflow 下的所有 Template 中都可以使用,可以使用 {{workflow.parameters.$name}} 语法来引用。

例如下面这个例子中指定了一个名为 message 的参数,并赋值为 hello world。

  arguments:
    parameters:
      - name: message
        value: hello world
参数复用

除了在 steps/dag 中指定 arguments,甚至可以直接在 Workflow 中指定,然后 steps/dag 中通过{{workflow.parameters.$name}} 语法进行引用。这样即可实现参数复用,Workflow 中定义一次,steps/dag 中可以多次引用。

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: example-
spec:
  entrypoint: main
  arguments:
    parameters:
    - name: workflow-param-1
  templates:
  - name: main
    dag:
      tasks:
      - name: step-A 
        template: step-template-A
        arguments:
          parameters:
          - name: template-param-1
            value: "{{workflow.parameters.workflow-param-1}}"

  - name: step-template-A
    inputs:
      parameters:
        - name: template-param-1
    script:
      image: alpine
      command: [/bin/sh]
      source: |
          echo "{{inputs.parameters.template-param-1}}"

Demo

通过下面这个 Demo 来理解参数传递:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: steps-
spec:
  entrypoint: hello           # We reference our first "template" here
  templates:
  - name: hello               # The first "template" in this Workflow, it is referenced by "entrypoint"
    steps:                    # The type of this "template" is "steps"
    - - name: hello
        template: whalesay    # We reference our second "template" here
        arguments:
          parameters: [{name: message, value: "hello"}]

  - name: whalesay             # The second "template" in this Workflow, it is referenced by "hello"
    inputs:
      parameters:
      - name: message
    container:                # The type of this "template" is "container"
      image: docker/whalesay
      command: [cowsay]
      args: ["{{inputs.parameters.message}}"]

上述例子中,template whalesay 定义了需要一个名为 message 的参数,同时在 steps template 中引用 whalesay 时通过 arguments 指定了参数 message 的值为 hello。因此最终会打印出 hello。

3.WorkflowTemplate

官方原文:

A WorkflowTemplate is a definition of a Workflow that lives in your cluster.

WorkflowTemplate 就是 Workflow 的定义,WorkflowTemplate 描述了这个流水线的详细信息,包括有哪些任务,任务之间的先后顺序等等。

根据前面对 Workflow 的描述可知,我们能直接创建 Workflow 对象来运行流水线,不过这种方式存在的一些问题:

  • 1)如果 template 比较多的话,Workflow 对象就会特别大,修改起来比较麻烦
  • 2)模板无法共享,不同 Workflow 都需要写一样的 template,或者同一个 template 会出现在不同的 Workflow yaml 中。

因此,关于 Workflow 和 WorkflowTemplate 的最佳实践:将 template 存到 WorkflowTemplate,Workflow 中只引用 Template 并提供参数即可。

而 ArgoWorkflow 中的工作流模板根据范围不同分为 WorkflowTemplate 和 ClusterWorkflowTemplate 两种。

  • WorkflowTemplate:命名空间范围,只能在同一命名空间引用
  • ClusterWorkflowTemplate:集群范围,任意命名空间都可以引用

WorkflowTemplate

下面是一个简单的 WorkflowTemplate:

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: workflow-template-submittable
  namespace: default
spec:
  entrypoint: whalesay-template
  arguments:
    parameters:
      - name: message
        value: tpl-argument-default
  templates:
    - name: whalesay-template
      inputs:
        parameters:
          - name: message
            value: tpl-input-default
      container:
        image: docker/whalesay
        command: [cowsay]
        args: ["{{inputs.parameters.message}}"]

可以看到 WorkflowTemplate 和 Workflow 参数是一模一样,这里就不在赘述了。

只需要将 kind 由 Workflow 替换为 WorkflowTemplate 即可实现转换。

workflowMetadata

workflowMetadata 是 Template 中独有的一个字段,主要用于存储元数据,后续由这个 Template 创建出的 Workflow 都会自动携带上这些信息。

通过这些信息可以追踪到 Workflow 是由哪个 Template 创建的。

使用方式就像下面这样,workflowMetadata 中指定了一个 label

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: workflow-template-submittable
spec:
  workflowMetadata:
    labels:
      example-label: example-value

然后由该 Template 创建的 Workflow 对象都会携带这个 label:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  annotations:
    workflows.argoproj.io/pod-name-format: v2
  creationTimestamp: "2023-10-27T06:26:13Z"
  generateName: workflow-template-hello-world-
  generation: 2
  labels:
    example-label: example-value
  name: workflow-template-hello-world-5w7ss
  namespace: default

ClusterWorkflowTemplate

类似于 WorkflowTemplate,可以理解为 k8s 中的 Role 和 ClusterRole 的关系,作用域不同罢了。

和 WorkflowTemplate 所有参数都一致,只是将 yaml 中的 kind 替换为 ClusterWorkflowTemplate 即可。

4.TemplateRef

创建好 WorkflowTemplate 之后就可以在 Workflow 中使用 TemplateRef 直接引用对应模板了,这样 Workflow 对象就会比较干净。

对于 WorkflowTemplate 的引用也有两种方式:

  • 1)workflowTemplateRef:引用完整的 WorkflowTemplate,Workflow 中只需要指定全局参数即可
  • 2)templateRef:只引用某一个 template,Workflow 中还可以指定其他的 template、entrypoint 等信息。

workflowTemplateRef

可以通过workflowTemplateRef字段直接引用 WorkflowTemplate。

注意📢:这里需要 Workflow 和 WorkflowTemplate 在同一个命名空间。

就像这样:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: workflow-template-hello-world-
spec:
  arguments:
    parameters:
      - name: message
        value: "from workflow"
  workflowTemplateRef:
    name: workflow-template-submittable

workflowTemplateRef 指定要引用的 Template 名字即可,这一句就相当于把对应 Template 中 spec 字段下的所有内容都搬过来了,包括 entrypoint、template 等。

Workflow 中一般只需要通过 argument 字段来实现参数覆盖,当然也可以不指定,若 Workflow 中不指定参数则会使用 Template 中提供的默认值

一个最精简的 Workflow 如下:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: workflow-template-hello-world-
spec:
  workflowTemplateRef:
    name: workflow-template-submittable

只有 workflowTemplateRef 字段,未提供 argument 参数。

和前面的 Workflow 对比起来,内容就更少了,因为大部分都写在 WorkflowTemplate 里了,Workflow 中一般只需要指定参数就行。

clusterWorkflowTemplateRef

和 workflowTemplateRef 类似,只需要增加 clusterScope: true 配置即可。

默认为 false,即 WorkflowTemplate

就像这样:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: workflow-template-hello-world-
spec:
  entrypoint: whalesay
  templates:
  - name: whalesay
    steps:                              # You should only reference external "templates" in a "steps" or "dag" "template".
      - - name: call-whalesay-template
          templateRef:                  # You can reference a "template" from another "WorkflowTemplate or ClusterWorkflowTemplate" using this field
            name: cluster-workflow-template-whalesay-template   # This is the name of the "WorkflowTemplate or ClusterWorkflowTemplate" CRD that contains the "template" you want
            template: whalesay-template # This is the name of the "template" you want to reference
            clusterScope: true          # This field indicates this templateRef is pointing ClusterWorkflowTemplate
          arguments:                    # You can pass in arguments as normal
            parameters:
            - name: message
              value: "hello world"

核心部分:

  workflowTemplateRef:
    name: cluster-workflow-template-submittable
    clusterScope: true

当指定为集群范围时,ArgoWorkflow 会去搜索 ClusterWorkflowTemplate 对象,否则会在当前命名空间搜索 WorkflowTemplate。

templateRef

除了使用 workflowTemplateRef / clusterWorkflowTemplateRef 引用整个 WorkflowTemplate 之外,还可以使用 templateRef 参数来实现引用 WorkflowTemplate 中的某一个 template。

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: workflow-template-hello-world-
spec:
  entrypoint: whalesay
  templates:
  - name: whalesay
    steps:                              # You should only reference external "templates" in a "steps" or "dag" "template".
      - - name: call-whalesay-template
          templateRef:                  # You can reference a "template" from another "WorkflowTemplate" using this field
            name: workflow-template-1   # This is the name of the "WorkflowTemplate" CRD that contains the "template" you want
            template: whalesay-template # This is the name of the "template" you want to reference
          arguments:                    # You can pass in arguments as normal
            parameters:
            - name: message
              value: "hello world"

核心为:

          templateRef:
            name: workflow-template-1
            template: whalesay-template

参数分析:

  • name 指的是 WorkflowTemplate 名称,即 workflow-template-1 这个 WorkflowTemplat
  • template 指的就是 template 的名称 ,即 workflow-template-1 这个 WorkflowTemplat 中的名为 whalesay-template 的 template

注意📢:根据官方文档,最好不要在 steps 和 dag 这两个**template invocators(模板调用器)**之外使用 templateRef 。

原文如下:

You should never reference another template directly on a template object (outside of a steps or dag template). This behavior is deprecated, no longer supported, and will be removed in a future version.

Parameters

WorkflowTemplate 中使用参数和 Workflow 基本一致,没有太大区别。

  • template definitions 中通过 inputs 字段定义需要的参数
  • template invocators 中通过 arguments 字段为参数复制

Workflow 中引用 WorkflowTemplate 时可以再次定义 arguments 以覆盖 WorkflowTemplate 中的默认参数。

最佳实践

根据 workflowTemplateRef 和 templateRef 描述可以得出以下最佳实践:

  • 1)workflowTemplateRef 使用:WorkflowTemplate 中定义完整流水线内容,包括 entrypoint、template,Workflow 中通过 workflowTemplateRef 引用并通过 argument 指定参数覆盖默认值。
  • 2)templateRef 使用:WorkflowTemplate 中不定义完整流水线内容,只定义常用 template,Workflow 中需要使用该类型 template 时,通过 templateRef 引用,而不是在 Workflow 中再定义一次该 template。

第一种情况 WorkflowTemplate 可以称为流水线模板,第二种情况下 WorkflowTemplate 充当 template-utils 角色。


**【ArgoWorkflow 系列】**持续更新中,搜索公众号【探索云原生】订阅,阅读更多文章。


5.小结

本文主要分析了 ArgoWorkflow 中的 Workflow、WorkflowTemplate、template 对象及其之间的联系。

1)Workflow 对象:由 templates、entrypoint 组成。

  • templates:按照作用分为 template definitions(模板定义)以及 template invocators(模板调用器)
    • template definitions:用于定义具体步骤要执行的内容,包括 container、script、resource、suspend
    • template invocators:用于组合其他模板,控制任务先后顺序,包括 steps、dag
  • entrypoint:任务执行起点

2)WorkflowTemplate/ClusterWorkflowTemplate 对象:和 Workflow 对象一致,但只用于定义流水线,不可运行

  • Workflow 中可以通过 WorkflowTemplateRef/clusterWorkflowTemplateRef 引用整个 WorkflowTemplate
  • 或者通过 templateRef 引用某个 template

3)Parameters:分为形参和实参

  • inputs:形参,申明该 template 需要使用哪些参数,可指定默认值
  • arguments:实参,为对应 template 中的参数赋值,会覆盖 inputs 提供的默认值

4)参数优先级:

  • Workflow arguments > WorkflowTemplate arguments > WorkflowTemplate inputs

最后则是Workflow、WorkflowTemplate、template 这三者之间的关系

arg-workflow-template-ref.png

5)一些最佳实践:

WorkflowTemplate:

  • 1)可以用作流水线模版:WorkflowTemplate 中定义全部的 entrypoint 和 templates,Workflow 中通过 WorkflowTemplateRef 引入即可使用
  • 2)用作 template-utils:WorkflowTemplate 中仅包含常用的 template,Workflow 中要使用时通过 templateRef 引入,避免重复定义 template

steps 和 dag 选择:

  • steps 方式更为直接,任务先后顺序一目了然,但是需要理清所有任务的先后顺序
  • dag 则不够直观,但是填写时只需要知道某个任务的依赖关系就可以添加新任务

建议:如果整个 Workflow 中所有任务先后顺序理清楚了就推荐使用 steps,如果很复杂,只知道每个任务之间的依赖关系那就直接用 DAG,让 ArgoWorkflow 计算。

Argo Workflows v3.0+ 实战:从零构建 4 步 CI/CD 流水线(含 Kaniko 镜像构建) 本文详细介绍了如何利用 Argo Workflows v3.0+ 构建企业级 CI/CD 流水线,涵盖代码编译、镜像构建、安全扫描和蓝绿部署等关键步骤。通过 Kubernetes 原生工作流引擎,实现高效、安全的容器化流水线部署,特别包含 Kaniko 镜像构建的实战示例,助力 DevOps 团队提升云原生环境下的自动化水平。 阅读详情

相关推荐

基于Argo全家桶构建云原生CI/CD全链路自动化实战

在云原生应用交付领域,持续集成与持续部署(CI/CD)是提升开发运维效率的核心实践。其原理在于通过自动化流水线,将代码变更快速、可靠地转化为生产环境中的运行服务,从而实现快速迭代与高质量交付。该技术的核心价值在于实现了软件交付过程的标准化、可重复与可观测,显著降低了人工操作错误与部署风险。典型的应用场景包括微服务架构下的多环境部署、复杂业务工作流编排以及渐进式发布等。本文聚焦于如何利用Argo生态中的Workflows、CD、Events和Rollouts四大组件,通过事件驱动与GitOps理念,协同构建一

weixin_33874713的博客 379

argo-workflow学习个人总结

文章目录前言一、Argo workflow what?一、Argo workflow Why?二、Argo workflow How?1.安装2.argo-wrokflow整体架构1.argo artifacts 传递总结 前言 本文记录argo-workflow学习过程,具体操作练习请参照官网 argo官网 主要初略研究argo-workflow各组件之间的作用和关系 由于刚接触K8S 更多深入问题后续补充 一、Argo workflow what? Argo workflow专门设计解决.

weixin_43953343的博客 8461

Silvaco Atlas新手入门:从安装到第一个半导体器件仿真(附DeckBuild配置)

本文为Silvaco Atlas新手提供了一份从软件安装、DeckBuild环境配置到完成第一个半导体器件仿真的实战指南。通过构建一个经典的PN结二极管案例,详细讲解了Atlas仿真语句的语法、核心命令顺序以及结果调试与可视化方法,帮助初学者快速上手并理解TCAD仿真流程。

sql99的博客 333

Argo项目实战示例:Argo Workflows(when分支,循环,递归等); Argo CD; Argo Events; Argo Rollouts

有些人可能会说为什么EventBus不直接到Trigger,中间引入一个Sensor,这主要是两个原因,一是为了使事件转发和处理松耦合,二是为了实现Trigger事件的参数化,通过Sensor不仅可以实现事件的过滤,还可以实现事件的参数化,比如后面的Trigger是创建一个Kubernetes Pod,那这个Pod的metadata、env等,都可以根据事件内容进行填充。Job解决了在Kubernetes单次执行任务的问题,但不支持任务的编排,难以解决多任务之间的依赖和数据共享。...

学亮编程手记 8227

Android逆向进阶篇 Charles + Drony Vpn绕过代理检测方案 获取猫眼App数据

Android逆向进阶篇 Charles + Drony Vpn绕过代理检测方案 获取猫眼App数据

曾用名:极客柒 1853

01、【solidworks】windosws正在配置solidworks问题及注册表未卸载干净问题解决

windosws正在配置solidworks问题及注册表未卸载干净问题解决

qq_740388770的博客 2万+

云原生工作流引擎 Argo Workflows 概述

Argo Workflows(以下简称 Argo) 是一款开源的云原生工作流引擎,专为实现 Kubernetes(以下简称 K8s) 环境下的并行作业编排与管理而设计。它允许用户通过声明式的方式来定义工作流程,这些流程不仅包括简单的顺序执行任务(步骤工作流),还能支持复杂的并行处理和条件分支逻辑(DAG 工作流)。

春天熊的技术博客 5387

【XR技术介绍】深度解析|AI眼镜核心技术架构:从硬件层到端云AI全链路拆解

本文将逐层拆解AI眼镜完整技术架构,梳理硬件、感知、光学、端侧AI、软件云端五大核心层级,剖析核心数据流与行业技术瓶颈,同时附上标准化技术架构图,适合技术学习、方案汇报、行业科普使用。

houdou112358的博客 443

ArgoWorkflow教程(八)---基于 LifecycleHook 实现流水线通知提醒

合集 - ArgoWorkflow(8)1.ArgoWorkflow教程(二)---快速构建流水线:Workflow &amp; Template 概念08-202.ArgoWorkflow 教程(一)--DevOps 另一选择?云原生 CICD 初体验08-063.ArgoWorkflow教程(三)---使用 Artifacts 实现步骤间文件共享08-294.ArgoWorkflow教程(四)...

weixin_41083520的博客 171

QLIB使用tushare更新每日行情

0. 介绍 qlib每日数据的源是yahoo,更新速度较慢并且不稳定。tushare每日行情是免费接口,速度较快。 1. 获取行情数据,保存为csv格式。 def GetDataFromTushare(self, today, fpath): pro = ts.pro_api() df = pro.daily(trade_date=today) for index, row in df.iterrows(): code = row['ts_code']

weixin_44177216的博客 2900

Oracle EBS R12 主 / 辅助分类帐(主 / 辅助帐套)

本文档提供了Oracle EBS R12主/辅助分类帐的完整实施指南,包含主帐套(法定帐套)和辅助帐套(4种模式)的配置清单、科目映射规则模板(一对一、多对一、重分类、税会差异、币种转换)、4类典型调整分录示例(存货跌价、折旧差异、往来重分类、外币折算)以及完整业务闭环案例。重点说明了SLA子分类帐的核心配置逻辑,并提供了可直接用于项目交付的配置文档、调整规则清单和期末结账流程。文档采用R12主分类帐+辅助分类帐标准格式,适用于多准则、多币种、税务调整等复杂场景的实施。

金牌架构师 196

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

合集 - ArgoWorkflow(6)1.ArgoWorkflow教程(二)---快速构建流水线:Workflow &amp; Template 概念08-202.ArgoWorkflow 教程(一)--DevOps 另一选择?云原生 CICD 初体验08-063.ArgoWorkflow教程(三)---使用 Artifacts 实现步骤间文件共享08-294.ArgoWorkflow教程(四)...

weixin_41852950的博客 213

从数据库到数据仓库:数据仓库导论

本文为数据仓库导论,旨在介绍数据仓库的基本理念和应用场景,帮助读者理解数据仓库的重要性及其在企业中的实际应用。

探索大数据世界 - 深入解析数据存储、分布式计算与人工智能 2787

分布式系统思维构建高可靠软件工厂:架构设计与工程实践

在现代软件工程领域,分布式系统设计原则已成为构建复杂、高可用应用架构的基石,其核心价值在于通过容错、一致性、可观测性和弹性伸缩等机制,保障系统在大规模、高并发场景下的稳定运行。这一理念正被延伸至研发效能体系本身,即将整个软件交付流水线——包括CI/CD、自动化测试和基础设施管理——视为一个分布式系统进行设计与优化。通过引入消息队列、工作流引擎和分布式追踪等关键技术,软件工厂能够将孤立的构建、测试任务转化为可协调、可观测的分布式服务,从而系统性解决流水线脆弱性、可观测性黑洞和资源弹性等工程挑战。这种架构模式特

weixin_34248705的博客 274

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

ArgoWorkflow教程(一)---DevOps 另一选择?云原生 CICD: ArgoWorkflow 初体验

DevOps 另一选择?云原生 CICD: ArgoWorkflow 初体验。

探索云原生 3477

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

自动化工作流管理:通过 Argo Workflows 简化 Kubernetes 工作流执行

Argo Workflows 是 Kubernetes 上的工作流编排工具,帮助用户自动化任务流的创建、调度与管理,支持参数化、并行执行和监控,广泛应用于 CI/CD、数据处理等场景。

专注分享编程开发与技术进阶干货! 2713

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Argo Workflows-Kubernetes的工作流引擎

什么是Argo Workflows?Argo Workflows是一个开源项目,为Kubernetes提供container-native工作流程,其主要通过Kubernetes CRD实现的。特点如下:工作流的每一步都是一个容器将多步骤工作流建模为一系列任务,或者使用有向无环图(DAG)描述任务之间的依赖关系可以在短时间内轻松运行用于机器学习或数据处理的计算密集型作业在Kubernetes上运行CI/CD Pipeline,无需复杂的软件配置安装安装控制器端Argo Wordflows的安装非常简单,直接

wanger5354的博客 1661

Argo Workflows概述,安装部署,服务的发布、加密方式

Argo Workflows概述,安装部署,服务的发布、加密方式一、什么是Argo Workflows?二、为什么选择Argon Workflows?三、快速开始3.1 安装Argo Workflows3.2 下载安装3.3 Argo Controller四、Argo Server4.1 Offloading Large Workflows4.2 Workflow Archive4.3 托管模式:Hosted Mode4.4 本地模式:Local Mode4.5 选项:Options身份验证模式:Auth

爱是与世界平行 5118

Argo Workflows 中文快速指南·

Argo Workflows是一个云原生的通用的工作流引擎。本教程主要介绍如何用其完成持续集成(Continous Integration, CI)任务。本文全部内容来自:https://github.com/LinuxSuRen/argo-workflows-guide如果您感觉本文对您或其他人有帮助,欢迎点赞、转发、分享。更加丰富、详细的内容也会在上面的 GitHub 仓库中更新,大家可以在...

Jenkins中文社区 2130

ArgoWorkflow教程(五)---Workflow 的多种触发模式:手动、定时任务与事件触发

手动触发:手动提交一个 Workflow,就会触发一次构建,那么我们创建的流水线,理论上是 WorkflowTemplate 对象。,类似于 k8s 中的 job 和 cronjob,CronWorkflow 会定时创建 Workflow 来实现定时触发。Event 事件触发:比如通过git commit 触发,借助可以实现此功能。

探索云原生 2036

Algo的workflow

Argo 是一套开源的云原生工具集,专注于 Kubernetes 平台上的持续交付和工作流编排。它旨在简化和自动化应用程序的构建、测试、部署和管理流程。1. Argo Workflows:工作流引擎Argo Workflows 是一个 Kubernetes 原生的工作流引擎,用于编排并行任务。它将工作流的每个步骤定义为容器,并使用 Kubernetes 的资源调度能力来执行这些步骤。特点:每个步骤都在一个独立的容器中运行,确保隔离性和可重复性。使用 YAML 文件定义工作流,易于理解和维护。

远方雪的专栏 1473

云原生工作流引擎 - argo-workflow

argo argo工作流是什么 Argo Workflows是一个开源的容器本机工作流引擎,用于在Kubernetes上协调并行作业。 Argo Workflows通过Kubernetes CRD(自定义资源定义)实现。 定义工作流,其中工作流中的每个步骤都是一个容器。 将多步骤工作流建模为一系列任务,或者使用图形(DAG)捕获任务之间的依赖关系。 使用Kubernetes上的Argo Workflow,可以在短时间内轻松运行用于计算机学习或数据处理的计算密集型作业。 在Kubernetes上本地运行CI

yevvzi的博客 5558

Argo Workflows 原理及应用

01组件概述Argo Workflows是一个开源的容器原生工作流引擎,允许用户在Kubernetes集群上定义并执行复杂的业务流程。支持对相关流程进行个性化编排,包括执行顺序、相互之间的依赖等等。Argo Workflows 是以 Kubernetes 自定义资源定义(Custom Resource Definitions,简称 CRD)的形式来实现其功能。CRD 是 Kubernetes 提供...

SOHU_TECH的博客 2634

Argo workflow 入门

Argo workflow 作为云原生的workflow 引擎被大量应用于CI/CD , 基础设施自动化支持 一般来说按照argo workflow 提供的官方文档可以完成 入门体验,argo workflow依赖的部分镜像,有某些不可描述的原因无法正常访问到,本文记录了笔者在安装和入门过程中踩的坑,以便遇到同样问题的朋友可以快速解决问题。 1 前置条件 要安装argo workflow 前提条件是你要有一套k8s环境。可以是完整的k8s, 如果只本地练习,可以考虑安装一个minikube。

m0_49032526的博客 2417
上一篇: ArgoWorkflow 教程(一)--DevOps 另一选择?云原生 CICD 初体验
下一篇: ArgoWorkflow教程(三)---使用 Artifacts 实现步骤间文件共享
探索云原生
博客等级 码龄9年 730粉丝 131原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值