Helm template: get node of first array element
Asked Answered
L

1

20

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?

Lissa answered 9/10, 2020 at 17:2 Comment(0)
L
44

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 }}
Luttrell answered 10/10, 2020 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.