Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helm template: get node of first array element

Say I have these values

grafana:
  ...
  ingress:
    enabled: true
    annotations: {}
      # kubernetes.io/ingress.class: nginx
      # kubernetes.io/tls-acme: "true"
    hosts:
      - host: chart-example.local
        paths: ["/grafana"]

This is standard helm. For this reason, I would like to keep hosts as an array (even if it makes the following move a bit tricky). How can I get the first .host (I do not mind about any possible other) in order to make env.value dynamic

      containers:
        - name: {{ .Chart.Name }}-grafana
          env:
          - name: GF_DOMAIN
            value: chart-example.local

I tried

          env:
          - name: GF_DOMAIN
          {{- range .Values.grafana.ingress.hosts }}
            value: {{ .host }}
          {{- end }}
          env:
          {{- range .Values.grafana.ingress.hosts }}
          - name: GF_DOMAIN
            value: {{ .host }}
          {{- end }}

Following this suggestion, I also tried

          env:
          {{- with .Values.grafana.ingress.hosts 0}}
          - name: GF_DOMAIN
            value: {{ .host }}
          {{- end}}

or

value: {{ .Values.grafana.ingress.hosts 0 .host }}
value: {{ .Values.grafana.ingress.hosts | first.host}}

How can I handle this case?

like image 637
zar3bski Avatar asked Sep 13 '25 14:09

zar3bski


1 Answers

What you need is the index function:

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

Alternatively, first works just as well:

env:
{{- with (first .Values.grafana.ingress.hosts) }}
- name: GF_DOMAIN
  value: {{ .host }}
{{- end }}
like image 61
Lauri Koskela Avatar answered Sep 21 '25 09:09

Lauri Koskela