How do you render a value only when the key exists, even if it is `false` or `0`?
AdvancedConceptTemplating: Go Templates & Sprig
Answer
Use hasKey on the parent map, because if and default treat false and 0 as unset:
{{- if hasKey .Values.api "automountServiceAccountToken" }}
automountServiceAccountToken: {{ .Values.api.automountServiceAccountToken }}
{{- end }}
{{- if hasKey .Values.api "replicaCount" }}
replicas: {{ .Values.api.replicaCount }}
{{- end }}
The second example is important when HPA manages replicas: omitting replicas stops Helm from resetting the HPA's scaling on every upgrade.
More on Templating: Go Templates & Sprig
What template language does Helm use?BeginnerWhat does the dot (`.`) mean in a Helm template?BeginnerWhat is `$` and when do you need it?IntermediateHow does whitespace control (`{{-` and `-}}`) work?BeginnerHow do `if`, `else if` and `else` evaluate truthiness?BeginnerWhat do `and`, `or`, `not`, `eq`, `ne`, `lt`, `gt` look like in Helm?Beginner