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

1/14/2020

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!

-- Taseer Ahmed
kubernetes-helm

1 Answer

1/14/2020

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 }}
-- Taseer Ahmed
Source: StackOverflow