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

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?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 44
  • Comments: 25 (3 by maintainers)

Commits related to this issue

Most upvoted comments

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

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

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

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.

you can also use printf for that:

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

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.

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

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.

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

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

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

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

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.

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"