Skip to content

Commit

Permalink
Support loading alertmanager template files in different directories (#…
Browse files Browse the repository at this point in the history
…33)

Fixes #12

Signed-off-by: Charlie Le <[email protected]>
  • Loading branch information
CharlieTLe authored and friedrichg committed May 9, 2024
1 parent f89c799 commit 81c5f1b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Order should be `CHANGE`, `FEATURE`, `ENHANCEMENT`, and `BUGFIX`
* [CHANGE] Updates version of Go to 1.22 and Alpine to 3.19.1 in Dockerfiles
* [FEATURE] Make rulerAPI Path configurable
* [FEATURE] Add tool to deserialize alertmanager state file
* [ENHANCEMENT] Support loading alertmanager templates from different directories
* [BUGFIX] Set tenant id in prom analyse command

## v0.11.1
Expand Down
26 changes: 20 additions & 6 deletions pkg/commands/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -120,16 +121,29 @@ func (a *AlertmanagerCommand) loadConfig(_ *kingpin.ParseContext) error {
return err
}

templates := map[string]string{}
for _, f := range a.TemplateFiles {
templates, err := createTemplates(a.TemplateFiles)
if err != nil {
return err
}

return a.cli.CreateAlertmanagerConfig(context.Background(), cfg, templates)
}

func createTemplates(templateFiles []string) (map[string]string, error) {
templates := make(map[string]string)
for _, f := range templateFiles {
tmpl, err := os.ReadFile(f)
if err != nil {
return errors.Wrap(err, "unable to load template file: "+f)
return nil, errors.Wrap(err, "unable to load template file: "+f)
}
templates[f] = string(tmpl)
}

return a.cli.CreateAlertmanagerConfig(context.Background(), cfg, templates)
baseName := filepath.Base(f)
if _, ok := templates[baseName]; ok {
return nil, fmt.Errorf("duplicate template file name: %s", baseName)
}
templates[baseName] = string(tmpl)
}
return templates, nil
}

func (a *AlertmanagerCommand) deleteConfig(_ *kingpin.ParseContext) error {
Expand Down
76 changes: 76 additions & 0 deletions pkg/commands/alerts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package commands

import (
"os"
"path/filepath"
"strings"
"testing"
)

func TestCreateTemplates(t *testing.T) {
// Create a temporary file
tmpfile, err := os.CreateTemp("", "template")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up

// Write some data to the file
text := "This is a test template"
if _, err := tmpfile.Write([]byte(text)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}

// Call createTemplates
templates, err := createTemplates([]string{tmpfile.Name()})
if err != nil {
t.Fatal(err)
}

// Check the returned map
if len(templates) != 1 {
t.Fatalf("Expected 1 template, got %d", len(templates))
}
if templates[filepath.Base(tmpfile.Name())] != text {
t.Fatalf("Expected template content to be '%s', got '%s'", text, templates[filepath.Base(tmpfile.Name())])
}
}

func TestCreateTemplates_DuplicateFilenames(t *testing.T) {
// Create two temporary files with the same base name in different directories
dir1 := filepath.Join(os.TempDir(), "dir1")
if err := os.Mkdir(dir1, 0755); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir1) // clean up
tmpfile1, err := os.Create(filepath.Join(os.TempDir(), "dir1", "fool.tmpl"))
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile1.Name()) // clean up

dir2 := filepath.Join(os.TempDir(), "dir2")
if err := os.Mkdir(dir2, 0755); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir2) // clean up
tmpfile2, err := os.Create(filepath.Join(os.TempDir(), "dir2", "fool.tmpl"))
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile2.Name()) // clean up

// Call createTemplates
_, err = createTemplates([]string{tmpfile1.Name(), tmpfile2.Name()})
if err == nil {
t.Fatal("Expected error due to duplicate filenames, got nil")
}

// Check that the error message contains "duplicate template file name"
if !strings.Contains(err.Error(), "duplicate template file name") {
t.Fatalf("Expected error message to contain 'duplicate template file name', got '%s'", err.Error())
}
}

0 comments on commit 81c5f1b

Please sign in to comment.