Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
##
## Targets for running tests:
##
## test Run tests
## lint Lint code
## integration-test Run integration tests
## test Run tests
## lint Lint code
## integration-test Run integration tests
## integration-test-k8s Run Kubernetes integration tests
##
## Targets for building binaries:
##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ alertmanager_config: |
smtp_require_tls: true
route:
receiver: "null"
continue: false
routes:
- receiver: testing/alertmgr-config1/null
matchers:
Expand All @@ -29,22 +28,16 @@ alertmanager_config: |
- receiver: testing/alertmgr-config2/database-pager
matchers:
- service="webapp"
continue: false
group_wait: 10s
receivers:
- name: "null"
- name: alloy-namespace/global-config/myreceiver
- name: testing/alertmgr-config1/null
- name: testing/alertmgr-config1/myamc
webhook_configs:
- send_resolved: false
- url: http://test.url
http_config:
follow_redirects: true
enable_http2: true
url: http://test.url
url_file: ""
max_alerts: 0
timeout: 0s
- name: testing/alertmgr-config2/null
- name: testing/alertmgr-config2/database-pager
templates:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ alertmanager_config: |
smtp_require_tls: true
route:
receiver: "null"
continue: false
routes:
- receiver: testing/alertmgr-config1/null
matchers:
Expand All @@ -27,13 +26,8 @@ alertmanager_config: |
- name: testing/alertmgr-config1/null
- name: testing/alertmgr-config1/myamc
webhook_configs:
- send_resolved: false
- url: http://test.url
http_config:
follow_redirects: true
enable_http2: true
url: http://test.url
url_file: ""
max_alerts: 0
timeout: 0s
templates:
- default_template
8 changes: 3 additions & 5 deletions internal/component/mimir/alerts/kubernetes/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (
"time"

"github.com/go-kit/log"
alertmgr_cfg "github.com/grafana/alloy/internal/mimir/alertmanager"
"github.com/grafana/dskit/backoff"
alertmgr_cfg "github.com/prometheus/alertmanager/config"
coreListers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/util/workqueue"
_ "k8s.io/component-base/metrics/prometheus/workqueue"
controller "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/yaml"

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

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

c.eventProcessor = c.newEventProcessor(queue, informerStopChan, namespaceLister, cfgLister, baseCfg)
c.eventProcessor = c.newEventProcessor(queue, informerStopChan, namespaceLister, cfgLister, *baseCfg)

go c.eventProcessor.run(ctx)
return nil
Expand Down
16 changes: 9 additions & 7 deletions internal/component/mimir/alerts/kubernetes/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import (

"github.com/blang/semver/v4"
"github.com/go-kit/log"
alertmgr_cfg "github.com/grafana/alloy/internal/mimir/alertmanager"
"github.com/grafana/dskit/instrument"
"github.com/prometheus-operator/prometheus-operator/pkg/alertmanager"
validation_v1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/alertmanager/validation/v1alpha1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
promv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
"github.com/prometheus-operator/prometheus-operator/pkg/assets"
promListers_v1alpha "github.com/prometheus-operator/prometheus-operator/pkg/client/listers/monitoring/v1alpha1"
alertmgr_cfg "github.com/prometheus/alertmanager/config"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/labels"
go_k8s "k8s.io/client-go/kubernetes"
coreListers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/yaml" // Used for CRD compatibility instead of gopkg.in/yaml.v2

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

convertedCfg := c.baseCfg.String()
err := cfgBuilder.InitializeFromRawConfiguration([]byte(convertedCfg))
convertedCfg, err := c.baseCfg.String()
if err != nil {
return nil, err
}

err = cfgBuilder.InitializeFromRawConfiguration([]byte(convertedCfg))
if err != nil {
return nil, fmt.Errorf("failed to initialize from global AlertmangerConfig: %w", err)
}
Expand All @@ -195,13 +198,12 @@ func (c *eventProcessor) provisionAlertmanagerConfiguration(ctx context.Context,
return nil, fmt.Errorf("failed to marshal configuration: %w", err)
}

var res alertmgr_cfg.Config
err = yaml.Unmarshal(generatedConfig, &res)
res, err := alertmgr_cfg.Unmarshal(generatedConfig)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal generated final configuration: %w", err)
}

return &res, nil
return res, nil
}

