如何通过Workflow of Workflow模式向子Argo Workflow传递列表?
Argo Workflow of Workflows 参数传递问题解答
问题描述
我打算用Workflow of Workflow模式做工作流,需求是接收列表、分批提交给子工作流,以此精准控制工作流之间的并行度和成败条件。现在通过resource模板关联WorkflowTemplate,把列表当参数传进去,想用withParam运行,但输入参数显示是null;就连用官方的Workflow of Workflows示例测试,生成的子工作流里输入参数message也是null(不是模板里的默认值),想问这正常吗?我的设计有没有问题?
代码示例
apiVersion: argoproj.io/v1alpha1 kind: WorkflowTemplate metadata: name: workflow-template-submittable spec: entrypoint: whalesay-template arguments: parameters: - name: message value: hello world templates: - name: whalesay-template inputs: parameters: - name: message container: image: docker/whalesay command: [cowsay] args: ["{{inputs.parameters.message}}"] --- # This template demonstrates a workflow of workflows. # Workflow triggers one or more workflows and manages them. apiVersion: argoproj.io/v1alpha1 kind: Workflow metadata: generateName: workflow-of-workflows- spec: entrypoint: main templates: - name: main steps: - - name: workflow2 template: resource-with-argument arguments: parameters: - name: workflowtemplate value: "workflow-template-submittable" - name: message value: | [ { "image": "debian", "tag": "9.1" }, { "image": "debian", "tag": "8.9" }, { "image": "alpine", "tag": "3.6" }, { "image": "ubuntu", "tag": "17.10" } ] - name: resource-with-argument inputs: parameters: - name: workflowtemplate - name: message resource: action: create manifest: | apiVersion: argoproj.io/v1alpha1 kind: Workflow metadata: generateName: workflow-of-workflows-2- spec: arguments: parameters: - name: message value: {{inputs.parameters.message}} workflowTemplateRef: name: {{inputs.parameters.workflowtemplate}} successCondition: status.phase == Succeeded failureCondition: status.phase in (Failed, Error)
问题分析与解决
这种情况不正常,问题出在参数传递的逻辑上,跟你的设计思路无关,只是实现方式错了。
为什么参数会是null?
- 你在子工作流的manifest里定义了
arguments.parameters,但Argo通过resource创建子工作流并关联WorkflowTemplate时,不会自动把这些参数传递给模板的输入参数。 - 另外,你想用
withParam做分批,但当前代码里根本没用到withParam,只是把整个列表当单个参数传了,而且模板渲染时可能出错,导致参数值为空,覆盖了WorkflowTemplate的默认值,所以显示null。
怎么改才能正常工作?
要实现分批处理+参数正确传递,得结合withParam遍历列表,同时在子工作流里明确把参数绑定到WorkflowTemplate的输入上。
修复后的完整代码
apiVersion: argoproj.io/v1alpha1 kind: WorkflowTemplate metadata: name: workflow-template-submittable spec: entrypoint: whalesay-template arguments: parameters: - name: message value: hello world templates: - name: whalesay-template inputs: parameters: - name: message container: image: docker/whalesay command: [cowsay] args: ["{{inputs.parameters.message}}"] --- apiVersion: argoproj.io/v1alpha1 kind: Workflow metadata: generateName: workflow-of-workflows- spec: entrypoint: main templates: - name: main inputs: parameters: - name: items value: | [ { "image": "debian", "tag": "9.1" }, { "image": "debian", "tag": "8.9" }, { "image": "alpine", "tag": "3.6" }, { "image": "ubuntu", "tag": "17.10" } ] steps: - - name: spawn-sub-workflow template: resource-with-argument arguments: parameters: - name: workflowtemplate value: "workflow-template-submittable" - name: item value: "{{item}}" withParam: "{{inputs.parameters.items}}" # 遍历列表,每个项生成一个子工作流 - name: resource-with-argument inputs: parameters: - name: workflowtemplate - name: item resource: action: create manifest: | apiVersion: argoproj.io/v1alpha1 kind: Workflow metadata: generateName: workflow-of-workflows-2- spec: arguments: parameters: - name: message value: "{{inputs.parameters.item.image}}:{{inputs.parameters.item.tag}}" # 把单个项的信息传给message参数 workflowTemplateRef: name: {{inputs.parameters.workflowtemplate}} successCondition: status.phase == Succeeded failureCondition: status.phase in (Failed, Error)
关键修改点
- 在
main模板里添加withParam,遍历传入的列表,每一项都会触发一个独立的子工作流,实现分批处理。 - 新增
item参数接收withParam传递的单个列表项,然后在子工作流的manifest里,把item的具体内容(比如image:tag)赋值给message参数,确保WorkflowTemplate能拿到正确的输入。 - 之前的默认值被覆盖是因为子工作流显式设置了
message但值为空,现在参数正确传递后,要么用传进来的值,要么在没传的时候用模板的默认值。
总结
你的设计思路没问题,完全可以用Workflow of Workflows模式实现列表分批、控制并行度的需求。之前的问题只是参数传递的逻辑错误,按照上面的修改就能解决参数为null的问题。
内容的提问来源于stack exchange,提问作者maclu
相关产品推荐
相关产品推荐

