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

Can Helm support to ignore {{expr}} which is just for configuration but not render? #2798

Closed
thxCode opened this issue Aug 12, 2017 · 25 comments

Comments

@thxCode
Copy link

thxCode commented Aug 12, 2017

There is a use case: deploy Prometheus as StatefulSet and config alerting-rules as ConfigMap.

alerting-rules can take more detail on here: https://prometheus.io/docs/alerting/rules/#alerting-rules

it looks like:

  IF node_memory_Active >= 1
  FOR 1m
  LABELS { 
    service = "k8s_metrics", 
    alertname = "InstanceMemoryOverload" 
  }
  ANNOTATIONS {
    summary = "Instance {{ $labels.instance }} memory overload",
    description = "{{ $labels.instance }} memory overload for more than 1 minutes, now is {{ $value }}."
  }

Can Helm support to ignore {{expr}} which is just for configuration but not render?

@phillydogg28
Copy link

I have a similar use case to have a go style template in a config map. Right now helm is trying to evaluate the template and causing errors in the config map.

@ntfrnzn
Copy link

ntfrnzn commented Aug 31, 2017

I ran into this same/similar problem awhile ago and solved it by embedding including the alerting rules as separate files, as

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "fullname" . }}-rules
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    role: {{ template "fullname" . }}-rulefiles
    prometheus: {{ template "fullname" . }}
data:
  {{ (.Files.Glob "files/kubernetes.rules").AsConfig }}
  {{ (.Files.Glob "files/etcd2.rules").AsConfig }}
  {{ (.Files.Glob "files/custom_alert.rules").AsConfig }}

AFAIK it is extremely difficult to escape {{ }} in a go template, and slurping the files in from elsewhere is much simpler.

@bacongobbler
Copy link
Member

The way to escape double curly brackets in Go templates is to use {{ "{{" }}. It's not extremely difficult, just ugly.

@bacongobbler
Copy link
Member

I'm going to close this issue as we have two separate solutions to the problem, but please re-open if this hasn't been resolved.

@Rivic
Copy link

Rivic commented Oct 29, 2018

@bacongobbler Hello Matthew, I've been fighting with how to escape curly braces in a Helm template in markdown. Maybe I'm not understanding your solution. How would I render this correctly?
(just a snippet). I've tried raw/endraw, pre, single backslash, double backslash - No success!
thanks for any suggestion...I'm trying to put this in code block with either indents or backticks.

  apiVersion: apps/v1beta1
  kind: Deployment
  metadata:
    name: {{ template "fullname" . }}

@Rivic
Copy link

Rivic commented Oct 29, 2018

@bacongobbler I found the solution. I need to use both pre and &123; &125;

@jonasscherer
Copy link

@bacongobbler I found the solution. I need to use both pre and &123; &125;

Could you please show an example?
Is there a way to exclude one yaml from the template engine?

@jorgenkg
Copy link

jorgenkg commented Feb 26, 2019

A viable hack is to let Helm render the template as a raw string. Note the opening and closing curly bracket with a backtick:

# Excerpt from a Prometheus Alertmanager yaml
receivers:
- name: slack-receiver
  slack_configs:
  - text: |-
      {{`{{ range .Alerts }}
        *Alert:* {{ .Annotations.summary }}
      {{ end }}`}}

@weihxa
Copy link

weihxa commented Mar 7, 2019

A viable hack is to let Helm render the template as a raw string. Note the opening and closing curly bracket with a backtick:

# Excerpt from a Prometheus Alertmanager yaml
receivers:
- name: slack-receiver
  slack_configs:
  - text: |-
      {{`{{ range .Alerts }}
        *Alert:* {{ .Annotations.summary }}
      {{ end }}`}}

Thank you very much. That solved the big problem

@yarinm
Copy link

yarinm commented Aug 5, 2019

you can also use printf for that:

{{ printf "{{ some value }}" }}

@bluebeach
Copy link

A viable hack is to let Helm render the template as a raw string. Note the opening and closing curly bracket with a backtick:

# Excerpt from a Prometheus Alertmanager yaml
receivers:
- name: slack-receiver
  slack_configs:
  - text: |-
      {{`{{ range .Alerts }}
        *Alert:* {{ .Annotations.summary }}
      {{ end }}`}}

it works, thx

@swapnildahiphale
Copy link

swapnildahiphale commented Jan 3, 2020

apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "fullname" . }}-rules
labels:
chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
role: {{ template "fullname" . }}-rulefiles
prometheus: {{ template "fullname" . }}
data:
{{ (.Files.Glob "files/kubernetes.rules").AsConfig }}
{{ (.Files.Glob "files/etcd2.rules").AsConfig }}
{{ (.Files.Glob "files/custom_alert.rules").AsConfig }}

This didn't work for me.
After banging my head for 2 days was able to make following work!

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "fullname" . }}-rules
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    role: {{ template "fullname" . }}-rulefiles
    prometheus: {{ template "fullname" . }}
data:
  {{- (.Files.Glob "files/kubernetes.rules").AsConfig | nindent 2}}

@v6
Copy link

v6 commented Feb 18, 2020

A viable hack is to let Helm render the template as a raw string. Note the opening and closing curly bracket with a backtick:

# Excerpt from a Prometheus Alertmanager yaml
receivers:
- name: slack-receiver
  slack_configs:
  - text: |-
      {{`{{ range .Alerts }}
        *Alert:* {{ .Annotations.summary }}
      {{ end }}`}}

I can add to the chorus of confirmations that this still works.

And you may need it to get this to work:

https://www.vaultproject.io/docs/platform/k8s/injector/examples/

cat <<EOF >> ./patch.yaml
spec:
  template:
    metadata:
      annotations:
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/agent-inject-status: "update"
        vault.hashicorp.com/agent-inject-secret-db-creds: "database/creds/db-app"
        vault.hashicorp.com/agent-inject-template-db-creds: |
          {{- with secret "database/creds/db-app" -}}
          postgres://{{ .Data.username }}:{{ .Data.password }}@postgres:5432/appdb?sslmode=disable
          {{- end }}
        vault.hashicorp.com/role: "db-app"
        vault.hashicorp.com/ca-cert: "/vault/tls/ca.crt"
        vault.hashicorp.com/client-cert: "/vault/tls/client.crt"
        vault.hashicorp.com/client-key: "/vault/tls/client.key"
        vault.hashicorp.com/tls-secret: "vault-tls-client"
EOF

@rjshrjndrn
Copy link

spec:
  groups:
  - name: alertrules.kafkalag
    rules:
    - alert: AssessmentAggregator lag
      expr: sum(samza_pipeline_metrics_consumer_lag{job_name= "AssessmentAggregator"}) > {{ .Values.assessment_aggregator_threshold }}
      for: 5m
      labels:
        severity: critical
      annotations:
        message: {{`"AssessmentAggregator lag is {{$value}}"`}}
        summary: AssessmentAggregator lag is Critical

You can add

{{` ....... `}}

and will template properly

@NargiT
Copy link

NargiT commented Jun 2, 2020

A viable hack is to let Helm render the template as a raw string. Note the opening and closing curly bracket with a backtick:

# Excerpt from a Prometheus Alertmanager yaml
receivers:
- name: slack-receiver
  slack_configs:
  - text: |-
      {{`{{ range .Alerts }}
        *Alert:* {{ .Annotations.summary }}
      {{ end }}`}}

Unfortunately this does not work using promtool. https://prometheus.io/docs/prometheus/latest/configuration/unit_testing_rules/

@Kaali09
Copy link

Kaali09 commented Jun 8, 2020

spec:
  groups:
  - name: alertrules.kafkalag
    rules:
    - alert: AssessmentAggregator lag
      expr: sum(samza_pipeline_metrics_consumer_lag{job_name= "AssessmentAggregator"}) > {{ .Values.assessment_aggregator_threshold }}
      for: 5m
      labels:
        severity: critical
      annotations:
        message: {{`"AssessmentAggregator lag is {{$value}}"`}}
        summary: AssessmentAggregator lag is Critical

You can add

{{` ....... `}}

and will template properly

Awesome!!!. It worked. Thanks.

@Sudharma
Copy link

Sudharma commented Jul 2, 2021

That works but how can we make it run for prometheus unit test? Any clues?

@pjamenaja
Copy link

Hi,

Just for the persons how reach here and finding the way to use literal like "{{ xxxxx }}" in your helm value file. The snippet below solved my issue.

      labels:
        sink_name: loki_syslog
        #Below key useed printf() to escape Helm interprets {{ }}
        message: >-
          {{ printf "{{ message }}" }}
        hostname: >-
          {{ printf "{{ hostname }}" }}

The helm rendered output look like this

        labels:
          hostname: '{{ hostname }}'
          message: '{{ message }}'
          sink_name: loki_syslog

@ghost
Copy link

ghost commented Sep 29, 2021

So, I want to add my two cents here on how to get the backtick in the raw string. It is possible, and I have found a way to do it.

For example:

{{`Environment: {{ .CommonLabels.environment }}
Cluster: {{ .CommonLabels.cluster }}`}}

I want the label content in backticks, so:

{{`Environment: {{ printf "\x60" }}{{ .CommonLabels.environment }}{{ printf "\x60" }}
Cluster: {{ printf "\x60" }}{{ .CommonLabels.cluster }}{{ printf "\x60" }}`}}

Basically, where you want backticks just add {{ printf "\x60" }}

@dmanser92
Copy link

The printf approach worked fine for me, I just wanted to add that if you need to print curly braces it's not gonna be valid yaml if not quoted.
There are certain use cases where for example a k8s-operator performs templating and you don't want to render it with helm already. That's how I did it (I want the stuff in quotes not to be rendered by helm):
.dockerconfigjson: {{ printf "{{ .pullsecret | toString }}" | quote }}

@jsitu777
Copy link

jsitu777 commented May 19, 2022

Anyone know how to escape a `sign? example `kubeflow`

@rufreakde
Copy link

IMHO this thread was closed to early. Just because a workaround was found for something that should be enabled in helm itself via some metadata like

{{ignoreTemplateStart}}
...
{{ignoreTemplateEnd}}

Where both of this evaluate to "" ofc.

Yes - there are workarounds but the original creator of this thread asked for a configuration for helm not a workaround for strings to trick helm. And I think it would be a great addition.

Its a feature request and should also be considered as such.

@joejulian
Copy link
Contributor

@rufreakde Since this is actually just how the go template engine works, perhaps file an issue there. The solutions offered in this thread are the only ways to do it with go templates.

@nicokowe
Copy link

nicokowe commented Nov 2, 2023

@rufreakde Since this is actually just how the go template engine works, perhaps file an issue there. The solutions offered in this thread are the only ways to do it with go templates.

helm could disable the go templating when it find a flag "ignoreTemplateStart" It should be not too difficult and it will sove lots of problems for any jinja configmap

@zakkg3
Copy link

zakkg3 commented Nov 3, 2023

there is a better workaround and is using toJson

apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-dashboards
data:
  compute.json:
{{ .Files.Get "dashboards/compute.json" | toJson | indent 4 }}

this avoids Go rendering the content of compute.json and puts the content there with the "{{variables}}" and everything.

be aware. Confimaps have a limitation of 1 MB. So do not abuse this. Otherwise, consider putting them on a repo and use something like this for GitLab (you can do it for any external URL / repo):

    dashboards:
      default:
        your-dashboard-at-gitlab:
          url: "https://gitlab.yourdomain.com/api/v4/projects/9999/repository/files/yourfolder%2Fcompute.json/raw?ref=main"

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

No branches or pull requests