首页 > 解决方案 > 如何覆盖 Helm Chart 中的表/映射

问题描述

我有values.yaml一个

ingress:
  enabled: false 

volume:
  hostPath:
    path: /tmp
    type: DirectoryOrCreate 

我有一个overlay.yaml改变values.yaml.

ingress:
  enabled: true 

volume:
  persistentVolumeClaim:
    claimName: test

对于入口,它像我怀疑的那样工作,因为值enabled将变为 true。但是,对于该卷,似乎表是相互添加而不是被覆盖的。例如,我会得到类似的东西:

volume: 
  persistentVolumeClaim:
    claimName: test
  hostPath:
    path: /tmp
    type: DirectoryOrCreate 

我想在 values.yaml 中指定默认的卷类型及其配置(例如路径),但其他人可以自由地通过覆盖来更改它。但是,我现在“添加”了一个卷类型,而不是覆盖它。有没有办法做到这一点?

标签: kubernetes-helmhelm3

解决方案


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 }}

推荐阅读