答案基于this。因此是 CW。
您可以使用_*.tpl 文件来定义通用模板,它们位于./templates/_*.tpl(. 是具有全局 Chart.yaml 和 values.yaml 的目录)。同样默认在 helm 全局值覆盖本地值。可以在here找到解决方案。
通过结合使用这两种技术,您可以制作通用模板,并且只使用 values.yaml 来呈现您想要呈现的内容。
例如:
# values.yaml
global:
defaults:
switches:
volumesEnabled: false
ingressEnabled: false
ingress:
host: "generic-host.com"
volumes:
volumeName: "generic-volume-name"
subchart1:
defaultOverrides:
switches:
volumesEnabled: true
volumes:
volumeName: "not-so-generic-name"
subchart2:
defaultOverrides:
switches:
volumesEnabled: true
ingressEnabled: true
然后是模板(java只是为了将模板归为一类,你可以尝试猜测我的后端微服务是用哪种语言编写的:))
# ./templates/java/_deployment.tpl
{{- define "templates.java.deployment" }}
{{- $properties := merge .Values.defaultOverrides $.Values.global.defaults -}}
{{*/ generic deployment structure */}}
{{- if $properties.switches.volumesEnabled -}}
volume: {{ $properties.volumes.volumeName }}
{{- end }}
{{*/ generic deployment structure */}}
{{- end }}
# ./templates/java/_ingress.tpl
{{- define "templates.java.ingress" }}
{{- $properties := merge .Values.defaultOverrides $.Values.global.defaults -}}
{{- if $properties.switches.ingressEnabled -}}
host: {{ $properties.ingress.host }}
{{*/ generic ingress structure */}}
{{- end }}
{{- end }}
然后是子图模板
# ./charts/subchart1/templates/deployment.yaml
{{ include "templates.java.deployment" . }}
./charts/subchart1/templates/ingress.yaml:
{{ include "templates.java.ingress" . }}
subchart2 has exactly the same includes.
最后我们会有:
- 子图 1:
- 已部署
- volumeName 被本地值覆盖为“not-so-generic-name”
- 根本不渲染入口
- 子图2:
- 已部署
- volumeName 是全局值的默认值
- 入口主机是全局值的默认值
但我会说,泛化太多是一种不好的做法,因为它会使您的模板过于复杂。在我的情况下,我发现了 2 个不同的组,它们在其中具有几乎相同的清单(基本上是前端和后端),并为每个组创建了一组 _*.tpl 文件,并分别在全局 values.yaml 中为每个组设置默认值。