Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10.0.x] Alerting: Fix provisioned templates being ignored by alertmanager #69488

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/services/ngalert/notifier/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ func (am *Alertmanager) applyConfig(cfg *apimodels.PostableUserConfig, rawConfig
cfg.TemplateFiles = map[string]string{}
}
cfg.TemplateFiles["__default__.tmpl"] = alertingTemplates.DefaultTemplateString
cfg.AlertmanagerConfig.Templates = append(cfg.AlertmanagerConfig.Templates, "__default__.tmpl")

// next, we need to make sure we persist the templates to disk.
_, templatesChanged, err := PersistTemplates(cfg, am.Base.WorkingDirectory())
paths, templatesChanged, err := PersistTemplates(cfg, am.Base.WorkingDirectory())
if err != nil {
return false, err
}
cfg.AlertmanagerConfig.Templates = paths

// If neither the configuration nor templates have changed, we've got nothing to do.
if !amConfigChanged && !templatesChanged {
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/ngalert/notifier/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func PersistTemplates(cfg *api.PostableUserConfig, path string) ([]string, bool,
}

file := filepath.Join(path, name)
pathSet[file] = struct{}{}
pathSet[name] = struct{}{}

// Check if the template file already exists and if it has changed
// We can safely ignore gosec here as we've previously checked the filename is clean
Expand Down Expand Up @@ -62,7 +62,7 @@ func PersistTemplates(cfg *api.PostableUserConfig, path string) ([]string, bool,
}
for _, existingFile := range existingFiles {
p := filepath.Join(path, existingFile.Name())
_, ok := pathSet[p]
_, ok := pathSet[existingFile.Name()]
if !ok {
templatesChanged = true
err := os.Remove(p)
Expand Down
5 changes: 0 additions & 5 deletions pkg/services/ngalert/notifier/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ func TestPersistTemplates(t *testing.T) {
files[f.Name()] = string(content)
}

// Given we use a temporary directory in tests, we need to prepend the expected paths with it.
for i, p := range tt.expectedPaths {
tt.expectedPaths[i] = filepath.Join(dir, p)
}

require.Equal(t, tt.expectedError, persistErr)
require.ElementsMatch(t, tt.expectedPaths, paths)
require.Equal(t, tt.expectedChange, changed)
Expand Down
5 changes: 5 additions & 0 deletions pkg/services/ngalert/provisioning/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func (t *TemplateService) SetTemplate(ctx context.Context, orgID int64, tmpl def
revision.cfg.TemplateFiles = map[string]string{}
}
revision.cfg.TemplateFiles[tmpl.Name] = tmpl.Template
tmpls := make([]string, 0, len(revision.cfg.TemplateFiles))
for name := range revision.cfg.TemplateFiles {
tmpls = append(tmpls, name)
}
revision.cfg.AlertmanagerConfig.Templates = tmpls

serialized, err := serializeAlertmanagerConfig(*revision.cfg)
if err != nil {
Expand Down