Skip to content

Commit 0196d56

Browse files
committed
fix(alertmanager): make templater reusable
Signed-off-by: alkur-gh <20610261+alkur-gh@users.noreply.github.com>
1 parent e3fe626 commit 0196d56

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

pkg/services/alertmanager.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
"maps"
910
"net/http"
1011
"net/url"
1112
"strings"
@@ -100,12 +101,12 @@ func (n AlertmanagerNotification) GetTemplater(name string, f texttemplate.FuncM
100101
return fmt.Errorf("at least one label pair required")
101102
}
102103

103-
notification.Alertmanager.Labels = n.Labels
104+
notification.Alertmanager.Labels = maps.Clone(n.Labels)
104105
if err := notification.Alertmanager.parseLabels(name, f, vars); err != nil {
105106
return err
106107
}
107108
if len(n.Annotations) > 0 {
108-
notification.Alertmanager.Annotations = n.Annotations
109+
notification.Alertmanager.Annotations = maps.Clone(n.Annotations)
109110
if err := notification.Alertmanager.parseAnnotations(name, f, vars); err != nil {
110111
return err
111112
}

pkg/services/alertmanager_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package services
22

33
import (
4+
"fmt"
45
"testing"
56
"text/template"
67

@@ -159,3 +160,46 @@ func Test_AlertManagerNoLabels(t *testing.T) {
159160
err := svc.Send(n, Destination{})
160161
assert.EqualError(t, err, "alertmanager at least one label pair required")
161162
}
163+
164+
func Test_AlertManagerReusableTemplater(t *testing.T) {
165+
n := Notification{
166+
Alertmanager: &AlertmanagerNotification{
167+
Labels: map[string]string{
168+
"alertname": "App_Deployed",
169+
"appname": "{{.app.metadata.name}}",
170+
},
171+
Annotations: map[string]string{
172+
"appname": "{{.app.metadata.name}}",
173+
},
174+
},
175+
}
176+
177+
templater, err := n.GetTemplater("", template.FuncMap{})
178+
if !assert.NoError(t, err) {
179+
return
180+
}
181+
182+
for i := 0; i < 2; i++ {
183+
name := fmt.Sprintf("argocd-notifications-%d", i)
184+
var notification Notification
185+
err = templater(&notification, map[string]interface{}{
186+
"app": map[string]interface{}{
187+
"metadata": map[string]interface{}{
188+
"name": name,
189+
},
190+
"spec": map[string]interface{}{
191+
"source": map[string]interface{}{
192+
"repoURL": "https://github.com/argoproj-labs/argocd-notifications.git",
193+
},
194+
},
195+
},
196+
})
197+
if !assert.NoError(t, err) {
198+
return
199+
}
200+
201+
assert.Equal(t, "App_Deployed", notification.Alertmanager.Labels["alertname"])
202+
assert.Equal(t, name, notification.Alertmanager.Labels["appname"])
203+
assert.Equal(t, name, notification.Alertmanager.Annotations["appname"])
204+
}
205+
}

0 commit comments

Comments
 (0)