【问题标题】:Argo workflow when a condition for substring match子字符串条件匹配时的 Argo 工作流程
【发布时间】:2022-01-15 06:25:25
【问题描述】:

如果字符串以特定子字符串开头,我想在 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/

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

【问题讨论】:

    标签: kubernetes conditional-statements argo-workflows argocd


    【解决方案1】:

    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 工作流conditionals 被评估为govaluate 表达式。 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"]
    

    【讨论】:

    • 这个条件在我的工作流程中不起作用,它给出了这个错误'Invalid ''when'' expression ''tests/dev-or.yaml =~ ''tests/dev-or.yaml'''': Unable to access unexported field ''yaml'' in token ''or.yaml'''
    • 你能粘贴你正在使用的确切表达式吗?
    • 这里是表达式 am using when: "'{{item}}' =~ '^tests/'" 在循环中运行这个 withParam
    • 这对我来说看起来不错……但它看起来一点也不像它与错误消息匹配。对于循环的失败迭代,您是否有 {{item}} 的原始字符串表示形式?也许变量中的某些东西我们搞砸了表达式。
    • 让我分享正在循环中的字符串
    猜你喜欢
    • 2020-03-03
    • 2015-12-23
    • 1970-01-01
    • 2022-07-07
    • 2022-01-04
    • 1970-01-01
    • 2022-08-16
    • 2022-06-10
    • 2020-08-29
    相关资源
    最近更新 更多