How to retrieve one of the elements of an array on a Helm template

helm template array index element

2 min read | by Jordi Prats

While working on Helm charts we might be interested on using a particular element of an array (most likely the first). To achieve this we won't be able to use the regular square brackets approach, instead we will have to use the index function

With the index function we retrieve one of the elements of an array. For example, if we have the following values:

ingress:
  annotations: {}
  hosts:
    - host: example.pet2cattle.com
      paths:
        - path: '/'
          backend:
            serviceName: ssl-redirect
            servicePortName: use-annotation
        - path: '/'
    - host: another-example.pet2cattle.com
      paths:
        - path: '/'
          backend:
            serviceName: ssl-redirect
            servicePortName: use-annotation
        - path: '/'

We can retrieve the first host like this:

  env:
  - name: API_ENDPOINT
    {{- with (index .Values.ingress.hosts 0) }}
    value: {{ .host }}
    {{- end }}

So using the previous values this environment variable would be set to example.pet2cattle.com


Posted on 25/11/2021

Categories