Search code examples
kuberneteskubernetes-helmgo-templates

What causes Helm chart template to throw 'unexpected EOF'?


I'm trying to add ingress to my nginx container.

The following ingress template gives me "parse error (<>/ingress.yaml:71: unexpected EOF)". I went through trying mark possible missing end statements, but even adding arbitrary end at the end of file didn't fix it. I am out of ideas as to what causes this EOF.

So the question is the general: What causes "unexpected EOF" in the file?

{{- if .Values.web.ingress.enabled }}

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: {{ .Release.Name }}-proxy-ingress
  labels:
    tier: intelowl
    component: proxy
    release: {{ .Release.Name }}
    chart: {{ .Chart.Name }}
    heritage: {{ .Release.Service }}
{{- with .Values.labels }}
{{ toYaml . | indent 4 }}
{{- end }} # {{- with .Values.labels }}

{{- if .Values.web.ingress.annotations }}
  annotations:
{{- with .Values.web.ingress.annotations }}
{{ toYaml . | indent 4 }}
{{- end }} # {{- with .Values.web.ingress.annotations }}
{{- end }} # {{- if .Values.web.ingress.annotations }}

spec:

{{- if .Values.web.ingress.tls.enabled }}
  tls:
    - hosts:
        - {{ .Values.web.ingress.host }}
      secretName: {{ .Values.web.ingress.tls.secretName }}
{{- end }} # {{- if .Values.web.ingress.tls.enabled }}

  rules:
    - http:
        paths:
{{- range .Values.web.ingress.precedingPaths }}
          - path: {{ .path }}
            backend:
              service:
                name: {{ .serviceName }}
                port: 
                  number: {{ .servicePort }}
{{- end }} # {{- range .Values.web.ingress.precedingPaths }}

          - backend:
              service:
                name: {{ .Release.Name }}-proxy
                port: 
                  number: {{ ternary 443 80 .Values.web.ingress.tls.enabled }}
{{- if .Values.web.ingress.path }}
            path: {{ .Values.web.ingress.path }}
{{- end }} # {{- if .Values.web.ingress.path }}

{{- range .Values.web.ingress.succeedingPaths }}
          - path: {{ .path }}
            backend:
              service:
                name: {{ .serviceName }}
                port: 
                  number: {{ .servicePort }}
{{- end }} # {{- range .Values.web.ingress.succeedingPaths }}

{{- if .Values.web.ingress.host }}
      host: {{ .Values.web.ingress.host }}
{{- end }} # {{- if .Values.web.ingress.host }}

{{- end }} # {{- if .Values.web.ingress.enabled }}


Solution

  • Your file is generally structured like so:

    {{- if .Values.someCondition }}
    ...
    {{- end }} # {{- if .Values.someCondition }}
    

    However, the Go text/template engine runs before any YAML parsing happens. There is not a comment in this example; there is an if statement, the matching end, and an unterminated if.

    The text/template language has its own {{/* comment */}} syntax, and in principle you could use this

    {{- if .Values.someCondition }}
    ...
    {{- end }}{{/* if .Values.someCondition */}}
    

    Beyond this, the file you show seems to have the right number of {{ end }}s.

    I'd probably avoid this style myself. Usually these condition blocks are fairly short; you could break the template into multiple define named templates if that helps.

    metadata:
      labels:
        tier: intelowl
        et: cetera
    {{- include "more-labels" . | indent 4 }}
    {{- include "ingress-annotations" . | indent 2 }}
    
    {{- define "more-labels" -}}
    {{ with .Values.labels }}{{ toYaml . }}{{ end }}
    {{- end -}}
    
    {{- define "ingress-annotations" -}}
    {{- with .Values.web.ingress.annotations }}
    annotations:
    {{ toYaml . | indent 2 }}
    {{- end -}}
    {{- end -}}
    

    Especially for labels you might find that some of the values will be reused in all of your objects, and so including a template in the _helpers.tpl file to produce the common values will reduce some of the repetitiveness.