Helm: Accessing values of the subchart with dash in the name

Created on 26 Mar 2017  ·  44Comments  ·  Source: helm/helm

Based on the docs, there is a convention to name charts with dashes.
But at the same time based on this doc, you should never have values with dashes.

So, let's say I have 2 dependent charts: gitlab and gitlab-runner.
How can I configure and access values for the subchart?

docs questiosupport

Most helpful comment

Use the index template function to access a value with a '-' in the name: {{ index .Values "gitlab-runner" }}.

And we should probably change the convention. That makes it a pain to deal with things like this. I assigned the issue to myself to go change the convention.

All 44 comments

This is a good question. I haven't had a problem doing this, but it is an inconsistency. @technosophos Should we change one of these to match the other?

I'm currently experiencing the problem accessing values for such subchart.

Next block gives me an error.

{{ if .Values.gitlab-runner.enabled }}

Error: UPGRADE FAILED: parse error in "gitlab/templates/deployment.yaml": template: gitlab/templates/deployment.yaml:217: bad character U+002D '-'

Use the index template function to access a value with a '-' in the name: {{ index .Values "gitlab-runner" }}.

And we should probably change the convention. That makes it a pain to deal with things like this. I assigned the issue to myself to go change the convention.

thanks, it works this way.

@prydonius Do you remember the reason why we initially decided that charts should be named with dashes? I was looking through older documentation and this has been a precedent we set since the beginning. I am thinking that maybe it's not such a good idea to change it.

I'am trying to understand how index works.
How can i access servicename value from:

mysub-chart:
  servicename: mysubchart-service

?
Found solution: {{ index .Values "mysub-chart" "servicename" }}

How would you use dash names in a control structure, such as a with or range block?

{{- range $key, $value := .Values.my-service-name.deployment.annotations }}

@spearsem I think the above comment has got the right workaround for that problem. I think this should work:

{{- range $key, $value := index .Values "my-service-name" "deployment" "annotations" }}

Otherwise I think you can instantiate a variable that points to the annotations and use that within the range or with blocks. For example

{{ $annotations := index .Values "my-service-name" "deployment" "annotations" }}
{{- range $key, $value := $annotations }}

@bacongobbler Does this also work for with? I am not familiar with the implementation of with to know if it can utilize the result of another function evaluation as "the context".

text/template appears to say that it should work with any structure, so I would assume yes.

If the value of the pipeline is empty, no output is generated; otherwise, dot is set to the value of the pipeline and T1 is executed.

The variable method doesn't seem to work for me. . The error just happens a line later.

Istio port naming convention also requires dashes (see Pod Spec Requirements).

Is there any workaround for setting vars in cli?

helm install --set one-two.three=10 releasename ./chartname

this is a real inconsistency and I have the problem too.
One solution is to use an alias in the requirements.yaml so that your subchart name is changed to something without the -, but this is indeed a pain.

I'm having this problem as well.

I think it would be nice if Helm will pass values from parent chart to subchart-name using camelcase-ify name instead, e.g. subchartName. It would make chart name convention and variables name convention to be compatible with each other, as well as side-stepping the syntax issues from above.

This does not work at all in templates, this example code:
{{ index .Values "first-key" "secondkey" }}
works in NOTES.txt but in _helpers.tpl when issuing an upgrade command:
_helpers.tpl:24:3: executing "test.template" at

