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

"Unexpected unclosed action in template clause" when trying to render templated string from values.yaml #7704

Closed
DaveOHenry opened this issue Feb 28, 2020 · 24 comments

Comments

@DaveOHenry
Copy link

DaveOHenry commented Feb 28, 2020

Many charts currently support Prometheus Operator by optionally adding "prometheusRule" objects to the cluster. A user can then just add the rules in his "values.yaml". The supplied values can be templated strings and will be used by a "prometheusrule.yaml" template. This works for simple rules, but in most cases the chart will just refuse to render properly. The funny thing is, that even the example alerts that can be found in the values.yaml files of many charts are not working.

I don't know where the problem exactly is, but it looks like something is trying to pretty print the values by inserting a "\n" at a position where it should not be and this breaks the template in most cases (see "{{ template \"test.fullname\"\n . }} in the complete error message below). I already tried all kinds of workarounds like removing whitespaces, playing with single and double quotes, yaml multiline ... nothing seems to work.

values.yaml:

prometheusRule:
  enabled: true
  additionalLabels: {}
  namespace: "monitoring"
  rules:
    - alert: MyTestAlert
      expr: my_test_alert_with_a_long_name_kjlgnkjfdg{service="{{ template "test.fullname" . }}-mymetricsservice"} / 3600 > 1
      for: 1m
      labels:
        severity: critical
      annotations:
        description: |-
          The {{ template "test.fullname" . }} has a problem. The value is {{ "{{ $value }}" }}
        summary: Something {{ "{{ $value }}" }}

prometheusrule.yaml:

{{- if .Values.prometheusRule.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: {{ template "test.fullname" . }}
{{- with .Values.prometheusRule.namespace }}
  namespace: {{ . }}
{{- end }}
  labels:
    app: {{ template "test.name" . }}
    chart: {{ template "test.chart" . }}
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
{{- with .Values.prometheusRule.additionalLabels }}
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- with .Values.prometheusRule.rules }}
  groups:
    - name: {{ template "test.name" $ }}
      rules: {{ tpl (toYaml .) $ | nindent 8 }}
{{- end }}
{{- end }}

Output of helm template .:

Error: template: test/templates/prometheusrule.yaml:21:16: executing "test/templates/prometheusrule.yaml" at <tpl (toYaml .) $>: error calling tpl: error during tpl function execution for "- alert: MyTestAlert\n  annotations:\n    description: The {{ template \"test.fullname\" . }} has a problem. The value is\n      {{ \"{{ $value }}\" }}\n    summary: Something {{ \"{{ $value }}\" }}\n  expr: my_test_alert_with_a_long_name_kjlgnkjfdg{service=\"{{ template \"test.fullname\"\n    . }}-mymetricsservice\"} / 3600 > 1\n  for: 1m\n  labels:\n    severity: critical": parse error at (test/templates/prometheusrule.yaml:6): unexpected unclosed action in template clause

Output of helm version:

version.BuildInfo{Version:"v3.1.1",` GitCommit:"afe70585407b420d0097d07b21c47dc511525ac8", GitTreeState:"clean", GoVersion:"go1.13.8"}
Client: &version.Version{SemVer:"v2.14.1", GitCommit:"5270352a09c7e8b6e8c9593002a73535276507c0", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.14.1", GitCommit:"5270352a09c7e8b6e8c9593002a73535276507c0", GitTreeState:"clean"}
@jdolitsky
Copy link
Contributor

The issues is certainly with the line

      rules: {{ tpl (toYaml .) $ | nindent 8 }}

in prometheusrule.yaml

Could you try removing the expr: field and see if you still get this error? I suspect thats the piece causing the error, and would be helpful to isolate it.

@jdolitsky
Copy link
Contributor

jdolitsky commented Feb 28, 2020

* expr field in values.yaml

@DaveOHenry
Copy link
Author

DaveOHenry commented Feb 28, 2020 via email

@DaveOHenry
Copy link
Author

Found this in the go-yaml repo:
go-yaml/yaml#166
There is also a pull request mentioned that could potentially solve this issue.

But even if this PR makes it into upstream it would still not be available in HELM, would it? As far as I understand Helm v2 uses https://github.com/ghodss/yaml (=go-yaml/v2 v2.2.2) and Helm v3 uses https://sigs.k8s.io/yaml (=go-yaml/v2 v2.2.8).

@technosophos
Copy link
Member

The linebreak above should not have an impact. I'm actually wondering if this is a case of nested {{}} characters. Can you get this down to a minimal template that just shows the issue without the rest of the template code?

@DaveOHenry
Copy link
Author

DaveOHenry commented Apr 22, 2020

Well, the linebreak is the main problem. See also go-yaml/yaml#166. Under normal circumstances the linebreak "should" not have an impact, because it is valid yaml in any case. Unfortunately this does not take into account that certain strings (see https://godoc.org/text/template) cannot be split.

However. When using something like this ...
values.yaml:

something:
  - foo: test1
    foo2: test2
    foo3: foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_bar="{{ "something is here" }}"

templates/test.yaml:

{{- with .Values.something }}
  something2:
    - name: yes
      issue: {{ tpl (toYaml .) $ | nindent 8 }}
{{- end }}

The error message is:

Error: render error in "foo/templates/test.yaml": template: foo/templates/test.yaml:4:16: executing "foo/templates/test.yaml" at <tpl (toYaml .) $>: error calling tpl: Error during tpl function execution for "- foo: test1\n  foo2: test2\n  foo3: foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_bar=\"{{ \"something is\n    here\" }}\"\n": parse error in "foo/templates/test.yaml": template: foo/templates/test.yaml:3: unexpected unterminated quoted string in command

It's a different error message, but the underlying problem is the same.

As a workaround I am currently using yaml literal style for all templated strings. At least in my case it works as expected:

something:
  - foo: test1
    foo2: test2
    foo3: |
      foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_foo_bar="{{ "something is here" }}"

@DaveOHenry
Copy link
Author

go-yaml/yaml#571

As far as I understand this PR could solve the issue. It was merged into go-yaml:v2 and go-yaml:v3, but is not released yet.

@gothrek22
Copy link

@DaveOHenry It has been released as part of v2.3.0 on 7th of May 😃

@DaveOHenry
Copy link
Author

Unfortunately Helm v2 uses https://github.com/ghodss/yaml (=go-yaml/v2 v2.2.2) and Helm v3 uses https://sigs.k8s.io/yaml (=go-yaml/v2 v2.2.8) 😕

@laverya
Copy link

laverya commented Oct 15, 2020

So thankfully you can update subdependencies without that dependency itself being updated - I've made a PR that does this, and a unit test demonstrating the fix!

@tshaiman
Copy link

using Helm v3.3.4 , issue still exists.

@sanjukta-maji
Copy link

what is the solution for this one ? I am having same issue with Helm v3.4.1. Please help !

Error: parse error at (mulesoft-probe/templates/PrometheusRule.yaml:28): unclosed action

spec:
groups:
- name: {{ template "mulesoft-probe.name" $ }}
rules: {{ tpl (toYaml .Values.monitoring.rules) $ | nindent 8 }}

@tshaiman
Copy link

For us it was Linux/Windows compatible issues.
open the file in VS Code and Select "LF" as the file format , not "CRLF"

@sanjukta-maji
Copy link

sanjukta-maji commented Dec 17, 2020

still no luck . its failing when i add avg_over_time () in the expr

  • alert: Probe is responding slow
    expr: avg_over_time( probe_duration_seconds{ instance="{{$.Values.monitoring.instance }}" }[2m] ) > 1
    for: 1m

@DaveOHenry
Copy link
Author

DaveOHenry commented Dec 17, 2020

The expression is too long. Unfortunately Helm splits the line and this breaks the Go template. Use yaml literal style for templated strings like:

expr: |
  avg_over_time( probe_duration_seconds{ instance="{{$.Values.monitoring.instance }}" }[2m] ) > 1

@sanjukta-maji
Copy link

Thanks ! This worked for me .

@adasari
Copy link

adasari commented Feb 3, 2021

Any update on this ? i am using helm2. tpl function on templates from values yaml is not working.
Eg:

  1. values.yaml -
initContainers:
  - name: "template-setup"
    image: "{{ include "my-test.name" . }}"  

deployment yaml -

initContainers: 
        {{- tpl (.Values.initContainers | toYaml) $ | nindent 8}}
  1. Values yaml -
initContainers:
  - name: "template-setup"
    image: "{{ .Values.image.registry }}{{ .Values.image.repository }}:{{ .Values.image.tag }}"

deployment yaml -

initContainers: 
        {{- tpl (.Values.initContainers | toYaml) $ | nindent 8}}

tpl is works with simple Values/Release objects.

Values yaml -

initContainers:
  - name: "template-setup"
    image: "{{ .Values.image.tag }}"

deployment yaml -

initContainers: 
        {{- tpl (.Values.initContainers | toYaml) $ | nindent 8}}

@DaveOHenry
Copy link
Author

@adasari : Did you try the workaround by changing your values to yaml literals?

initContainers:
  - name: "template-setup"
    image: |
      "{{ include "my-test.name" . }}"  
initContainers:
  - name: "template-setup"
    image: |
      "{{ .Values.image.tag }}"

@adasari
Copy link

adasari commented Feb 8, 2021

@DaveOHenry literals working. I have confirmed in my original comment.

@ramanNarasimhan77
Copy link

ramanNarasimhan77 commented Feb 17, 2021

I hit this issue while trying to create a configmap from a file using
{{ ( tpl (.Files.Glob "files/myconfig.yaml").AsConfig . ) | nindent 2 }}

I was able to fix this by using helm variables in files/myconfig.yaml

{{- $imageTag := ( index .Values "image" "tag" -}}
{{ printf "\n" }}
initContainers:
  - name: "template-setup"
    image: {{ $imageTag }}

I was referring to the same variable from Values multiple times in the files/myconfig.yaml file.
So this approach suited my use case better than using yaml literals.

@github-actions
Copy link

This issue has been marked as stale because it has been open for 90 days with no activity. This thread will be automatically closed in 30 days if no further activity occurs.

@github-actions github-actions bot added the Stale label May 19, 2021
@DaveOHenry
Copy link
Author

ping

@github-actions github-actions bot removed the Stale label May 20, 2021
@github-actions
Copy link

This issue has been marked as stale because it has been open for 90 days with no activity. This thread will be automatically closed in 30 days if no further activity occurs.

@andreimosteoru
Copy link

can this issue reopened again? it is not fixed yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants