Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use .Values in other variable loops

Below is my case:

{{- $v := (.Files.Get "values-productpage.yaml") | fromYaml }}.   
   spec:
     {{- range $key, $value := $v.containers }}
     containers:
     - name: {{ $value.name }}
       image: {{.Values.productpage_image}}:latest

Here when reaching .Values.productpage_image, it reports: can't evaluate field productpage_image in type interface {}.

Is there any usage error here? Why can I not use .Values.xxx in this loop? If I move the .Values to the first line, there is no error.

like image 377
yangyang Avatar asked Sep 13 '25 18:09

yangyang


2 Answers

You can simply use $ to get to the root scope

Without defining what $root is, you can references .Values as $.Values from within a loop, or any other scope.

Source: https://github.com/kubeapps/kubeapps/pull/1057

like image 170
GeorgeLambadas Avatar answered Sep 15 '25 18:09

GeorgeLambadas


As @abinet explained properly about the reason, I'll share my solution for that( which helped me a lot, and I hope that will save you time):

First, I saved the scope:
{{- $root := . -}}

and after that , I called the .Value inside the loop context like this:
{{ $root.Values.data }}

so basically , you code should be look like:

{{- $root := . -}}

{{- $v := (.Files.Get "values-productpage.yaml") | fromYaml }}.   
  spec:
   {{- range $key, $value := $v.containers }}
   containers:
   - name: {{ $value.name }}
     image: {{$root.Values.productpage_image}}:latest
like image 33
nisanarz Avatar answered Sep 15 '25 18:09

nisanarz