Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing values of the subchart with dash in the name #2192

Closed
lwolf opened this issue Mar 26, 2017 · 45 comments
Closed

Accessing values of the subchart with dash in the name #2192

lwolf opened this issue Mar 26, 2017 · 45 comments

Comments

@lwolf
Copy link

lwolf commented Mar 26, 2017

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?

@thomastaylor312
Copy link
Contributor

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?

@lwolf
Copy link
Author

lwolf commented Mar 28, 2017

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 '-'

@technosophos technosophos added this to the 2.4.0 - Features milestone Apr 12, 2017
@technosophos technosophos self-assigned this Apr 12, 2017
@technosophos
Copy link
Member

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.

@lwolf
Copy link
Author

lwolf commented Apr 13, 2017

thanks, it works this way.

@technosophos
Copy link
Member

@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.

@0megam
Copy link

0megam commented Jun 4, 2017

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" }}

@spearsem
Copy link

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 }}

@bacongobbler
Copy link
Member

bacongobbler commented Oct 13, 2017

@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 }}

@spearsem
Copy link

@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".

@bacongobbler
Copy link
Member

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.

@josdotso
Copy link

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

@sslavic
Copy link
Contributor

sslavic commented Dec 17, 2017

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

@tetafro
Copy link

tetafro commented Dec 25, 2017

Is there any workaround for setting vars in cli?

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

@sgandon
Copy link

sgandon commented Feb 2, 2018

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.

@sirn
Copy link

sirn commented Feb 2, 2018

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.

@sergiuv2020
Copy link

sergiuv2020 commented Feb 13, 2018

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 <index .Values "first-key...>: error calling index: index of untyped nil

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"}

@cristifalcas
Copy link

@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

@fejta-bot
Copy link

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

@Wykiki
Copy link

Wykiki commented Sep 25, 2020

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 ?

clara-lo added a commit to clara-lo/charts that referenced this issue Oct 20, 2020
Error: bad character U+002D '-' Use --debug flag to render out invalid YAML
Solution: helm/helm#2192
@AntonOfTheWoods
Copy link

@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"

@technosophos
Copy link
Member

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.

@AntonOfTheWoods
Copy link

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" golang/go#23710. I think the reasons given (that specifically talk about the helm usecase) are perfectly valid but the go authors really don't care.

golang/go#23710 (comment) 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.

@bacongobbler
Copy link
Member

bacongobbler commented Nov 18, 2020

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!

@AntonOfTheWoods
Copy link

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! :-)

@bacongobbler
Copy link
Member

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

@prydonius
Copy link
Member

"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?

@AntonOfTheWoods
Copy link

@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.

@prydonius
Copy link
Member

+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.

@zerthimon
Copy link

zerthimon commented Nov 25, 2020

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 }}

birkb added a commit to schose/splunk-connect-for-kubernetes that referenced this issue Dec 4, 2020
maelvls added a commit to jetstack/jetstack-secure-gcm that referenced this issue Feb 5, 2021
With:

    {{ .Values.google-cas-issuer.serviceAccount.name }}

I would get the error:

    Error: parse error at googlecasissuer.yaml:40: bad character U+002D '-'

The trick is to use the "index" function from the Go templates language:

    {{ index .Values "google-cas-issuer" "serviceAccount" "name" }}

See: helm/helm#2192 (comment)

Signed-off-by: Maël Valais <mael@vls.dev>
maelvls added a commit to jetstack/jetstack-secure-gcm that referenced this issue Feb 10, 2021
With:

    {{ .Values.google-cas-issuer.serviceAccount.name }}

I would get the error:

    Error: parse error at googlecasissuer.yaml:40: bad character U+002D '-'

The trick is to use the "index" function from the Go templates language:

    {{ index .Values "google-cas-issuer" "serviceAccount" "name" }}

See: helm/helm#2192 (comment)

Signed-off-by: Maël Valais <mael@vls.dev>
maelvls added a commit to jetstack/jetstack-secure-gcm that referenced this issue Feb 11, 2021
With:

    {{ .Values.google-cas-issuer.serviceAccount.name }}

I would get the error:

    Error: parse error at googlecasissuer.yaml:40: bad character U+002D '-'

The trick is to use the "index" function from the Go templates language:

    {{ index .Values "google-cas-issuer" "serviceAccount" "name" }}

See: helm/helm#2192 (comment)

Signed-off-by: Maël Valais <mael@vls.dev>
maelvls added a commit to jetstack/jetstack-secure-gcm that referenced this issue Feb 11, 2021
With:

    {{ .Values.google-cas-issuer.serviceAccount.name }}

I would get the error:

    Error: parse error at googlecasissuer.yaml:40: bad character U+002D '-'

The trick is to use the "index" function from the Go templates language:

    {{ index .Values "google-cas-issuer" "serviceAccount" "name" }}

See: helm/helm#2192 (comment)

Signed-off-by: Maël Valais <mael@vls.dev>
maelvls added a commit to jetstack/jetstack-secure-gcm that referenced this issue Feb 12, 2021
With:

    {{ .Values.google-cas-issuer.serviceAccount.name }}

I would get the error:

    Error: parse error at googlecasissuer.yaml:40: bad character U+002D '-'

The trick is to use the "index" function from the Go templates language:

    {{ index .Values "google-cas-issuer" "serviceAccount" "name" }}

See: helm/helm#2192 (comment)

Signed-off-by: Maël Valais <mael@vls.dev>
Co-authored-by: Richard Wall <richard.wall@jetstack.io>
geobeau pushed a commit to geobeau/consul-k8s that referenced this issue Feb 25, 2021
 - see helm/helm#2192

Change-Id: I3f95e1d28a7471d7bbca5104757a0f1346fcf270
Signed-off-by: Jean-Francois Weber-Marx <jf.webermarx@criteo.com>
@helm helm deleted a comment from webchi Aug 4, 2021
@bacongobbler
Copy link
Member

bacongobbler commented Aug 4, 2021

locking the thread - see #2192 (comment) for a workaround as well as https://helm.sh/docs/chart_best_practices/values/ for the updated guide on best practices.

@helm helm locked as resolved and limited conversation to collaborators Aug 4, 2021
maciej-brzezinski pushed a commit to TouK/hermes-helm-chart that referenced this issue Feb 1, 2022
maciej-brzezinski pushed a commit to TouK/hermes-helm-chart that referenced this issue Feb 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests