首页 > 解决方案 > 使用 toYaml 进行 Helm Chart 配置图模板化

问题描述

我有values.yml文件,其中包含具有以下格式的 mountPaths 列表:

global:
  mountPath:
    hello:
      config: /etc/hello/hello.conf
      node: /opt/hello/node.jks
      key: /opt/hello/key.jks
      cert: /opt/hello/cert.jks

我希望生成的渲染模板是

      volumeMounts:
        - name: config
          mountPath: /etc/hello/hello.conf
          subPath: config
        - name: node
          mountPath: /opt/hello/node.jks
          subPath: node
        - name: key
          mountPath: /opt/hello/key.jks
          subPath: key
        - name: cert
          mountPath: /opt/hello/cert.jks
          subPath: cert

我将如何做到这一点?我在deployment.yaml模板文件中尝试了以下内容:

      volumeMounts:
      {{- range $key, $value := pluck .Values.service_name .Values.global.mountPath.serviceName | first }}
        - name: {{ $key }}
          mountPath: $value
          subPath: {{ $key }}
      {{- end }}

我已经运行了以下 helm 命令,但它对我不起作用。如何根据输入完成获取我想要的上述格式?

helm upgrade --install \
  --namespace ${NAMESPACE} \
  --set service_name=hello \
  --set namespace=${NAMESPACE} \
  hello . \
  -f values.yaml \

标签: kubernetesyamlkubernetes-helm

解决方案


这是我所做的:

      volumeMounts:
      {{- range $key, $value := pluck .Values.service_name .Values.global.mountPath | first }}
        - name: {{ $key }}
          mountPath: {{  $value }}
          subPath: {{ $key }}
      {{- end }}

helm template --set service_name=hello [...] 似乎完全呈现你想要的。

请注意,我更改了带有 mountPath 字段的行: $value-> {{ $value }},以及带有 range: .Values.global.mountPath.serviceName->的行.Values.global.mountPath


推荐阅读