Home > database >  how to pass a multi line json string in map in argo workflows
how to pass a multi line json string in map in argo workflows

Time:12-01

I am working on creating a argo workflow with a loop withParam for map variable. In this map I want to use multi line json string. Is there any way to use it?

Here is the way am using policy as multi line string but it is not working

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]

Is there any way in argo workflows to achieve this? if not, what's the alternate way to do this?

CodePudding user response:

There are three issues:

  1. The policies are not valid JSON. The asterisks must be quoted since they are strings.
      {
        "Sid": "BucketAdmin",
        "Effect": "Allow",
    -   "Principal": *,
        "Principal": "*",
        "Action": "s3:*",
    -   "Resource": *
        "Resource": "*"
      }
    
  2. (UPDATE: this change isn't actually necessary - Argo Workflows properly handles the policy JSON object.) The policies should be encoded as strings so they can be passed easily as parameters. For example:
    "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"BucketAdmin\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:*\",\"Resource\":\"*\"}]}"
    
  3. The policy value needs to be explicitly passed as a parameter to the cat-os-release template.

The final working workflow should look like this:

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}}"]

CodePudding user response:

The solutions provided by Michael works and also the below solution works for you with some readability of the policy

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]
  • Related