func (e *eventProcessor) reconcileState(ctx context.Context) error {
Expand Down
38 changes: 11 additions & 27 deletions internal/component/mimir/alerts/kubernetes/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"sigs.k8s.io/yaml"

"github.com/go-kit/log"
"github.com/grafana/alloy/internal/mimir/alertmanager"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoringv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
promListers_v1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/client/listers/monitoring/v1alpha1"
"github.com/prometheus/alertmanager/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand All @@ -33,7 +33,7 @@ import (

type fakeMimirClient struct {
alertMgrConfigsMut sync.RWMutex
alertMgrConfig config.Config
alertMgrConfig alertmanager.Config
templateFiles map[string]string
}

Expand All @@ -43,7 +43,7 @@ func newFakeMimirClient() *fakeMimirClient {
return &fakeMimirClient{}
}

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

func (m *fakeMimirClient) getAlertmanagerConfig() config.Config {
func (m *fakeMimirClient) getAlertmanagerConfig() alertmanager.Config {
m.alertMgrConfigsMut.RLock()
defer m.alertMgrConfigsMut.RUnlock()
return m.alertMgrConfig
}

func convertToAlertmanagerType(t *testing.T, alertmanagerConf string) config.Config {
config.MarshalSecretValue = true

var res config.Config
err := yaml.Unmarshal([]byte(alertmanagerConf), &res)
func convertToAlertmanagerType(t *testing.T, alertmanagerConf string) alertmanager.Config {
cfg, err := alertmanager.Unmarshal([]byte(alertmanagerConf))
assert.NoError(t, err)
return res
return *cfg
}

// createTestLoggerWithBuffer creates a logger that writes to a thread-safe buffer for testing
Expand Down Expand Up @@ -94,7 +91,6 @@ global:
smtp_require_tls: true
route:
receiver: "null"
continue: false
receivers:
- name: "null"
templates: []`
Expand Down Expand Up @@ -152,7 +148,6 @@ spec:
smtp_require_tls: true
route:
receiver: "null"
continue: false
routes:
- receiver: mynamespace/alertmgr-config1/null
matchers:
Expand All @@ -166,14 +161,9 @@ receivers:
- name: mynamespace/alertmgr-config1/null
- name: mynamespace/alertmgr-config1/myamc
webhook_configs:
- send_resolved: false
http_config:
- http_config:
follow_redirects: true
enable_http2: true
url: http://test.url
url_file: ""
max_alerts: 0
timeout: 0s
templates: []`

final_amConf_1_and_2 := `global:
Expand All @@ -185,7 +175,6 @@ templates: []`
smtp_require_tls: true
route:
receiver: "null"
continue: false
routes:
- receiver: mynamespace/alertmgr-config1/null
matchers:
Expand All @@ -202,21 +191,15 @@ route:
- receiver: mynamespace/alertmgr-config2/database-pager
matchers:
- "service=\"webapp\""
continue: false
group_wait: 10s
receivers:
- name: "null"
- name: mynamespace/alertmgr-config1/null
- name: mynamespace/alertmgr-config1/myamc
webhook_configs:
- send_resolved: false
timeout: 0s
http_config:
- http_config:
follow_redirects: true
enable_http2: true
url: http://test.url
url_file: ""
max_alerts: 0
- name: mynamespace/alertmgr-config2/null
- name: mynamespace/alertmgr-config2/database-pager
templates: []`
Expand Down Expand Up @@ -405,7 +388,8 @@ spec:

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

Expand Down
Loading
Loading