Interview Prep/Helm/Templating: Go Templates & Sprig

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.

All Helm questions Roadmap: Helm

More on Templating: Go Templates & Sprig