Taseer Ahmed
Taseer Ahmed

Reputation: 179

Helm: Get value from a Map where the key is variable

I have a helm chart as follows

dns_entries:
  cluster1: xx.xx.xx.xx
  cluster2: xx.xx.xx.xx

The cluster value is also set dynamically while installing the helm chart. In the templates, I need to choose this dynamically from the map above

{{- if hasKey .Values.dns_entries .Values.clusterId }}
  clusterIP: {{ .Values.dns_entries.{{ .Values.clusterId }} }}
  {{- end }}

How can I implement the above correctly ?

Thanks!

Upvotes: 8

Views: 12565

Answers (2)

jamylak
jamylak

Reputation: 133624

https://helm.sh/docs/chart_template_guide/function_list/

get

Given a map and a key, get the value from the map.

get $myDict "key1"

get .Values.dns_entries .Values.clusterId

Upvotes: 20

Taseer Ahmed
Taseer Ahmed

Reputation: 179

I figured out that I needed to iterate over the map with built-in functions.

{{- if hasKey .Values.dns_entries .Values.clusterId }}
{{- range $key, $value := .Values.dns_entries }}
  {{- if eq $key .Values.clusterId }}
  clusterIP: $value
  {{- end }}
  {{- end }}
{{- end }}

Upvotes: 4

Related Questions