Kubernetes argo loop through json array
Asked Answered
P

1

6

I'm attempting to use Argo to loop through an array of JSON objects however the workflow is returning:

failed to resolve {{item}}

The workflow configuration is as follows:

- name: output-parameter
   steps:
    - - name: generate-parameter
        template: getresult
    - - name: consume-parameter
         template: print-result
         arguments:
           parameters:          
           - name: result
           value: "{{item}}"
         withParam: "{{steps.generate-parameter.outputs.result}}"      


 - name: getresult
   script:
     image: python:alpine3.6
     command: [python]
     source: |        
       import json
       import sys

       json.dump([{"name":"Foo"},{"name":"Doe"}], sys.stdout)

 - name: print-result
   inputs:
     parameters:
     - name: result
   container:
     image: crweu.azurecr.io/ubuntu_run:v1.0.6
     command: [sh, -c]
     args: ["echo {{inputs.parameters.result}}"]

If I used the example argo loop with:

json.dump([i for i in range(20, 31)], sys.stdout)

It prints out the number range without issues.

I assume the problem is due to the fact it's not a simple item it's an object and the {{item}} needs to change but I am unable to find any documentation on it.

Pendulous answered 5/6, 2019 at 9:58 Comment(0)
W
5

You need to adjust the arguments of the consume-parameter step to:

         arguments:
           parameters:          
           - name: result
             value: "{{item.name}}"
         withParam: "{{steps.generate-parameter.outputs.result}}"      

This is because Argo is parsing the JSON output of the getresult step.

Whitening answered 12/8, 2019 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.