如何覆盖 Helm Chart 中的表/映射

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

我有一个

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-helm
1个回答
5
投票

null
特定的有效 YAML 值(与 JSON
null
相同)。 如果您将 Helm 值设置为
null
,则 Go
text/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 }}
© www.soinside.com 2019 - 2024. All rights reserved.