1

have been trying to create a notification template from values but it doesn't seem to be working.

Code:

  alerting:
    rules.yaml:
      apiVersion: 1
      templates:
        - orgID: 1
          name: Slack Template
          template: |
            {{ define "alert_severity_prefix_emoji" }}
              {{- if ne .Status "firing" }}
                :white_check_mark:
              {{- else if eq .CommonLabels.severity "critical" }}
                :red_circle:
              {{- else if eq .CommonLabels.severity "warning" }}
                :warning:
              {{ end }}
            {{ end }}
            {{ define "slack.print_alert" }}
              {{ template "alert_severity_prefix_emoji" . }}
              [{{.Status}}] {{ .Labels.alertname }}
              *Labels*:
              {{ range .Labels.SortedPairs }}
                - {{ .Name }}: {{ .Value }}
              {{ end }}
              {{ if .Annotations }}
              *Annotations*:
                {{ range .Annotations.SortedPairs }}
                  - {{ .Name }}: {{ .Value }}
                {{ end }}
              {{ end }}
              {{ if .SilenceURL }}
                *Silence*: {{ .SilenceURL }}
              {{ end }}
              {{ if .DashboardURL -}}
                *Go to dashboard*: {{ .DashboardURL }}
              {{ end }}
            {{ end }}
            {{ define "slack.message" -}}
              {{ if .Alerts.Firing -}}
                {{ len .Alerts.Firing }} firing alert(s):
                {{ range .Alerts.Firing }}
                  {{ template "slack.print_alert" . }}
                {{ end }}
              {{ end }}
              {{ if .Alerts.Resolved }}
                {{ len .Alerts.Resolved }} resolved alert(s):
                {{ range .Alerts.Resolved }}
                  {{ template "slack.print_alert" .}}
                {{ end }}
              {{ end }}
            {{ end }}'

The issue here is we have to pass this template in yaml file and yaml thinks {} are structs and because of that it is ignoring the whole template. Is there any way that I can pass this whole template as it is?

One way i tried is giving \ escape character that this '' is being reflected in the final template as well which we don't want.

Thanks in advance

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
Sai Vamsi
  • 111
  • 3
  • 19

1 Answers1

1

YAML is very sensitive to syntax and formatting, and it does treat {} as structures. That means you cannot indeed just escape them with \ as it would be reflected in the final template.

One workaround to this issue is to use the | character for literal block scalar in YAML.
See "What is the use of the pipe symbol in YAML?"

But if you are already using the pipe character (|) and it is not working, then it is possible that the issue is not with YAML parsing, but rather with how Grafana is interpreting the template.

The values.yaml file for a Helm chart is typically processed with Go templating before it is used to generate the final Kubernetes manifests. The issue you are encountering might be due to Grafana not correctly interpreting the nested templating, as your template includes both Go template syntax ({{ }}) and YAML.

One possible solution could be to try and escape the Grafana template brackets by doubling them up, like so: {{ "{{ .Status }}" }}. This is how you can tell Helm to render the inner brackets as literal text, rather than trying to interpret them as a Go template expression. So your template would look something like this:

template: |
  '{{ define "alert_severity_prefix_emoji" }}
    {{ "{{- if ne .Status \"firing\" }}" }}
      :white_check_mark:
    {{ "{{- else if eq .CommonLabels.severity \"critical\" }}" }}
      :red_circle:
    {{ "{{- else if eq .CommonLabels.severity \"warning\" }}" }}
      :warning:
    {{ "{{ end }}" }}
  {{ "{{ end }}" }}'

This can get quite complex and hard to read with a lot of nesting, but it should hopefully resolve the issue.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250