Home > OS >  helm helpers file can't evaluate field type interface array/string
helm helpers file can't evaluate field type interface array/string

Time:02-11

I am rather new to helm, and I am trying to create a chart, but running into values not transforming from the values.yaml file into my generated chart.

here are my values.yaml

apiVersion: security.istio.io/v1alpha2
kind: RequestAuthentication
metadata:
  name: name01
  namespace: ns-01
spec:
  selector:
    matchLabels:
      app: app-label
  jwtRules:
    - issuer: foo
      jwksUri: bar
      forwardOriginalToken: true
      audiences:
        - user1
        - user2

then with my helm template:

apiVersion: networking.istio.io/v1alpha2
kind: RequestAuthentication
metadata:
  name: name01
spec:
  selector:
    matchLabels:
      app: {{ .Values.spec.selector.matchLabels.app }}
  jwtRules:
    - issuer: foo
      jwksUri: bar
      forwardOriginalToken: true
      audiences: |-
        {{- range .Values.spec.jwtRules.audiences }}
          - {{ . | title | quote }}
      {{ end }}
---

I also have a helpers file.

_helpers.tpl

{{/* vim: set filetype=mustache: */}}
{{- define "jwtRules.audiences" -}}
{{- range $.Values.spec.jwtRules.audiences }}
      audiences:
        - {{ . | quote }}
{{- end }}
{{- end }}

the error its producing: at <.Values.spec.jwtRules.audiences>: can't evaluate field audiences in type interface {}

CodePudding user response:

This one is simple - you don't have a spec.jwtRules.audiences in your values file! jwtRules contains an array, so you'll have to use some index or iterate over it. Also, i don't think that neither your indentation, nor using of |- for audiences is correct, per docs it should be an array of strings.

So i came up with this example (your values are unchanged):

apiVersion: networking.istio.io/v1alpha2
kind: RequestAuthentication
metadata:
  name: name01
spec:
  selector:
    matchLabels:
      app: {{ .Values.spec.selector.matchLabels.app }}
  jwtRules:
    - issuer: foo
      jwksUri: bar
      forwardOriginalToken: true
      audiences:
      {{- with (first .Values.spec.jwtRules) }}
      {{- range .audiences }}
        - {{ . | title | quote -}}
      {{- end }}
      {{- end }}

renders into:

apiVersion: networking.istio.io/v1alpha2
kind: RequestAuthentication
metadata:
  name: name01
spec:
  selector:
    matchLabels:
      app: app-label
  jwtRules:
    - issuer: foo
      jwksUri: bar
      forwardOriginalToken: true
      audiences:
        - "User1"
        - "User2"

In this case it uses a first element of array

CodePudding user response:

Thank you @andrew. I also came to a simple solution but would like feedback on it.

I removed the helpers file then modified my helm chart with the following.

values.yaml (is kept the same as above)

helmchart:

apiVersion: networking.istio.io/v1alpha2
kind: RequestAuthentication
metadata:
  name: name01
spec:
  selector:
    matchLabels:
      app: {{ .Values.spec.selector.matchLabels.app }}
  jwtRules:
    - issuer: foo
      jwksUri: bar
      forwardOriginalToken: true
      {{- with index .Values.spec.jwtRules 0 }}
      audiences:
         {{- range $a := .audiences }}
         - {{ $a -}}
      {{ end }}
      {{ end }}
---

This produces the following:

apiVersion: networking.istio.io/v1alpha2
kind: RequestAuthentication
metadata:
  name: name01
spec:
  selector:
    matchLabels:
      app: app-label
  jwtRules:
    - issuer: foo
      jwksUri: bar
      forwardOriginalToken: true
      audiences:
         - user1
         - user2

Should I continue to use a helpers file?

Thanks again for all the help.

  • Related