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

Bir*_*iru 2 conditional-statements kubernetes argocd argo-workflows

如果字符串以特定子字符串开头,我想在 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"]
Run Code Online (Sandbox Code Playgroud)

以下是我运行工作流程时出现的错误

WorkflowFailed 7s workflow-controller  Invalid 'when' expression 'tests/dev-or.yaml startsWith 'tests/'': Unable to access unexported field 'yaml' in token 'or.yaml'
Run Code Online (Sandbox Code Playgroud)

似乎它不接受-, .yamland/同时评估 when 条件。

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

Mic*_*haw 6

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条件被评估为govaluate表达式。govaluate没有任何内置函数,Argo Workflows 也没有为其添加任何函数。所以startsWith没有定义。

相反,您应该使用 govaluate 的正则表达式比较器。表达式将如下所示: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"]
Run Code Online (Sandbox Code Playgroud)