Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 435
jjbskir Avatar asked Nov 08 '25 16:11

jjbskir


2 Answers

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 }}
like image 75
Yuri G. Avatar answered Nov 10 '25 17:11

Yuri G.


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 }}
like image 41
Mafor Avatar answered Nov 10 '25 17:11

Mafor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!