null
是一个特定的有效 YAML 值(与 JSON 相同null
)。如果您将 Helm 值设置为null
,则 Gotext/template
逻辑会将其解组为 Go nil
,并且它将在if
语句和类似条件中显示为“false”。
volume:
persistentVolumeClaim:
claimName: test
hostPath: null
不过,我可能会在图表逻辑中避免这个问题。一种方法是使用单独的type
字段来说明您要查找的子字段:
# the chart's defaults in values.yaml
volume:
type: HostPath
hostPath: { ... }
# your `helm install -f` overrides
volume:
type: PersistentVolumeClaim
persistentVolumeClaim: { ... }
# (the default hostPath: will be present but unused)
第二种选择是使默认值“不存在”,或者完全禁用该功能,或者在图表代码中构建合理的默认值(如果它不存在)。
# values.yaml
# volume specifies where the application will keep persistent data.
# If unset, each replica will have independent data and this data will
# be lost on restart.
#
# volume:
#
# persistentVolumeClaim stores data in an existing PVC.
#
# persistentVolumeClaim:
# name: ???
# deep in templates/deployment.yaml
volumes:
{{- if .Values.volume.persistentVolumeClaim }}
- name: storage
persistentVolumeClaim:
claimName: {{ .Values.volume.persistentVolumeClaim.name }}
{{- else if .Values.volume.hostPath }}
- name: storage
hostPath:
path: {{ .Values.volume.hostPath.path }}
{{- end }}
{{-/* (just don't create a volume if not set) */}}
或者,总是提供某种存储,即使它不是那么有用:
volumes:
- name: storage
{{- if .Values.volume.persistentVolumeClaim }}
persistentVolumeClaim:
claimName: {{ .Values.volume.persistentVolumeClaim.name }}
{{- else if .Values.volume.hostPath }}
hostPath:
path: {{ .Values.volume.hostPath.path }}
{{- else }}
emptyDir: {}
{{- end }}