4
votes

I have a HELM Chart with a few requirements (i.e. subcharts).

When deploying that chart, I use a values.yaml containing all the values for both the main chart and the subcharts :

globalFoo: "bar"

subchart1:
  foo: subchart1-{{ globalFoo }}

subchart2:
  localFoo: "bar2"
  foo: subchart2-{{ subchart2.localFoo }}

I'd like to achieve two things :

  • Reference a previously declared global variable (i.e. in the global chart scope) in a subchart value (subchart1.foo in my example)
  • Reference a previously declared local variable in the same subchart scope (subchart2.foo in my example)

The exemple above doesn't work. I tried several syntaxes and none of them worked. I didn't find anything like that in the HELM documentation.

Is it doable ?

2

2 Answers

3
votes

Reference a previously declared global variable in a subchart value, Reference a previously declared local variable in the same subchart scope

This can be achieved to some extent using anchors and aliases.

global: 
   foo: &global-foo bar
   
subchart1:
  # this verbatim copies the content of the anchor
  foo: *global-foo
  local: &subchart1-local bar 

subchart2:
  foo: *subchart1-local

Values can naturally be combined in a helm template:

kind: ConfigMap
...
data:
   FOO: "subchart2-{{ .Values.subchart2.foo }}"

If find yourself needing "templated values" the tpl function may be helpful:

# values 
global:
   user: foo
   pass: bar 
   dbhost: dbserver.com

mychart:
   connection: "db://{{.d.user}}:{{d.pass}}/{{d.dbhost}}"

# template
kind: ConfigMap
...
data:
   DBURL: "{{ tpl .Values.mychart.connection (dict "d" .Values.global "Template" $.Template }}"

Note the (dict ...) syntax is derived from a hint in this helm github comment. This helps to shorten the template string by giving the ".d" context instead ".Values", i.e. .d.user is short for .Values.global.user

2
votes

Currently (as of Helm version 3) this is not supported.

A similar issue is discussed in Proposal: Allow templating in values.yaml

And is rejected for several reasons. One of them stated by the creator of Helm

The bigger constraint is that the values.yaml file MUST always be a valid YAML file.

And also in Support for using {{ values }} within values.yaml

the file that is passed into the template engine as a source of data for templates is itself not passed through the template engine. We almost definitely will not do that because it gets very confusing for users. It also breaks both standard YAML compatibility and backward compatibility with all existing Helm versions.

And last but not least here

The tl;dr At its most simple, the reason behind not wanting to do this is you don’t template the file that provides the values that you template with. Furthermore, this feature will be made obsolete by Helm 3

...

Obsolete by Helm 3 In Helm 3 as part of the eventing/hook system, we will allow Lua-based modification of values in a much easier way than having to template them out. Seeing as we are well underway with development for Helm 3, adding a feature with all of the drawbacks mentioned above would cause more churn than the value added. So we aren’t rejecting this outright, but are implementing it (albeit in a different way) for Helm 3

But as of now mentioned support via Lua seems to be still open.