【问题标题】:How to pass multiple lists in a single loop in argo workflows如何在 argo 工作流中的单个循环中传递多个列表
【发布时间】:2021-11-09 18:52:59
【问题描述】:

我想使用 argo 模板在一个循环中传递多个列表的元素

我有 3 个如下列表

effect = ["Allow", "Allow", "Allow"]
resource = ["*", "*", "*"]
action = ["ec2:CreateTags", "ec2:DescribeInstances", "ec2:AuthorizeSecurityGroupIngress"]

我有一个需要使用 argo 模板构建的 IAM 策略。以下 IAM 策略从每个循环中的 3 个列表中获取元素。我们如何在一个循环中传递这三个列表元素?

我参考了 argo 文档,只有 withItems/withParams 循环可用,一次只需要一个列表

我尝试了下面的方法,但它不起作用

- name: policy
  value: |-
  <<-EOP
  {
    "Effect": "{{item.1}}",
    "Action": "{{item.2}}",
    "Resource": "{{item.3}}"
  }
  EOP
  withTogether:
   - "{{inputs.parameters.effect}}"
   - "{{inputs.parameters.actions}}"
   - "{{inputs.parameters.resources}}"

如果 argo 不支持,有没有其他方法可以实现?

【问题讨论】:

  • 你能发布你期望给定输入的确切 JSON 吗?
  • 和这个JSON一样[{"action":"ec2:CreateTags","effect":"Allow","resource":"*"},{"action":"ec2:DescribeInstances","effect":"Allow","resource":"*"},{"action":"ec2:AuthorizeSecurityGroupIngress","effect":"Allow","resource":"*"}]

标签: argo-workflows


【解决方案1】:

不要使用 withItems/withParams 进行简单的 JSON 操作。 Argo Workflows 用至少一个 Pod 表示这些循环的每次迭代。这很慢。

我建议使用熟悉的编程语言和script template 来执行这项工作。

Argo 有一个简单的内置“表达式标签”模板工具,您可以使用它来执行相同的突变。

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: so-69180308-
spec:
  arguments:
    parameters:
      - name: effects
        value: '["Allow", "Allow", "Allow"]'
      - name: resources
        value: '["*", "*", "*"]'
      - name: actions
        value: '["ec2:CreateTags", "ec2:DescribeInstances", "ec2:AuthorizeSecurityGroupIngress"]'
  entrypoint: main
  templates:
    - name: main
      script:
        env:
          - name: POLICIES
            value: >-
              {{=
                toJson(
                  map(
                    0..(len(jsonpath(workflow.parameters['effects'], '$'))-1),
                    {
                      {
                        effect: jsonpath(workflow.parameters['effects'], '$')[#],
                        resource: jsonpath(workflow.parameters['resources'], '$')[#],
                        action: jsonpath(workflow.parameters['actions'], '$')[#]
                      }
                    }
                  )
                )
              }}
        image: debian:9.4
        command: [bash]
        source: |
          echo "$POLICIES"

输出:

[{"action":"ec2:CreateTags","effect":"Allow","resource":"*"},{"action":"ec2:DescribeInstances","effect":"Allow","resource":"*"},{"action":"ec2:AuthorizeSecurityGroupIngress","effect":"Allow","resource":"*"}]

我建议不要使用表达式标签路线。这是一种鲜为人知的语言,对其他人来说更难维护。

【讨论】:

    猜你喜欢
    • 2023-01-11
    • 2022-01-04
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多