pythonkubernetesargo-workflows

How to access a content of file which is passed as input artifact to a script template in argo workflows


I am trying to access the content(json data) of a file which is passed as input artifacts to a script template. It is failing with the following error NameError: name 'inputs' is not defined. Did you mean: 'input'?

My artifacts are being stored in aws s3 bucket. I've also tried using environment variables instead of directly referring the artifacts directly in script template, but it is also not working.

Here is my workflow

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: output-artifact-s3-
spec:
  entrypoint: main
  templates:
    - name: main
      dag:
        tasks:
          - name: whalesay-script-template
            template: whalesay

          - name: retrieve-output-template
            dependencies: [whalesay-script-template]
            arguments:
              artifacts:
                - name: result
                  from: "{{tasks.whalesay-script-template.outputs.artifacts.message}}"
            template: retrieve-output


    - name: whalesay
      script:
        image: python
        command: [python]
        env:
          - name: OUTDATA
            value: |
              {
              "lb_url" : "<>.us-east-1.elb.amazonaws.com",
              "vpc_id" : "<vpc-id",
              "web_server_count" : "4"
              }
        source: |
          import json
          import os
          OUTDATA = json.loads(os.environ["OUTDATA"])
          with open('/tmp/templates_lst.txt', 'w') as outfile:
            outfile.write(str(json.dumps(OUTDATA)))
        volumeMounts:
          - name: out
            mountPath: /tmp
      volumes:
        - name: out
          emptyDir: { }
      outputs:
        artifacts:
          - name: message
            path: /tmp


    - name: retrieve-output
      inputs:
        artifacts:
          - name: result
            path: /tmp
      script:
        image: python
        command: [python]
        source: |
          import json
          result = {{inputs.artifacts.result}}
          with open(result, 'r') as outfile:
            lines = outfile.read()
            print(lines)
          print('Execution completed')

What's wrong in this workflow?


Solution

  • In the last template, replace {{inputs.artifacts.result}} with ”/tmp/templates_lst.txt”.

    inputs.artifacts.NAME has no meaning in the source field, so Argo leaves it as-is. Python tries to interpret it as code, which is why you get an exception.

    The proper way to communicate an input artifact to Python in Argo is to specify the artifact destination (which you’ve done) in the templates input definition. Then in Python, use files from that path the same way you would do in any Python app.