子字符串匹配条件时的 Argo 工作流

Argo workflow when a condition for substring match

如果字符串以特定子字符串开头,我想在 Argo 工作流中执行任务。 例如,我的字符串是 tests/dev-or.yaml 如果我的字符串以 tasks/

开头,我想执行任务

这是我的工作流程,但条件未得到正确验证

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: conditional-
spec:
  entrypoint: conditional-example
  arguments:
    parameters:
    - name: should-print
      value: "tests/dev-or.yaml"

  templates:
  - name: conditional-example
    inputs:
      parameters:
      - name: should-print
    steps:
    - - name: print-hello
        template: whalesay
        when: "{{inputs.parameters.should-print }} startsWith 'tests/'"

  - name: whalesay
    container:
      image: docker/whalesay:latest
      command: [sh, -c]
      args: ["cowsay hello"]

以下是我 运行 工作流程

时出现的错误
WorkflowFailed 7s workflow-controller  Invalid 'when' expression 'tests/dev-or.yaml startsWith 'tests/'': Unable to access unexported field 'yaml' in token 'or.yaml'

似乎在评估 when 条件时不接受 -.yaml/

我的工作流程有什么错误吗?使用此条件的正确方法是什么?

tl;dr - 使用这个:when: "'{{inputs.parameters.should-print}}' =~ '^tests/'"

参数替换发生在计算 when 表达式之前。所以 when 表达式实际上是 tests/dev-or.yaml startsWith 'tests/'。可以看到,第一个字符串需要引号。

但即使您有 when: "'{{inputs.parameters.should-print}}' startsWith 'tests/'"(添加了单引号),表达式也会失败并出现此错误:Cannot transition token types from STRING [tests/dev-or.yaml] to VARIABLE [startsWith].

Argo Workflows conditionals are evaluated as govaluate expressions. govaluate does not have any built-in functions,Argo Workflows 没有增加任何功能。所以startsWith没有定义。

相反,您应该使用 govaluate 的 regex comparator。表达式将如下所示:when: "'{{inputs.parameters.should-print}}' =~ '^tests/'".

这是功能性工作流程:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: conditional-
spec:
  entrypoint: conditional-example
  arguments:
    parameters:
      - name: should-print
        value: "tests/dev-or.yaml"
  templates:
    - name: conditional-example
      inputs:
        parameters:
          - name: should-print
      steps:
        - - name: print-hello
            template: whalesay
            when: "'{{inputs.parameters.should-print}}' =~ '^tests/'"
    - name: whalesay
      container:
        image: docker/whalesay:latest
        command: [sh, -c]
        args: ["cowsay hello"]