jjbskir
jjbskir

Reputation: 10667

helm range get values outside of loop

I was looking at the helm range example they have on their docs.

yaml

favorite:
  drink: coffee
  food: pizza
pizzaToppings:
  - mushrooms
  - cheese
  - peppers
  - onions

helm

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea" | quote }}
  food: {{ .food | upper | quote }}
  {{- end }}
  toppings: |-
    {{- range .Values.pizzaToppings }}
    - {{ . | title | quote }}
    - {{ .Values.favorite.drink }}
    {{- end }}

I updated it to have this line - {{ .Values.favorite.drink }} but when I run helm template I get the error

can't evaluate field Values 

Is there anyway to access the top level .Values from within the range function and escape the loop?

Upvotes: 4

Views: 5353

Answers (2)

Yuri G.
Yuri G.

Reputation: 4648

You can also use a global variable $ that points to the root context

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea" | quote }}
  food: {{ .food | upper | quote }}
  {{- end }}
  toppings: |-
    {{- range $.Values.pizzaToppings }}
    - {{ . | title | quote }}
    - {{ $.Values.favorite.drink }}
    {{- end }}

Upvotes: 14

Mafor
Mafor

Reputation: 10681

You can use a variable:

  toppings: |-
    {{- $drink := .Values.favorite.drink }}
    {{- range .Values.pizzaToppings }}
    - {{ . | title | quote }}
    - {{ $drink }}
    {{- end }}

You can assign Values to a variable as well if you prefer.

  toppings: |-
    {{- $val := .Values }}
    {{- range .Values.pizzaToppings }}
    - {{ . | title | quote }}
    - {{ $val.favorite.drink }}
    {{- end }}

Upvotes: 6

Related Questions