Oli*_*ver 11 templates go-templates kubernetes-helm
我试图声明,如果变量为 true 或不存在(即 var 的默认值为 true),则应存在 helm 模板中的代码块。以下作品:
{{- if or .Values.livenessProbe (not (hasKey .Values "livenessProbe")) }}
...
{{- end }}
Run Code Online (Sandbox Code Playgroud)
这看起来很复杂,有没有更简单的?我尝试了default几种方法来使用函数,但它们都会导致忽略该值(存在或不存在,真或假,块总是被渲染):
{{- if (default true .Values.livenessProbe) }}
...
{{- end }}
Run Code Online (Sandbox Code Playgroud)
Oli*_*ver 13
请参阅https://helm.sh/docs/chart_template_guide/function_list/#default了解为什么不能按预期工作的解释default: boolean false 被视为“空”,因此当 value 为 false 时,默认值返回默认值,即忽略实际价值!
我还发现了https://github.com/helm/helm/issues/3308,这表明很多人都被这个绊倒了。看看该问题中的其他解决方案,我觉得我的(作为问题的一部分发布)实际上是最简单的,相当不幸。模式是这样的:
{{- if or .Values.myVar (not (hasKey .Values "myVar")) }}
...
{{- end }}
Run Code Online (Sandbox Code Playgroud)
这基本上是说“如果值为真则渲染块,或者如果因为键不存在而值为假则渲染块”。