【问题标题】:Dynamically provide an image name for the container template为容器模板动态提供图像名称
【发布时间】:2022-01-14 06:26:14
【问题描述】:

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

我们有 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"]

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

还有一个文档描述了可以在哪些字段中使用工作流变量? Documentation page 只说:

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

【问题讨论】:

    标签: argo-workflows argo


    【解决方案1】:

    绝对支持您尝试做的事情。只需将{{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"]
    

    目前没有列出可模板字段的文档。但是有一个 open issue 用于添加该文档。

    【讨论】:

      【解决方案2】:

      这是一种可能的解决方法:使用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"
      

      【讨论】:

      • 您是否找到了不必创建所有这些步骤的方法?我想根据我的工作流程中的一些参数对动态图像运行相同的步骤。
      • @JDK92 是的,请检查接受的答案。 Argo 提供了这个功能。
      猜你喜欢
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 2010-12-10
      相关资源
      最近更新 更多