Location>code7788 >text

ArgoWorkflow Tutorial (VI) - Seamless implementation of inter-step parameter passing

Popularity:258 ℃/2024-10-10 14:04:18

Previously we analyzed how to pass parameters between Workflow, WorkflowTemplate and template.

This article focuses on analyzing the implementation of parameter passing between different steps in the same Workflow, such as using the output of the previous step as the result of the next step (instead of passing it as a file).

1. General

Then it is only previously analyzed how to pass parameters between Workflow, WorkflowTemplate, template 3, today continue to analyze how to pass parameters between steps.

To implement inter-step parameter passing, two functions need to be implemented:

  • 1) Export results

  • 2) Import parameters

Based on previous knowledge, one conceivable way to implement these two features is to use artifact:

  • Export results: write the parameters to a file and save it to s3 as artifact
  • Importing parameters: the next step downloads the artifact and takes the parameters from it.

It does work, but it's kind of crappy, because the artifact is mainly for saving files. argoworkflow also provides the corresponding feature directly for you to use.

2. Inter-step parameterization

  • Export results as Output Parameter
  • Import the Output Parameter of the previous step into the Input Parameter of the current step.

The full demo is below:

apiVersion: /v1alpha1
kind: Workflow
metadata:
  generateName: output-parameter-
spec:
  entrypoint: output-parameter
  templates:
  - name: output-parameter
    steps:
    - - name: generate-parameter
        template: whalesay
    - - name: consume-parameter
        template: print-message
        arguments:
          parameters:
          # Pass the hello-param output from the generate-parameter step as the message input to print-message
          - name: message
            value: "{{-param}}"

  - name: whalesay
    container:
      image: docker/whalesay:latest
      command: [sh, -c]
      args: ["echo -n hello world > /tmp/hello_world.txt"]  # generate the content of hello_world.txt
    outputs:
      parameters:
      - name: hello-param  # name of output parameter
        valueFrom:
          path: /tmp/hello_world.txt # set the value of hello-param to the contents of this 

  - name: print-message
    inputs:
      parameters:
      - name: message
    container:
      image: docker/whalesay:latest
      command: [cowsay]
      args: ["{{}}"]

Export results

- name: whalesay
    container:
      image: docker/whalesay:latest
      command: [sh, -c]
      args: ["echo -n hello world > /tmp/hello_world.txt"]  # generate the content of hello_world.txt
    outputs:
      parameters:
      - name: hello-param  # name of output parameter
        valueFrom:
          path: /tmp/hello_world.txt # set the value of hello-param to the contents of this 

The first step is the content of the step, here, for simplicity, there is only an echo command, which writes the result (hello world) to the file

/tmp/hello_world.txt Center.

And then there's the result everywhere.

    outputs:
      parameters:
      - name: hello-param  # name of output parameter
        valueFrom:
          path: /tmp/hello_world.txt # set the value of hello-param to the contents of this 

defines an output parameter, named hello-param, whose value starts at/tmp/hello_world.txt file, the final value will be the value that was written to thehello world

At this point, we have exported the result of the current step into an Output Parameter that can be used in subsequent steps.

Import parameters

The next step is actually very simple, the same as the normal step, through the Input Parameter definition of parameters, and then in the use of the use of syntax through the{{}} A citation will suffice.

  - name: print-message
    inputs:
      parameters:
      - name: message
    container:
      image: docker/whalesay:latest
      command: [cowsay]
      args: ["{{}}"]

The only difference is the source of the parameter. Previously, we had defined the parameter directly in Workflow, but here we need to change it to refer to the Output Parameter exported in the previous step, like this:

spec:
  entrypoint: output-parameter
  templates:
  - name: output-parameter
    steps:
    - - name: generate-parameter
        template: whalesay
    - - name: consume-parameter
        template: print-message
        arguments:
          parameters:
          # Pass the hello-param output from the generate-parameter step as the message input to print-message
          - name: message
            value: "{{-param}}"

directly references the Output Parameter from the previous step, with syntax of{{steps.$.$parameterName}}

Previously, we exported the results in a step called generate-parameter, and then the exported parameter was called hello-param, so here we'll just use the{{-param}} to reference the parameter.

The built-in result parameter

In addition to our manually exported parameters, ArgoWorkflow generates an Output Parameter by default, which is result.

As with other Output Parameters, this can be done via the{{steps.$.$parameterName}} Syntax for referencing.

The result parameter captures up to 256KB of standard output as value, so it can contain the following:

  • 1) The result of the script
  • 2) Standard output of the container
  • 3)...

The contents can be captured by result as long as they are output to standard output in the container.


[ArgoWorkflow Series]Continuously updated, search the public number [Explore Cloud Native]Subscribe to read more articles.


3. Summary

This article analyzes how to pass parameters in Workflow in Argo is still relatively simple:

  • (1) Export parameters through Output Parameter
  • 2) Refer to the parameters exported in the previous step in the

Finally, it introduces the built-in result Output Parameter, which can be used to get the standard output from the container.