red888
red888

Reputation: 31520

helm chart error can't evaluate field Values in type interface {}

I know this is some kind of syntax/yaml structure related error but the message is so cryptic I have no idea what the issue is:

Error: render error in "mychart/templates/ingress.yaml": template: mychart/templates/ingress.yaml:35:37: executing "mychart/templates/ingress.yaml" at <.Values.network.appP...>: can't evaluate field Values in type interface {}

This is in my values.yaml:

network:
  appPort: 4141

This is the ingress.yaml:

{{- $fullName := include "mychart.fullname" . -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    app.kubernetes.io/name: {{ include "mychart.name" . }}
    helm.sh/chart: {{ include "mychart.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
{{- if .Values.ingress.tls }}
  tls:
  {{- range .Values.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . | quote }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
        {{- range .paths }}
          - path: {{ . }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: {{ .Values.network.appPort }}
        {{- end }}
  {{- end }}

Why doesn't {{ .Values.network.appPort }} work? I have used values with this same structure in other places

Upvotes: 60

Views: 130933

Answers (4)

In the range block . refers to the current value in the execution time. Instead of . you can use $ to access to the root data object in the range block instead of declaring top level variables.

Example:

  {{- range $host := .Values.ingress.hosts }}
    - host: {{ $host }}
      http:
        paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: frontend
              port:
                number: {{ $.Values.frontend.service.port }}
  {{- end}}

Upvotes: 53

Xin Cai
Xin Cai

Reputation: 161

I use helm3 and I have the same error message, when i run helm template my-chart. In my case, i have define wrongly in values.yaml,

WRONG configuration (In my values.yaml missing path under paths):

ingress:
  enabled: true
  annotations:
     kubernetes.io/ingress.class: "nginx"
     nginx.ingress.kubernetes.io/rewrite-target: /$2
     nginx.ingress.kubernetes.io/ssl-redirect: "false"
     nginx.ingress.kubernetes.io/use-regex: "true"

  hosts:
    - host: "dev"
      paths:
      - /dev(/|$)(.*)
  tls: []

correct the ingress definition like this , it works

ingress:
  enabled: true
  annotations:
     kubernetes.io/ingress.class: "nginx"
     nginx.ingress.kubernetes.io/rewrite-target: /$2
     nginx.ingress.kubernetes.io/ssl-redirect: "false"
     nginx.ingress.kubernetes.io/use-regex: "true"

  hosts:
    - host:
      paths:
      - path: "/dev(/|$)(.*)"
  tls: []

my ingress template, ingress.yaml, which is generated automatically via helm

{{- if .Values.ingress.enabled -}}
{{- $fullName := include "bsb-lookup.fullname" . -}}
{{- $svcPort := .Values.service.port -}}
{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    {{- include "bsb-lookup.labels" . | nindent 4 }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.ingress.tls }}
  tls:
    {{- range .Values.ingress.tls }}
    - hosts:
        {{- range .hosts }}
        - {{ . | quote }}
        {{- end }}
      secretName: {{ .secretName }}
    {{- end }}
  {{- end }}
  rules:
    {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
          {{- range .paths }}
          - path: {{ .path }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: {{ $svcPort }}
          {{- end }}
    {{- end }}
  {{- end }}

Upvotes: 7

Tutai Kumar Dalal
Tutai Kumar Dalal

Reputation: 114

for YAML it is also required to have the immediate parent of any optional value.

Like you are trying to do a check "a.b.c" in some yaml during helm build its gives similar error. it is required to have a.b at least in the default values.yaml.

Solved by going details in - https://github.com/helm/helm/issues/5435

Upvotes: 1

VladoDemcak
VladoDemcak

Reputation: 5259

Isn't it just scope issue?

Try something as below

{{- $fullName := include "mychart.fullname" . -}}
{{- $networkAppPort  := .Values.network.appPort -}}
...
.... omitted code
...
      http:
        paths:
        {{- range .paths }}
          - path: {{ . }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: {{ $networkAppPort }}
        {{- end }}
  {{- end }}

Upvotes: 45

Related Questions