如何在 argo 工作流程中的地图中传递多行 json 字符串
Posted
技术标签:
【中文标题】如何在 argo 工作流程中的地图中传递多行 json 字符串【英文标题】:how to pass a multi line json string in map in argo workflows 【发布时间】:2022-01-04 16:03:04 【问题描述】:我正在创建一个带有循环 withParam
用于映射变量的 argo 工作流。在这张地图中,我想使用多行 json 字符串。有什么方法可以使用吗?
这是使用policy
作为多行字符串的方式,但它不起作用
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: loops-param-arg-
spec:
entrypoint: loop-param-arg-example
arguments:
parameters:
- name: os-list
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"
withParam: "inputs.parameters.os-list"
- name: cat-os-release
inputs:
parameters:
- name: image
- name: tag
container:
image: "inputs.parameters.image:inputs.parameters.tag and policy is inputs.parameters.policy"
command: [cat]
args: [/etc/os-release]
在 argo 工作流程中有什么方法可以实现这一点吗?如果没有,有什么替代方法可以做到这一点?
【问题讨论】:
【参考方案1】:存在三个问题:
-
策略不是有效的 JSON。星号必须加引号,因为它们是字符串。
"Sid": "BucketAdmin",
"Effect": "Allow",
- "Principal": *,
+ "Principal": "*",
"Action": "s3:*",
- "Resource": *
+ "Resource": "*"
(更新:实际上不需要此更改 - Argo Workflows 正确处理 policy
JSON 对象。)策略应编码为字符串,以便可以轻松地作为参数传递。例如:
"\"Version\":\"2012-10-17\",\"Statement\":[\"Sid\":\"BucketAdmin\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:*\",\"Resource\":\"*\"]"
策略值需要作为参数显式传递给 cat-os-release 模板。
最终的工作流程应如下所示:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: loops-param-arg-
spec:
entrypoint: loop-param-arg-example
arguments:
parameters:
- name: os-list
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"
- name: policy
value: "item.policy"
withParam: "inputs.parameters.os-list"
- name: cat-os-release
inputs:
parameters:
- name: image
- name: tag
- name: policy
container:
image: "inputs.parameters.image:inputs.parameters.tag"
command: [echo]
args: ["inputs.parameters.policy"]
【讨论】:
感谢@michael crenshaw 的回答,它正在工作。但是我有很大的政策,如果我为政策中的每条指令添加转义字符,我们就会失去政策的可读性。 我已对问题进行了更改。为星号添加双引号 @monu 很有趣,不知道你可以有一个非字符串item.something
成员。将更新我的答案!【参考方案2】:
Michael 提供的解决方案有效,以下解决方案也适用于您,并具有一定的政策可读性
kind: Workflow
metadata:
generateName: loops-param-arg-
spec:
entrypoint: loop-param-arg-example
arguments:
parameters:
- name: os-list
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"
withParam: "inputs.parameters.os-list"
- name: cat-os-release
inputs:
parameters:
- name: image
- name: tag
container:
image: "inputs.parameters.image:inputs.parameters.tag and policy is inputs.parameters.policy"
command: [cat]
args: [/etc/os-release]
【讨论】:
以上是关于如何在 argo 工作流程中的地图中传递多行 json 字符串的主要内容,如果未能解决你的问题,请参考以下文章
argo的输入输出--output和input输出目录或文件到下一步骤