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

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.
This commit is contained in:
Michał Zielonka 2022-07-14 11:54:08 +02:00 committed by GitHub
parent 34d45977ca
commit b54da68765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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