Alerting: fix validation of alertmanager template. (#51538) (#52210)

without setting function map from alertmanager we receive error:
method=PUT path=/api/v1/provisioning/templates/slack.message status=400
level=error msg="invalid object specification: invalid template: template: :1: function \"toUpper\" not defined"

So for validation we should use the same settings as alertmanager do
for templates internally.

(cherry picked from commit b54da68765)

Co-authored-by: Michał Zielonka <michal.zielonka.8001@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2022-07-14 06:11:14 -04:00
committed by GitHub
co-authored by Michał Zielonka
parent 314b932ab8
commit f00f6e3375
@@ -2,11 +2,12 @@ package definitions
import (
"fmt"
"html/template"
tmplhtml "html/template"
"regexp"
"strings"
"time"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/common/model"
"gopkg.in/yaml.v3"
)
@@ -64,7 +65,9 @@ func (t *MessageTemplate) Validate() error {
return fmt.Errorf("template must have content")
}
_, err := template.New("").Parse(t.Template)
tmpl := tmplhtml.New("").Option("missingkey=zero")
tmpl.Funcs(tmplhtml.FuncMap(template.DefaultFuncs))
_, err := tmpl.Parse(t.Template)
if err != nil {
return fmt.Errorf("invalid template: %w", err)
}