Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes argo loop through json array

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.

like image 353
Softey Avatar asked Jun 05 '19 09:06

Softey


1 Answers

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.

like image 111
user1458424 Avatar answered Nov 08 '22 08:11

user1458424