Skip to content

Commit 7c53dd1

Browse files
ptodevCopilot
andauthored
Vendor Alertmanager config (#4905)
* Vendor Alertmanager config * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 41edfb3 commit 7c53dd1

File tree

12 files changed

+574
-262
lines changed

12 files changed

+574
-262
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
##
1616
## Targets for running tests:
1717
##
18-
## test Run tests
19-
## lint Lint code
20-
## integration-test Run integration tests
18+
## test Run tests
19+
## lint Lint code
20+
## integration-test Run integration tests
21+
## integration-test-k8s Run Kubernetes integration tests
2122
##
2223
## Targets for building binaries:
2324
##

internal/cmd/integration-tests-k8s/tests/mimir-alerts-kubernetes/testdata/expected_1.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ alertmanager_config: |
1212
smtp_require_tls: true
1313
route:
1414
receiver: "null"
15-
continue: false
1615
routes:
1716
- receiver: testing/alertmgr-config1/null
1817
matchers:
@@ -29,22 +28,16 @@ alertmanager_config: |
2928
- receiver: testing/alertmgr-config2/database-pager
3029
matchers:
3130
- service="webapp"
32-
continue: false
3331
group_wait: 10s
3432
receivers:
3533
- name: "null"
3634
- name: alloy-namespace/global-config/myreceiver
3735
- name: testing/alertmgr-config1/null
3836
- name: testing/alertmgr-config1/myamc
3937
webhook_configs:
40-
- send_resolved: false
38+
- url: http://test.url
4139
http_config:
4240
follow_redirects: true
43-
enable_http2: true
44-
url: http://test.url
45-
url_file: ""
46-
max_alerts: 0
47-
timeout: 0s
4841
- name: testing/alertmgr-config2/null
4942
- name: testing/alertmgr-config2/database-pager
5043
templates:

internal/cmd/integration-tests-k8s/tests/mimir-alerts-kubernetes/testdata/expected_2.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ alertmanager_config: |
1212
smtp_require_tls: true
1313
route:
1414
receiver: "null"
15-
continue: false
1615
routes:
1716
- receiver: testing/alertmgr-config1/null
1817
matchers:
@@ -27,13 +26,8 @@ alertmanager_config: |
2726
- name: testing/alertmgr-config1/null
2827
- name: testing/alertmgr-config1/myamc
2928
webhook_configs:
30-
- send_resolved: false
29+
- url: http://test.url
3130
http_config:
3231
follow_redirects: true
33-
enable_http2: true
34-
url: http://test.url
35-
url_file: ""
36-
max_alerts: 0
37-
timeout: 0s
3832
templates:
3933
- default_template

internal/component/mimir/alerts/kubernetes/alerts.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import (
99
"time"
1010

1111
"github.com/go-kit/log"
12+
alertmgr_cfg "github.com/grafana/alloy/internal/mimir/alertmanager"
1213
"github.com/grafana/dskit/backoff"
13-
alertmgr_cfg "github.com/prometheus/alertmanager/config"
1414
coreListers "k8s.io/client-go/listers/core/v1"
1515
"k8s.io/client-go/util/workqueue"
1616
_ "k8s.io/component-base/metrics/prometheus/workqueue"
1717
controller "sigs.k8s.io/controller-runtime"
18-
"sigs.k8s.io/yaml"
1918

2019
"github.com/grafana/alloy/internal/component"
2120
"github.com/grafana/alloy/internal/component/mimir/util"
@@ -230,13 +229,12 @@ func (c *Component) Startup(ctx context.Context) error {
230229
return err
231230
}
232231

233-
var baseCfg alertmgr_cfg.Config
234-
err = yaml.Unmarshal([]byte(c.args.GlobalConfig), &baseCfg)
232+
baseCfg, err := alertmgr_cfg.Unmarshal([]byte(c.args.GlobalConfig))
235233
if err != nil {
236234
return fmt.Errorf("failed to unmarshal global config: %w", err)
237235
}
238236

239-
c.eventProcessor = c.newEventProcessor(queue, informerStopChan, namespaceLister, cfgLister, baseCfg)
237+
c.eventProcessor = c.newEventProcessor(queue, informerStopChan, namespaceLister, cfgLister, *baseCfg)
240238

241239
go c.eventProcessor.run(ctx)
242240
return nil

internal/component/mimir/alerts/kubernetes/events.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ import (
88

99
"github.com/blang/semver/v4"
1010
"github.com/go-kit/log"
11+
alertmgr_cfg "github.com/grafana/alloy/internal/mimir/alertmanager"
1112
"github.com/grafana/dskit/instrument"
1213
"github.com/prometheus-operator/prometheus-operator/pkg/alertmanager"
1314
validation_v1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/alertmanager/validation/v1alpha1"
1415
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
1516
promv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
1617
"github.com/prometheus-operator/prometheus-operator/pkg/assets"
1718
promListers_v1alpha "github.com/prometheus-operator/prometheus-operator/pkg/client/listers/monitoring/v1alpha1"
18-
alertmgr_cfg "github.com/prometheus/alertmanager/config"
1919
"github.com/prometheus/client_golang/prometheus"
2020
"k8s.io/apimachinery/pkg/labels"
2121
go_k8s "k8s.io/client-go/kubernetes"
2222
coreListers "k8s.io/client-go/listers/core/v1"
2323
"k8s.io/client-go/util/workqueue"
24-
"sigs.k8s.io/yaml" // Used for CRD compatibility instead of gopkg.in/yaml.v2
2524

2625
"github.com/grafana/alloy/internal/component/common/kubernetes"
2726
"github.com/grafana/alloy/internal/component/mimir/util"
@@ -180,8 +179,12 @@ func (c *eventProcessor) provisionAlertmanagerConfiguration(ctx context.Context,
180179
cfgBuilder = alertmanager.NewConfigBuilder(slog.New(logging.NewSlogGoKitHandler(c.logger)), *version, store, &monitoringv1.Alertmanager{})
181180
)
182181

183-
convertedCfg := c.baseCfg.String()
184-
err := cfgBuilder.InitializeFromRawConfiguration([]byte(convertedCfg))
182+
convertedCfg, err := c.baseCfg.String()
183+
if err != nil {
184+
return nil, err
185+
}
186+
187+
err = cfgBuilder.InitializeFromRawConfiguration([]byte(convertedCfg))
185188
if err != nil {
186189
return nil, fmt.Errorf("failed to initialize from global AlertmangerConfig: %w", err)
187190
}
@@ -195,13 +198,12 @@ func (c *eventProcessor) provisionAlertmanagerConfiguration(ctx context.Context,
195198
return nil, fmt.Errorf("failed to marshal configuration: %w", err)
196199
}
197200

198-
var res alertmgr_cfg.Config
199-
err = yaml.Unmarshal(generatedConfig, &res)
201+
res, err := alertmgr_cfg.Unmarshal(generatedConfig)
200202
if err != nil {
201203
return nil, fmt.Errorf("failed to unmarshal generated final configuration: %w", err)
202204
}
203205

204-
return &res, nil
206+
return res, nil
205207
}
206208

207209
func (e *eventProcessor) reconcileState(ctx context.Context) error {

internal/component/mimir/alerts/kubernetes/events_test.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"sigs.k8s.io/yaml"
1111

1212
"github.com/go-kit/log"
13+
"github.com/grafana/alloy/internal/mimir/alertmanager"
1314
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
1415
monitoringv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
1516
promListers_v1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/client/listers/monitoring/v1alpha1"
16-
"github.com/prometheus/alertmanager/config"
1717
"github.com/stretchr/testify/assert"
1818
"github.com/stretchr/testify/require"
1919
corev1 "k8s.io/api/core/v1"
@@ -33,7 +33,7 @@ import (
3333

3434
type fakeMimirClient struct {
3535
alertMgrConfigsMut sync.RWMutex
36-
alertMgrConfig config.Config
36+
alertMgrConfig alertmanager.Config
3737
templateFiles map[string]string
3838
}
3939

@@ -43,7 +43,7 @@ func newFakeMimirClient() *fakeMimirClient {
4343
return &fakeMimirClient{}
4444
}
4545

46-
func (m *fakeMimirClient) CreateAlertmanagerConfigs(ctx context.Context, conf *config.Config, templateFiles map[string]string) error {
46+
func (m *fakeMimirClient) CreateAlertmanagerConfigs(ctx context.Context, conf *alertmanager.Config, templateFiles map[string]string) error {
4747
m.alertMgrConfigsMut.Lock()
4848
defer m.alertMgrConfigsMut.Unlock()
4949
// These are just shallow copies, but it should be sufficient.
@@ -52,19 +52,16 @@ func (m *fakeMimirClient) CreateAlertmanagerConfigs(ctx context.Context, conf *c
5252
return nil
5353
}
5454

55-
func (m *fakeMimirClient) getAlertmanagerConfig() config.Config {
55+
func (m *fakeMimirClient) getAlertmanagerConfig() alertmanager.Config {
5656
m.alertMgrConfigsMut.RLock()
5757
defer m.alertMgrConfigsMut.RUnlock()
5858
return m.alertMgrConfig
5959
}
6060

61-
func convertToAlertmanagerType(t *testing.T, alertmanagerConf string) config.Config {
62-
config.MarshalSecretValue = true
63-
64-
var res config.Config
65-
err := yaml.Unmarshal([]byte(alertmanagerConf), &res)
61+
func convertToAlertmanagerType(t *testing.T, alertmanagerConf string) alertmanager.Config {
62+
cfg, err := alertmanager.Unmarshal([]byte(alertmanagerConf))
6663
assert.NoError(t, err)
67-
return res
64+
return *cfg
6865
}
6966

7067
// createTestLoggerWithBuffer creates a logger that writes to a thread-safe buffer for testing
@@ -94,7 +91,6 @@ global:
9491
smtp_require_tls: true
9592
route:
9693
receiver: "null"
97-
continue: false
9894
receivers:
9995
- name: "null"
10096
templates: []`
@@ -152,7 +148,6 @@ spec:
152148
smtp_require_tls: true
153149
route:
154150
receiver: "null"
155-
continue: false
156151
routes:
157152
- receiver: mynamespace/alertmgr-config1/null
158153
matchers:
@@ -166,14 +161,9 @@ receivers:
166161
- name: mynamespace/alertmgr-config1/null
167162
- name: mynamespace/alertmgr-config1/myamc
168163
webhook_configs:
169-
- send_resolved: false
170-
http_config:
164+
- http_config:
171165
follow_redirects: true
172-
enable_http2: true
173166
url: http://test.url
174-
url_file: ""
175-
max_alerts: 0
176-
timeout: 0s
177167
templates: []`
178168

179169
final_amConf_1_and_2 := `global:
@@ -185,7 +175,6 @@ templates: []`
185175
smtp_require_tls: true
186176
route:
187177
receiver: "null"
188-
continue: false
189178
routes:
190179
- receiver: mynamespace/alertmgr-config1/null
191180
matchers:
@@ -202,21 +191,15 @@ route:
202191
- receiver: mynamespace/alertmgr-config2/database-pager
203192
matchers:
204193
- "service=\"webapp\""
205-
continue: false
206194
group_wait: 10s
207195
receivers:
208196
- name: "null"
209197
- name: mynamespace/alertmgr-config1/null
210198
- name: mynamespace/alertmgr-config1/myamc
211199
webhook_configs:
212-
- send_resolved: false
213-
timeout: 0s
214-
http_config:
200+
- http_config:
215201
follow_redirects: true
216-
enable_http2: true
217202
url: http://test.url
218-
url_file: ""
219-
max_alerts: 0
220203
- name: mynamespace/alertmgr-config2/null
221204
- name: mynamespace/alertmgr-config2/database-pager
222205
templates: []`
@@ -405,7 +388,8 @@ spec:
405388

406389
// Wait for the configs to be added to mimir
407390
require.EventuallyWithT(t, func(c *assert.CollectT) {
408-
actual := mimirClient.getAlertmanagerConfig().String()
391+
actual, err := mimirClient.getAlertmanagerConfig().String()
392+
require.NoError(c, err)
409393
require.YAMLEq(c, tt.want, actual, "want", tt.want, "actual", actual)
410394
}, 10*time.Second, 100*time.Millisecond)
411395

0 commit comments

Comments
 (0)