Client: &version.Version{SemVer:"v2.6.2", GitCommit:"be3ae4ea91b2960be98c07e8f73754e67e87963c", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.7.0", GitCommit:"08c1144f5eb3e3b636d9775617287cc26e53dba4", GitTreeState:"clean"}

Same thing with both versions the same:
Client: &version.Version{SemVer:"v2.6.2", GitCommit:"be3ae4ea91b2960be98c07e8f73754e67e87963c", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.6.2", GitCommit:"be3ae4ea91b2960be98c07e8f73754e67e87963c", GitTreeState:"clean"}

kubectl is :
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T10:09:24Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.4", GitCommit:"9befc2b8928a9426501d3bf62f72849d5cbcd5a3", GitTreeState:"clean", BuildDate:"2017-11-20T05:17:43Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}

@svidrascu , check that you have the values defined in the Chart.yaml file for that specific subchart. It looks like the templating engine can't find your variable

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten
/remove-lifecycle stale

FYI, an amendment to #4379 was done in #4400.

This is so rubbish - no dashes, no underscores, no capital letters. How should one define compicate chart name for two (or more) simular components? Changing one simple . to " " is just awful :(

@ksemaev please see #4400. As mentioned previously, we still want users to use dashes in their package names, but they should be aware of the "gotchas" around certain limitations with the template system.

Whoops, fat-fingered the close button :laughing:

Is there any workaround for setting vars in cli?

helm install --set one-two.three=10 releasename ./chartname

If anybody is wondering, this seems to work as of helm and tiller v2.14.2.

$ helm install --set one-two.three=10 --name foo stable/mariadb
$ helm get values foo
# one-two:
#  three: 10

It really doesn't make sense to allow dashes in chart names if you don't allow dashes in values. The reason is that sometimes values are chart names. For example I have sub-chart.value which I want to specify in the parent chart values.yaml file to override child's values. I also want to use that same value in the parent template.

For other people, if you need
{{-if and value1 value2 }} you'd need to do this then:

{{- if .Values.loki.enabled }}
{{- if index .Values "prometheus-operator" "grafana" "enabled" }}
{{- if index .Values "prometheus-operator" "grafana" "sidecar" "datasources" "enabled" }}

I have one slight different question. Lets take a random example -
{{- if index .Values "prometheus-operator" "grafana" "sidecar" "datasources" "enabled" }}

My question is if we do not know "/prometheus-operator" how do I refer to this key using index. Basically I would like to get the first key of the .Values ( key being dynamic, it is not constant in my case and so how to refer this map by key in index. Can someone please let me know.

i used /" nithin-kumar"/ for this issue it is an issue of "-" , tried the escape chars

Anyone has a solution when using toYaml ?
It fails with
<toYaml>: wrong number of args for toYaml: want 1 got 5
with ou without the use of index function.

any fix for U+002D '-'

@mods/maintainers

Is there an update on this?

What kind of update?

Hello, this issue has been opened a while ago, but the problem still occurs.

The Helm Chart guidelines states that chart must be named with dashes (https://helm.sh/docs/chart_best_practices/conventions/)
But the templating gives us a hard time if we try to put dashes in values.yaml to mirror the chart names, ifwe try to use those dashes values like this :

{{ .Values.my-chart.name }}

Reading post above, it looks like that if I still want to use dashes this way, I need to use index :

{{ index .Values "my-chart" "name" }}

In addition to that, keys in values.yaml should be CamelCased (https://helm.sh/docs/chart_best_practices/values/).

I don't think that keys naming should affect Helm templating.

Is it planned to normalize that ?

@bacongobbler , like whether it's going to be "fixed", i.e., that we will be able to use dashes without resorting to hacks and tricks, or whether this is a definitive "Won't fix, get used to it, fork or find something else"

It is a limitation of the Go template language. If you would like a change, you should file it upstream in the Go project: https://github.com/golang/go

If/when they fix it, we will get support for it.

In typical Google fashion ("users are usually wrong"), this has been given a definitive "Won't fix, get used to it, fork or find something else" https://github.com/golang/go/issues/23710. I think the reasons given (that specifically talk about the helm usecase) are perfectly valid but the go authors really don't care.

https://github.com/golang/go/issues/23710#issuecomment-363488583 mentions two solutions - discouraging use of dashes (the current approach) or "rewriting the [helm] template pipelines to use the index function automatically". While I concede it might be a major undertaking, wouldn't it be possible to have a template preprocessor that could do just this? Maybe there is a hook or something that could be written to preprocess just this? While working around this is obviously possible, it just seems a little childish from the go authors and worth fixing for helm.

While I concede it might be a major undertaking

Bingo.

I mean, it’d be great if we could make some quality of life enhancements in this area, but there are a few areas that do take a bit higher priority in our minds right now (OCI support moving to stable, for example). And to write a pre-processor is definitely a major undertaking.

If you’re interested in contributing, feel free to take a look!

For me having clarity on the situation is a great first step. We have the following:

  • It is a restriction of the go template language
  • The go authors (currently) have no intention of adding this possibility, saying it will needlessly complicated their code. They specifically say that annoyed helm users should get used to it or implement a template preprocessor. They won't.
  • The helm authors have lots of higher priorities than this, as there is a relatively easy work-around, and implementing a template preprocessor is almost certainly a major undertaking. It would also only fix a bit of a non-issue, given the relatively easy work-around, so it likely won't make it to the top of the pile in the near future, if ever.
  • Anyone can have a look and, with sufficient time and go expertise, offer something to helm (the go people aren't interested apparently), though obviously it would need to be relatively elegant for the limited practical utility brought and the likely complexity added.

If everyone thinks that's pretty accurate and fair, I will stop bothering everyone! :-)

I would add that anyone who wants to look at this should consider writing a HIP - see https://github.com/helm/community/blob/master/hips/hip-0001.md

Otherwise yes

"rewriting the [helm] template pipelines to use the index function automatically". While I concede it might be a major undertaking, wouldn't it be possible to have a template preprocessor that could do just this?

It's unclear to me what this would actually mean, do you just mean that Helm should convert dash-cased keys to camel-cased keys when it builds its values map?

@prydonius , I really hadn't started to think about it on an implementation level. Dashes are valid as JSON keys, so are valid YAML keys, so the assumption from a user perspective is that there is no reason for them not to work as keys in helm templates. Because the templates are go templates, this assumption doesn't hold. It appears that some (many?) users only encounter go templates in the context of helm, and find this rather arbitrary and frustrating.

I guess one approach could be to go through and convert values to camel-case, then go through the templates and do the same. But I agree the infrastructure for this is likely a lot more of a burden than it would be worth. Actually a side-bar explanation in the intro docs saying something like:

Because helm uses the go templating language, you are not able to use dashes - in keys directly in the templates using the normal reference mechanism {{ .Values.mybasekey.my-key }} (this is NOT valid). This is a restriction of the go templating language, and something go authors have refused to support. If you really want to use dashes in your keys, you can work around this restriction by using the index function, like so {{ index .Values.mybasekey "my-key" ]}. Because this work-around is relatively easy, there are currently no plans to support using dashes in key names without index in helm. Subchart names will also be keys in templates, so if you use a subchart with a dash in the name and want to override a value, you will need to provide an alias for the chart name or use the work-around mentioned here ({{ index .Values "my-chart" "name" }}).

would probably be the best situation. For me at least, what I found frustrating was a lack of clarity on exactly why it didn't work, where the issue was and who could/should fix it. The current section in the chart best practices section is a little misleading. It is not a best practice as such to not use them, it actually requires workarounds to use them.

I am not sure where such an explanation, if accurate, should go though.

+1 I agree we need to say this in the documentation somewhere, at the very least.

It's definitely confusing, but I don't see a path for making this easier in Helm. Any template helper we add would act like a function in the same way index does (e.g. Helm's toYaml, toJson set of functions). The only way I can see of providing more direct access would be to remove dashes in keys, but as you mentioned it is valid YAML/JSON and that would likely be even more confusing. Ultimately the Go template parser doesn't support it and there's not much we can do about that without forking it.

Here is a working example of toYaml:
{{- toYaml Values.jobs.update-es.resources | nindent 12 }} = error

{{- toYaml (index .Values "jobs" "update-es" "resources") | nindent 12 }}
Was this page helpful?
0 / 5 - 0 ratings