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

Kun*_* Ko 2 kubernetes-helm helm3

我有values.yaml一个

ingress:
  enabled: false 

volume:
  hostPath:
    path: /tmp
    type: DirectoryOrCreate 
Run Code Online (Sandbox Code Playgroud)

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

ingress:
  enabled: true 

volume:
  persistentVolumeClaim:
    claimName: test
Run Code Online (Sandbox Code Playgroud)

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

volume: 
  persistentVolumeClaim:
    claimName: test
  hostPath:
    path: /tmp
    type: DirectoryOrCreate 
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*aze 5

null特定的有效 YAML 值(与 JSON 相同null)。如果将 Helm 值设置为null,则 Gotext/template逻辑会将其解组为 Go nil,并且它将在语句和类似条件中显示为“false” if

volume:
  persistentVolumeClaim:
    claimName: test
  hostPath: null
Run Code Online (Sandbox Code Playgroud)

不过,我可能会在图表逻辑中避免这个问题。一种方法是使用单独的type字段来说明您要查找的子字段:

# the chart's defaults in values.yaml
volume:
  type: HostPath
  hostPath: { ... }
Run Code Online (Sandbox Code Playgroud)
# your `helm install -f` overrides
volume:
  type: PersistentVolumeClaim
  persistentVolumeClaim: { ... }
  # (the default hostPath: will be present but unused)
Run Code Online (Sandbox Code Playgroud)

第二个选项是使默认值“不存在”,要么完全禁用该功能,要么在图表代码中构造合理的默认值(如果不存在)。

# 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: ???
Run Code Online (Sandbox Code Playgroud)
# 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) */}}
Run Code Online (Sandbox Code Playgroud)

或者,始终提供某种存储,即使它没那么有用:

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 }}
Run Code Online (Sandbox Code Playgroud)