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:
ALERT InstanceMemoryOverload
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?
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.
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.
The way to escape double curly brackets in Go templates is to use {{ "{{" }}
. It's not extremely difficult, just ugly.
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.
@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" . }}
@bacongobbler I found the solution. I need to use both pre and &123; &125;
@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?
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 }}`}}
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
you can also use printf for that:
{{ printf "{{ some value }}" }}
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
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
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
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/
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.
Most helpful comment
A viable hack is to let Helm render the template as a raw string. Note the opening and closing curly bracket with a backtick: