如何在Argo Workflows的map参数中传递多行JSON字符串?
结论
Argo Workflows完全支持该需求,现有方案无法运行的核心原因有两点:一是外层JSON数组中内嵌的policy字符串引号未正确转义,导致JSON结构非法,withParam无法正常解析参数;二是下游cat-os-release模板未声明policy参数,无法接收传递的数值。
可行实现方案
方案1:直接嵌套JSON对象(最优,无需转义)
Argo的withParam原生支持嵌套结构的JSON/YAML参数,不需要把policy强制转成字符串,直接作为嵌套对象放入数组即可。不需要处理转义逻辑,后续既可以直接读取policy内的字段,也可以通过内置toJson函数将整个policy转为字符串使用。
正确配置示例如下:
apiVersion: argoproj.io/v1alpha1 kind: Workflow metadata: generateName: loops-param-arg- spec: entrypoint: loop-param-arg-example arguments: parameters: - name: os-list # 直接写YAML结构,Argo会自动识别为合法数组供withParam使用 value: - image: "debian" tag: "9.1" policy: Version: "2012-10-17" Statement: - Sid: "BucketAdmin" Effect: "Allow" Principal: "*" Action: "s3:*" Resource: "*" - image: "ubuntu" tag: "17.10" policy: Version: "2012-10-17" Statement: - Sid: "BucketAdmin" Effect: "Allow" Principal: "*" Action: "s3:*" Resource: "*" templates: - name: loop-param-arg-example inputs: parameters: - name: os-list steps: - - name: test-linux template: cat-os-release arguments: parameters: - name: image value: "{{item.image}}" - name: tag value: "{{item.tag}}" # 可直接传递完整policy对象,也可以用{{toJson item.policy}}转为字符串传递 - name: policy value: "{{toJson item.policy}}" withParam: "{{inputs.parameters.os-list}}" - name: cat-os-release inputs: parameters: - name: image - name: tag - name: policy # 必须声明policy参数才能接收上游传递的数值 container: # 注意:原配置中将policy拼在镜像名称中属于语法错误,会导致镜像拉取失败,此处仅做示例输出 image: "{{inputs.parameters.image}}:{{inputs.parameters.tag}}" command: [echo] args: ["policy content: {{inputs.parameters.policy}}"]
方案2:保留policy为字符串格式(需正确转义)
如果业务要求必须把policy存为字符串类型,你可以选择两种方式处理转义:
- 把policy字符串用YAML单引号包裹,内部双引号不需要额外转义
- 将policy字符串内所有双引号替换为转义格式
\",保证外层JSON结构合法
内容的提问来源于stack exchange,提问作者user7171052
相关产品推荐
相关产品推荐

