在 Helm 中合并两个字典

问题描述 投票:0回答:1

我正在使用Helm 3。我有两个

values.yaml
文件。在
common/values.yaml
中我定义了:

deployment:
  ports:
    - name: http
      protocol: TCP

common
属于
library
类型。在类型为
my-app
application
中,
common
作为依赖项添加。在
my-app/values.yaml
我添加了:

deployment:
  ports:
    - containerPort: 8081

我在

_deployment.yaml
中定义了一个模板
common/templates
。在此文件中,我尝试使用以下方法将这两个
deployment
字典合并为一个:

{{- $deployment := merge .Values.common.deployment .Values.deployment -}}

当我打印

{{ $deployment }}
时,它给出输出:

map[ports:[map[containerPort:8080 name:http protocol:TCP]]]

如果我这样做:

{{- $deployment := merge .Values.deployment .Values.common.deployment -}}

{{ $deployment }}
的输出为:

map[ports:[map[containerPort:8081]]]

此外

{{ .Values.common.deployment }}
的输出是:

map[ports:[map[name:http protocol:TCP]]]

{{ .Values.deployment }}
的输出是:

map[ports:[map[containerPort:8081]]]

合并后我想要的是:

deployment:
  ports:
    - name: http
      protocol: TCP
      containerPort: 8081

您能提供的任何建议将不胜感激。

kubernetes-helm go-templates sprig-template-functions
1个回答
0
投票

看起来合并操作在列表上没有按预期工作(这是一个常见问题,因为合并操作在列表上不明确:合并时应该追加还是替换列表?)

无论如何,我建议将端口数据合并为:

{{- $ports := merge .Values.deployment.ports[0] .Values.common.deployment.ports[0] -}}

并用以下方式渲染结果:

  deployment:
    ports:
      - {{- toJson $ports }}

HTH

© www.soinside.com 2019 - 2024. All rights reserved.