[argo]Inputs and Outputs

Inputs and Outputs

1 Parameters

一种输入或输出是参数。与artifacts不同,这些是简单的字符串值,适用于大多数简单场景。

1.1 Input Parameters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: input-parameters-
spec:
entrypoint: main
arguments:
parameters:
- name: message
value: hello world
templates:
- name: main
inputs:
parameters:
- name: message
container:
image: busybox
command: [echo]
args: ["{{inputs.parameters.message}}"]

该例子表明main template有一个输入参数”message”

如果一个workflow有参数,可以使用-p来更改参数

1
argo submit --serviceaccount argo-workflow --watch input-parameters-workflow.yaml -p message='Welcome to Argo!'

1.2 Output Parameters

输出参数可以来自几个地方,但通常最灵活的是来自文件。在下面的例子中,容器创建了一个包含消息的文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: parameters-
spec:
entrypoint: main
templates:
- name: main
dag:
tasks:
- name: generate-parameter
template: echo
- name: consume-parameter
template: print-message
dependencies:
- generate-parameter
arguments:
parameters:
- name: message
value: "{{tasks.generate-parameter.outputs.parameters.hello-param}}"

- name: echo
container:
image: busybox
command: [sh, -c]
args: ["echo -n hello world > /tmp/hello_world.txt"]
outputs:
parameters:
- name: hello-param
valueFrom:
path: /tmp/hello_world.txt

- name: print-message
inputs:
parameters:
- name: message
container:
image: busybox
command: [echo]
args: ["{{inputs.parameters.message}}"]

在DAG模板和step模板中,可以使用模板标签(template tag)将一个任务的输出作为另一个任务的输入进行引用。

Parameters overview

Workflow input parameters

2 Artifacts

Artifact(工件)是对压缩并存储在对象存储(如S3、MinIO、GCS等)中的文件的一种称呼。

Argo中有两种类型的工件:

  • 输入工件:指的是从存储(例如S3)下载并作为卷挂载到容器内的文件。
  • 输出工件:指的是在容器内创建并上传到存储的文件。

工件通常会被上传到诸如AWS S3或GCP GCS之类存储服务中的一个桶里。我们将这种存储称为工件仓库(artifact repository)。

2.1 Output Artifacts

工作流中的每个任务都可以产生输出工件。要指定一个输出工件,你必须在清单中包含outputs

每个输出工件声明:

  • 在容器内的路径,用于定位该工件。
  • 一个名称,以便于引用该工件。

例如:

1
2
3
4
5
6
7
8
9
- name: save-message
container:
image: busybox
command: [ sh, -c ]
args: [ "echo hello world > /tmp/hello_world.txt" ]
outputs:
artifacts:
- name: hello-art
path: /tmp/hello_world.txt

当容器执行完毕后,文件将从容器中被复制出来,压缩,并存储起来。

文件也可以是目录形式,因此如果是一个目录,则会将该目录下的所有文件压缩成一个归档文件进行存储。

2.2 Input Artifacts

要声明一个输入工件,你必须在清单中包含inputs。每个输入工件必须声明:

  • 名称
  • 创建该artifact的路径

例如:

1
2
3
4
5
6
7
8
9
- name: print-message
inputs:
artifacts:
- name: message
path: /tmp/message
container:
image: busybox
command: [ sh, -c ]
args: [ "cat /tmp/message" ]

如果工件是一个压缩目录,则它会被解压并解包到指定路径中。

2.3 Inputs and Outputs

不能单独使用输入或输出,需要通过step或DAG模板将它们结合起来,如下例所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
- name: main
dag:
tasks:
- name: generate-artifact
template: save-message
- name: consume-artifact
template: print-message
dependencies:
- generate-artifact
arguments:
artifacts:
- name: message
from: "{{tasks.generate-artifact.outputs.artifacts.hello-art}}"

在上面的例子中,arguments用于声明工件输入的值。这里使用了模板标签,{{tasks.generate-artifact.outputs.artifacts.hello-art}}成为了仓库中工件的路径。

任务consume-artifact必须在generate-artifact之后运行,因此我们使用dependencies来声明这种关系。

完整的DAG工作流:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: artifacts-
spec:
entrypoint: main
templates:
- name: main
dag:
tasks:
- name: generate-artifact
template: save-message
- name: consume-artifact
template: print-message
dependencies:
- generate-artifact
arguments:
artifacts:
- name: message
from: "{{tasks.generate-artifact.outputs.artifacts.hello-art}}"

- name: save-message
container:
image: busybox
command: [sh, -c]
args: ["echo hello world > /tmp/hello_world.txt"]
outputs:
artifacts:
- name: hello-art
path: /tmp/hello_world.txt

- name: print-message
inputs:
artifacts:
- name: message
path: /tmp/message
container:
image: busybox
command: [sh, -c]
args: ["cat /tmp/message"]


[argo]Inputs and Outputs
https://erlsrnby04.github.io/2025/03/17/argo-Inputs-and-Outputs/
作者
ErlsrnBy04
发布于
2025年3月17日
许可协议