0

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 {}

sype
  • 21
  • 3

2 Answers2

2

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

Andrew
  • 3,912
  • 17
  • 28
0

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.

sype
  • 21
  • 3
  • helpers usually help to eliminate boilerplate and also helpful when you have a lot of extra conditions, which alter your output significantly depending on some values. Check default `_helpers.tpl` to get an idea when they are used. In your case, however, hiding your implementation of audiences will hamper readability for others, and since you won't be reusing this code in other templates - i think it's better to not use helpers in this case. Also, welcome to StackOverflow, and please, create a separate question if original one is answered, and don't use answer section for questions. Ty! – Andrew Feb 09 '22 at 16:16
  • Thank you for the warm welcome. I appreciate you and your thorough answers. – sype Feb 09 '22 at 18:12