TExcelTAD
Home › Kubernetes Packaging: Helm & Kustomize › Linting & Debugging Templates

Linting & Debugging Templates

Template bugs fall into three groups: the template doesn't execute (a missing key, a wrong function argument), it executes but produces invalid YAML (indentation), or it produces valid YAML that is the wrong Kubernetes object (a field at the wrong level). Helm catches the first two; kubeconform and your own eyes catch the third. This lesson is a field guide to all three.

Invalid YAML: see what Helm rendered

When rendering produces broken YAML, Helm refuses to print it and reports the YAML parser's complaint. --debug prints the raw rendered text anyway, which is usually enough to spot the problem:

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.9.0\n' > Chart.yaml
cat > values.yaml <<'EOF'
resources:
  requests: {cpu: 100m, memory: 64Mi}
EOF
cat > templates/deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
spec:
  selector: {matchLabels: {app: linkly}}
  template:
    metadata: {labels: {app: linkly}}
    spec:
      containers:
        - name: linkly
          image: ghcr.io/acme/linkly:1.4.0
          resources:
{{ toYaml .Values.resources | indent 8 }}
EOF
helm template web . 2>&1 | head -2
echo "--- with --debug, the rendered text:"
helm template web . --debug 2>/dev/null | sed -n '/containers:/,$p'
Error: YAML parse error on linkly/templates/deployment.yaml: error converting YAML to JSON: yaml: line 13: did not find expected '-' indicator

--- with --debug, the rendered text:
      containers:
        - name: linkly
          image: ghcr.io/acme/linkly:1.4.0
          resources:
        requests:
          cpu: 100m
          memory: 64Mi

indent 8 placed requests: at column 8, shallower than resources: at column 10 and level with the - name: list item, so the parser expected another list item (-) there. The line number in the message counts lines of the rendered document, not of your template file. The fix is {{- toYaml .Values.resources | nindent 12 }}, which puts the block one level below resources:, as in Templates & Pipelines.

Valid YAML, wrong object

Now indent too far instead: requests ends up nested where Kubernetes doesn't expect it. The YAML is valid, so Helm is happy, and only schema validation notices:

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.9.0\n' > Chart.yaml
printf 'resources:\n  requests: {cpu: 100m}\n' > values.yaml
cat > templates/deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
spec:
  selector: {matchLabels: {app: linkly}}
  template:
    metadata: {labels: {app: linkly}}
    spec:
      containers:
        - name: linkly
          image: ghcr.io/acme/linkly:1.4.0
          resources: {}
          {{- toYaml .Values.resources | nindent 10 }}
EOF
helm lint . --quiet && echo "helm lint: no complaints"
helm template web . | kubeconform -strict -summary
helm lint: no complaints
stdin - Deployment web is invalid: problem validating schema. Check JSON formatting: jsonschema validation failed with 'file:///opt/k8s-schemas/v1.34.12-standalone-strict/deployment-apps-v1.json#' - at '/spec/template/spec/containers/0': additional properties 'requests' not allowed
Summary: 1 resource found parsing stdin - Valid: 0, Invalid: 1, Errors: 0, Skipped: 0

The block landed at the container level, so requests became a field of the container. -strict rejects unknown fields. Without that check the mistake would surface late: helm install validates objects against the cluster's OpenAPI schema and fails at deploy time, and tools that apply with lenient field validation drop the unknown field with only a warning, leaving linkly without its CPU request. Always pipe helm template through kubeconform in CI (Validating with kubeconform).

helm lint

helm lint renders the chart with its default values (plus any -f/--set you give) and checks:

  • Chart.yaml fields (name, SemVer version, icon recommended);
  • that templates execute and produce parseable YAML;
  • the values schema;
  • basic object sanity: names valid for Kubernetes, deprecated API versions for the target Kubernetes version (--kube-version).

Messages come in three levels. --strict turns warnings into failures, which is what CI should use, and --with-subcharts lints dependencies too:

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.9.0\nicon: https://example.com/linkly.png\n' > Chart.yaml
printf 'name: Linkly_Config\n' > values.yaml
cat > templates/configmap.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.name }}
data: {}
EOF
helm lint . > lint.txt 2>&1
echo "exit code: $?"
cut -c1-120 lint.txt
helm lint . --strict > /dev/null 2>&1
echo "exit code with --strict: $?"
helm lint . --strict --set name=linkly-config && echo "fixed"
exit code: 0
==> Linting .
[WARNING] templates/configmap.yaml: object name does not conform to Kubernetes naming requirements: "Linkly_Config": met

1 chart(s) linted, 0 chart(s) failed
exit code with --strict: 1
==> Linting .

1 chart(s) linted, 0 chart(s) failed
fixed

The warning (cut to 120 characters here) doesn't fail a plain lint; with --strict the same chart fails until the name is valid.

A debugging toolbox

Problem Tool
see one template's output helm template web . -s templates/deployment.yaml
see output that doesn't parse helm template web . --debug
see the final merged values a temporary {{ toYaml .Values }} template, or helm install --dry-run=client -o yaml (the release record includes config)
compare two environments --output-dir per environment, then diff -r
test a capability branch --kube-version, --api-versions, --is-upgrade
stop on bad input required, fail, a values schema
find which values key a template reads grep -rn 'Values.image' templates/

The --output-dir + diff pair is the one you'll use most when reviewing a values change:

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.9.0\n' > Chart.yaml
printf 'replicaCount: 2\nlogLevel: info\n' > values.yaml
printf 'replicaCount: 5\n' > values-prod.yaml
cat > templates/configmap.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-config
data:
  LOG_LEVEL: {{ .Values.logLevel | quote }}
  REPLICAS: {{ .Values.replicaCount | quote }}
EOF
helm template web . --output-dir out/default > /dev/null
helm template web . -f values-prod.yaml --output-dir out/prod
diff -r out/default out/prod
wrote out/prod/linkly/templates/configmap.yaml

diff -r out/default/linkly/templates/configmap.yaml out/prod/linkly/templates/configmap.yaml
9c9
<   REPLICAS: "2"
---
>   REPLICAS: "5"

Test yourself

Helm reports YAML parse error … line 14. What does the line number refer to?

  1. The line in values.yaml
  2. The line in the rendered output of that template
  3. The line in Chart.yaml
  4. The line in your template source

A misplaced resources block renders valid YAML. What catches it before the cluster does?

  1. helm lint
  2. kubeconform -strict
  3. helm package
  4. Nothing

Which flag makes helm lint fail on warnings?

  1. --quiet
  2. --strict
  3. --debug
  4. --with-subcharts