2

有没有办法根据输入参数为容器模板动态提供图像名称?

我们有 30 多个不同的任务,每个任务都有自己的图像,并且应该在工作流中以相同的方式调用。根据前一个任务的输出,每次运行的数量可能会有所不同。所以我们不想甚至不能在工作流 YAML 中硬编码它们。

一个简单的解决方案是根据输入参数为容器提供图像字段,并为每个任务提供相同的模板。但看起来这是不可能的。此工作流程不起作用:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: hello-world-
spec:
  entrypoint: whalesay

  templates:
  - name: whalesay
    inputs:
      parameters:
        - name: image
          default: whalesay:latest
    container:
      image: "docker/{{image}}"
      command: [cowsay]
      args: ["hello world"]

这种特殊情况有一些解决方法吗?

还有一个文档描述了可以在哪些字段中使用工作流变量?文档页面仅显示:

工作流规范中的某些字段允许由 Argo 自动替换的变量引用。

4

2 回答 2

2

绝对支持您尝试做的事情。只需更改{{image}}为完全限定的变量名{{inputs.parameters.image}}

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: hello-world-
spec:
  entrypoint: whalesay
  templates:
  - name: whalesay
    inputs:
      parameters:
        - name: image
          default: whalesay:latest
    container:
      image: "docker/{{inputs.parameters.image}}"
      command: [cowsay]
      args: ["hello world"]

目前没有列出可模板字段的文档。但是添加该文档存在一个未解决的问题。

于 2022-01-15T13:55:36.633 回答
0

这是一种可能的解决方法:使用when和有条件地运行任务。我们需要列出所有可能的任务及其容器图像:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: test-dynamic-image-
spec:
  entrypoint: main

  templates:
  - name: main
    steps:
    - - name: fanout-step
        template: fanout
    - - name: loop-step
        template: options
        arguments:
          parameters:
          - name: code
            value: "{{item}}"
        withParam: "{{steps.code-step.outputs.parameters.codes}}"

  - name: fanout
    script:
      image: python:alpine3.6
      command: [python]
      source: |
        import json
        with open("/tmp/codes.json", 'w') as wf:
          json.dump(["foo", "bar", "buz"], wf)
    outputs:
      parameters:
        - name: codes
          valueFrom:
            path: /tmp/codes.json

  - name: foo-code
    script:
      image: python:alpine3.6
      command: [ python ]
      source: |
        print("foo-code")

  - name: bar-code
    script:
      image: python:alpine3.6
      command: [ python ]
      source: |
        print("bar-code")

  - name: buz-code
    script:
      image: python:alpine3.6
      command: [ python ]
      source: |
        print("buz-code")

  - name: missed-code
    script:
      image: python:alpine3.6
      command: [ python ]
      source: |
        print("THIS SHOULD NOT BE PRINTED")

  - name: options
    inputs:
      parameters:
        - name: code
    steps:
    - - name: foo-code-option
        template: foo-code
        when: "{{inputs.parameters.code}} == foo"
      - name: bar-code-option
        template: bar-code
        when: "{{inputs.parameters.code}} == bar"
      - name: buz-code-option
        template: buz-code
        when: "{{inputs.parameters.code}} == buz"
      - name: missed-code-option
        template: missed-code
        when: "{{inputs.parameters.code}} == missed"
于 2022-01-14T11:29:51.690 回答