Search code examples
kubernetes-helmgo-templates

How to add a comment to a helm chart template?


In helm templates, what's the best way to add a comment to a template file? For example following the helm charts getting started guide provides the template below. I want to add a comment describing what this template is for.

apiVersion: v1
kind: ConfigMap
metadata:
  name: mychart-configmap
data:
  myvalue: "Hello World"

I assume there is a way to do this in YAML or using the templating engine?


Solution

  • There is a section in the Helm documentation on Comments (YAML Comments vs. Template Comments); type comments into the search box at the top left of https://docs.helm.sh/docs/. Also see the Go library reference documentation for the text/template package.

    The Go text/template engine has its own comment syntax, {{/* comment here */}}. These may be multiline comments, and may be combined with ordinary whitespace controls, {{- /* whatever */ -}}.

    You can also use YAML comment syntax, # to the end of the line. This will appear in places like helm template output.

    The important difference between these two syntaxes is that {{/* ... */}} comments are applied before the rest of the templating is applied, but # ... comments are after. This can make a difference if you're trying to comment out a block that would generate multiple lines.

    # ordinarily:
    labels: {{- include "mychart.labels" . | nindent 2 }}
    
    # RIGHT: generates nothing
    {{/* labels: {{- include "mychart.labels" . | nindent 2 }} */}}
    
    # WRONG: omits "labels:" but then incorrectly includes the actual generated labels
    # labels: {{- include "mychart.labels" . | nindent 2 }}