从我的 Argo 工作流中的 ConfigMap 中获取值

Getting a value from a ConfigMap in my Argo Workflow

提问人:Daniel 提问时间:11/16/2023 最后编辑:Daniel 更新时间:11/16/2023 访问量:23

问:

我的 Argo Workflow 有一个模板,用于生成以下配置映射:

{
  "apiVersion": "v1",
  "kind": "ConfigMap",
  "metadata": { "name": "configmap" },
  "data":
    {
      "ELASTIC_SEARCH_URL": "https://staging-aaa.domain.com",
      "EXTENSION_PATH": "/dist",
      "GRAPHQL_GATEWAY_URL": "https://graphql-gateway.stg.domain.com/graphql",
      "bucket_url": "stg-bucket/fie/to/path",
    },
}

我在我的其他模板之一中使用了这个值,如下所示:

...
envFrom:
- configMapRef:
    name: "{{inputs.parameters.configmap}}"

我还想通过进入该输出来获得“二合一”,所以我创建了这个模板来测试我是否能够打印我想要的东西(我不能覆盖原始输出,所以我添加了):bucket_urlwithParam[]{{steps.create-configmap.outputs.parameters.configmap}}

- - name: print-with-loop
    template: print-with-loop-tmpl
    arguments:
      parameters:
        - name: data
          value: "{{item}}"
    withParam: "[{{steps.create-configmap.outputs.parameters.configmap}}]"

此模板的输出正是 Config Map 本身:

{apiVersion:v1,data:{ELASTIC_SEARCH_URL:https://staging-aaa.domain.com,EXTENSION_PATH:/dist,GRAPHQL_GATEWAY_URL:https://graphql.stg.domain.com/graphql,bucket_url:stg-bucket/fie/to/path},kind:ConfigMap,metadata:{name:env-configmap}}

我也可以在该配置映射中打印:item.data

{ELASTIC_SEARCH_URL:https://staging-aaa.domain.com,EXTENSION_PATH:/dist,GRAPHQL_GATEWAY_URL:https://graphql.stg.domain.com/graphql,bucket_name:stg-bucket,ext_path:extension/stable/dist-raw.tar.gz,bucket_url:stg-bucket/extension/stable/dist-raw.tar.gz}

但是,我无法访问 .如果我使用 or ,它不起作用,并且我收到来自 Argo 的错误。item.dataitem.data.bucket_urlitem.data['bucket_url']

我尝试使用 sprig 操作输出,但我无法找到解决方案。基本上,我正在尝试获取以在此工作流程中的另一个模板中使用。bucket_url

自行重现问题

  1. 运行 Argo 服务器
  2. 创建工作流 yaml 文件
  3. 使用新的工作流文件运行。argo submit
  4. 就是这样:)

我制作了最小的模板,应该产生完全相同的结果。如果你像我一样在本地运行 Argo,也许可以尝试一下:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: test-
spec:
  entrypoint: main
  arguments:
    parameters:
      - name: cluster
        value: "stg"
      - name: version
        value: "stg/stable"

  templates:
    - name: main
      steps:
        - - name: create-configmap
            template: create-configmap-tmpl

        - - name: print-with-loop
            template: print-with-loop-tmpl
            arguments:
              parameters:
                - name: data
                  value: "{{item}}"
            withParam: "[{{steps.create-configmap.outputs.parameters.configmap}}]"

    - name: create-configmap-tmpl
      inputs:
        artifacts:
          - name: python-script
            path: /tmp/script.py
            raw:
              data: |
                import json

                def create_simple_config_map():
                    config = {
                        "ELASTIC_SEARCH_URL": "https://example-domain.com",
                        "GRAPHQL_GATEWAY_URL": "https://graphql.example.com/graphql",
                        "bucket_name": "example-bucket",
                        "ext_path": "example/path/file",
                        "bucket_url": "example-bucket/example/path/file"
                    }
                    
                    return config

                def main():
                    config = create_simple_config_map()

                    configmap = {
                        "apiVersion": "v1",
                        "kind": "ConfigMap",
                        "metadata": {
                            "name": "env-configmap"
                        },
                        "data": config
                    }

                    with open("/tmp/configmap.json", "w") as file:
                        json.dump(configmap, file, indent=4)

                    print(json.dumps([config], indent=4))

                if __name__ == "__main__":
                    main()
      container:
        image: python:3.11
        command: ["python", "/tmp/script.py"]
      outputs:
        parameters:
          - name: configmap
            valueFrom:
              path: /tmp/configmap.json

    - name: print-with-loop-tmpl
      inputs:
        parameters:
          - name: data
      script:
        image: bash
        command: [bash]
        source: |
          echo "{{inputs.parameters.data}}"

该步骤会生成一个有效的 Config Map,您也可以在本地运行它:create-configmap-tmpl

import json

def create_simple_config_map():
    config = {
        "ELASTIC_SEARCH_URL": "https://example-domain.com",
        "GRAPHQL_GATEWAY_URL": "https://graphql.example.com/graphql",
        "bucket_name": "example-bucket",
        "ext_path": "example/path/file",
        "bucket_url": "example-bucket/example/path/file"
    }
    
    return config

def main():
    config = create_simple_config_map()

    configmap = {
        "apiVersion": "v1",
        "kind": "ConfigMap",
        "metadata": {
            "name": "configmap"
        },
        "data": config
    }

    with open("/tmp/configmap.json", "w") as file:
        json.dump(configmap, file, indent=4)

    print(json.dumps([config], indent=4))

if __name__ == "__main__":
    main()

此脚本的输出如下:

{
    "apiVersion": "v1",
    "kind": "ConfigMap",
    "metadata": {
        "name": "configmap"
    },
    "data": {
        "ELASTIC_SEARCH_URL": "https://example-domain.com",
        "GRAPHQL_GATEWAY_URL": "https://graphql.example.com/graphql",
        "bucket_name": "example-bucket",
        "ext_path": "example/path/file",
        "bucket_url": "example-bucket/example/path/file"
    }
}

您现在可以尝试使用打印:

- - name: print-with-loop
    template: print-with-loop-tmpl
    arguments:
    parameters:
        - name: data
        value: "{{item}}"
    withParam: "[{{steps.create-configmap.outputs.parameters.configmap}}]"
  • 如果我们使用 ,它会打印整个 Config Mapitem
  • 如果我们使用 ,它也会起作用。item.data

问题在于访问 或 .这是行不通的。 如前所述,我尝试了各种功能,例如列表和字典操作,但没有任何效果。item.data.bucket_urlitem.data['bucket_url']sprigtoJson

yaml go-templates argo

评论


答:

0赞 Daniel 11/16/2023 #1

我最终使用了不同的参数:withParam

        - - name: print-with-loop
            template: print-with-loop-tmpl
            arguments:
              parameters:
                - name: data
                  value: "{{item.bucket_url}}"
            withParam: "{{steps.create-configmap.outputs.result}}"

由于我在生成 ConfigMap 的脚本中打印了我正在执行的操作,因此我可以轻松访问 .上面模板的输出正是我所需要的。json.dumpbucket_url