0

I have several yaml files which I try to generalize.

I have util.tpl:

{{- define "pod-test" -}}
{{- if eq .Values.appName.properties.name  "m1" }}
x: {{.Values.appName.properties.x }}
{{- end }}

I would like to make the appName string in the Values.appName.properties.x dynamic.

I call this template from the destination YAML

{{- include "pod-test" . (list "app-name") | indent 2}}

but I can't find a way to concat to something like :

 {{- $arg1 := index . 0 }}
    {{.Values.{{ $arg1 }}.properties.x }}

This just doesn't work what is the best way to do it?

user63898
  • 29,839
  • 85
  • 272
  • 514

1 Answers1

0

Will not work on helm. Instead, you can use the index function. From this:

{{ .Values.{{ .app-name }}.properties.x }}

To this:

{{ (index .Values .app-name).properties.x }}

Keep in mind that .Values[.app-name] should exist or else it will throw an error because it will try to access a property from a nil object. What you can do is the enclose it in an if-block checking for property existence.

{{ if has .Values .index }}
  {{ (index .Values .app-name).properties.x }}
{{ end }}
Joshua Robles
  • 151
  • 1
  • 8
  • thanks for your answer i updated the question with adding the {{- $arg1 := index . 0 }} but it still dosn't work when i try : {{- if eq index (.Values .$arg1).properties.name "m1" }} its not working getting : at : wrong number of args for index: want at least 1 got 0 – user63898 Jun 13 '21 at 18:12
  • I don't really understand what you're doing with `{{- $arg1 := index . 0 }}` What are you trying to get? Also your parenthesis are in the wrong position. It should be: `{{- if eq (index .Values $arg1).properties.name "m1" }}` – Joshua Robles Jun 14 '21 at 07:08
  • I realized that the code i wrote was wrong since the index was outside of the parenthesis, updated it to be correct. Please lmk if it works – Joshua Robles Jun 14 '21 at 07:16
  • Thanks! the $arg1 is the variable value im passing into the template , this value is the dynamic part of the full name of th epipeline . And way im gettng this error : executing "pod-test" at <.Values>: can't evaluate field Values in type []interface {} – user63898 Jun 14 '21 at 13:37
  • From a quick search, it looks like a scoping issue. Maybe you can check this answer https://stackoverflow.com/questions/56671452/helm-chart-error-cant-evaluate-field-values-in-type-interface If you still need help, can you update your code so I see how you're passing the value and how you're defining the template – Joshua Robles Jun 14 '21 at 13:58