0

假设使用相同的模板有两个不同的 argo 工作流程whalesay

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: workflow1
spec:
  # invoke the whalesay template with
  # "hello world" as the argument
  # to the message parameter
  entrypoint: whalesay
  arguments:
    parameters:
    - name: message
      value: hello world

  templates:
  - name: whalesay
    inputs:
      parameters:
      - name: message       
    container:
      # run cowsay with that message input parameter as args
      image: docker/whalesay
      command: [cowsay]
      args: ["{{inputs.parameters.message}}"]
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: workflow2
spec:
  entrypoint: hello-hello-hello

  templates:
  - name: hello-hello-hello
    # Instead of just running a container
    # This template has a sequence of steps
    steps:
    - - name: hello1            
        template: whalesay
        arguments:
          parameters:
          - name: message
            value: "hello1"
    - - name: hello2a           
        template: whalesay
        arguments:
          parameters:
          - name: message
            value: "hello2a"

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

有没有办法避免在两个不同的工作流程中重复(和维护)相同的模板?也许类似于 WorklowTemplate,例如:

apiVersion: ...
kind: TemplateTemplate
name: whalesay
inputs:
   parameters:
   - name: message
container:
   image: docker/whalesay
   command: [cowsay]
   args: ["{{inputs.parameters.message}}"]

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: workflow1
metadata:
  name:
spec:
  entrypoint: whalesay
  arguments:
    parameters:
    - name: message
      value: hello world
  templates:
  - templateRef: whalesay
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: workflow2
spec:
  entrypoint: hello-hello-hello

  templates:
  - name: hello-hello-hello
    # Instead of just running a container
    # This template has a sequence of steps
    steps:
    - - name: hello1            
        templateRef: whalesay
        arguments:
          parameters:
          - name: message
            value: "hello1"
    - - name: hello2a           
        templateRef: whalesay
        arguments:
          parameters:
          - name: message
            value: "hello2a"
4

1 回答 1

0

正如您猜对的那样,有一些WorkflowTemplates可以保存在 Argo 中,然后在其他 Workflows(或 WorkflowTemplates)中重复使用。您已经运行得差不多了,请参阅:

在 Argo 中,您可以在工作流模板部分添加模板(或使用命令行)。 在此处输入图像描述

在里面粘贴模板代码(如下),然后工作流(和其他工作流模板)可以发现这些代码。

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: whalesay-template
spec:
  entrypoint: runner
  arguments:
    parameters:
    - name: message
      value: hello world
  templates:
  - name: runner
    inputs:
      parameters:
      - name: message       
    container:
      image: docker/whalesay
      command: [ cowsay ]
      args: [ "{{inputs.parameters.message}}" ]

然后你可以在你的工作流(甚至工作流模板)中使用它。复制以下代码:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: workflow1-
  labels:
    env: "test"
spec:
  entrypoint: runner
  arguments:
    parameters:
    - name: outer-message
      value: hello CRUEL world
  templates:
    - name: runner
      steps:
      - - name: template-run
          templateRef:
            name: whalesay-template # template name from metadata
            template: runner # inside entrypoint name from template
          arguments:
            parameters:
              - name: message
                value: "{{workflow.parameters.outer-message}}"
      - - name: template-run2
          templateRef:
            name: whalesay-template
            template: runner
          arguments:
            parameters:
              - name: message
                value: hello MAD world

并将其粘贴到工作流提交表单中: 在此处输入图像描述

它将使用生成的名称创建一个工作流,完成后您应该看到: 在此处输入图像描述

当然,在日志中有预期的消息(从 Workflow 传递的消息,而不是 WorkflowTemplate 中使用的消息): 在此处输入图像描述 在此处输入图像描述

这样,您确实需要维护工作流程,但是公共部分仅在一个地方。

于 2021-11-20T19:52:29.017 回答