Interview Prep/Helm/Templating: Go Templates & Sprig

How do you include a whole YAML block from values only if it is set?

BeginnerConceptTemplating: Go Templates & Sprig
Answer

Combine with, toYaml and nindent:

      {{- with .Values.api.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.api.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}

Empty lists and maps skip the block entirely, so the rendered manifest stays clean and the server-side default applies.

All Helm questions Roadmap: Helm

More on Templating: Go Templates & Sprig