首页 > 解决方案 > 检查 Helm 中嵌套对象的存在

问题描述

我在 YAML 文件中有这些值:

configuration:
  files:
  - from: "foo.yml"
    to: "bar.yml"

我想检查是否configurationfiles存在这样的:

{{- if and .Values.configuration .Values.configuration.files -}}
volumeMounts:
  {{- range .Values.configuration.files -}}
  - name: "config"
    mountPath: {{ .to }}
  {{- end -}}
{{- end -}}

它在何时存在时工作得很好configurationfiles但是当两者都被省略时,我在渲染 Helm 模板时收到此错误:

错误:在“chart/templates/deployment.yaml”中呈现错误:模板:chart/templates/deployment.yaml:62:48:在 <.Values.configuration.files> 处执行“chart/templates/deployment.yaml”:nil指针评估接口 {}.files

如何在不嵌套两个if子句的情况下检查两者是否存在?

标签: kubernetes-helmgo-templates

解决方案


推荐阅读