From 3e904f77855e171ecef5ad6558c7eeae657edad6 Mon Sep 17 00:00:00 2001 From: Theo Brigitte Date: Mon, 16 Dec 2024 19:51:15 +0100 Subject: [PATCH 1/5] set alertmanager controller name --- internal/controller/alertmanager_controller.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/controller/alertmanager_controller.go b/internal/controller/alertmanager_controller.go index 8799353b..d8efc62c 100644 --- a/internal/controller/alertmanager_controller.go +++ b/internal/controller/alertmanager_controller.go @@ -46,6 +46,7 @@ func SetupAlertmanagerReconciler(mgr ctrl.Manager, conf config.Config) error { // Setup the controller return ctrl.NewControllerManagedBy(mgr). + Named("alertmanager"). For(&v1.Secret{}, builder.WithPredicates(secretPredicate)). Watches(&v1.Pod{}, p, builder.WithPredicates(podPredicate)). Complete(r) From 39279472b353065c4a1ca17eaffcf13e8790481e Mon Sep 17 00:00:00 2001 From: Theo Brigitte Date: Tue, 17 Dec 2024 10:28:23 +0100 Subject: [PATCH 2/5] Fix incorrect config sent --- pkg/alertmanager/alertmanager.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/pkg/alertmanager/alertmanager.go b/pkg/alertmanager/alertmanager.go index a2c71864..f973c5cf 100644 --- a/pkg/alertmanager/alertmanager.go +++ b/pkg/alertmanager/alertmanager.go @@ -5,10 +5,8 @@ import ( "context" "fmt" "io" - "maps" "net/http" "path" - "slices" "strings" "github.com/pkg/errors" @@ -94,28 +92,24 @@ func (s Service) configure(ctx context.Context, alertmanagerConfigContent []byte logger := log.FromContext(ctx) // Load alertmanager configuration - alertmanagerConfig, err := config.Load(string(alertmanagerConfigContent)) + _, err := config.Load(string(alertmanagerConfigContent)) if err != nil { return errors.WithStack(fmt.Errorf("alertmanager: failed to load configuration: %w", err)) } - // Set template names - // Values set here must match the keys set in requestData.TemplateFiles - alertmanagerConfig.Templates = slices.Collect(maps.Keys(templates)) - alertmanagerConfigString := alertmanagerConfig.String() - // Prepare request for Alertmanager API - requestData := configRequest{ - AlertmanagerConfig: alertmanagerConfigString, + requestData := &configRequest{ + AlertmanagerConfig: string(alertmanagerConfigContent), TemplateFiles: templates, } data, err := yaml.Marshal(requestData) if err != nil { return errors.WithStack(fmt.Errorf("alertmanager: failed to marshal yaml: %w", err)) } + dateLen := len(data) url := s.alertmanagerURL + alertmanagerAPIPath - logger.WithValues("url", url, "data_size", len(data), "config_size", len(alertmanagerConfigString), "templates_count", len(templates)).Info("Alertmanager: sending configuration") + logger.WithValues("url", url, "data_size", dataLen, "config_size", len(alertmanagerConfigContent), "templates_count", len(templates)).Info("Alertmanager: sending configuration") // Send request to Alertmanager's API req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) @@ -123,6 +117,7 @@ func (s Service) configure(ctx context.Context, alertmanagerConfigContent []byte return errors.WithStack(fmt.Errorf("alertmanager: failed to create request: %w", err)) } req.Header.Set(common.OrgIDHeader, tenantID) + req.ContentLength = int64(dateLen) resp, err := http.DefaultClient.Do(req) if err != nil { From fee9354aa42b877070261f42c7a8192440334ed7 Mon Sep 17 00:00:00 2001 From: Theo Brigitte Date: Tue, 17 Dec 2024 10:35:52 +0100 Subject: [PATCH 3/5] incorrect dataLen --- pkg/alertmanager/alertmanager.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/alertmanager/alertmanager.go b/pkg/alertmanager/alertmanager.go index f973c5cf..8d49248c 100644 --- a/pkg/alertmanager/alertmanager.go +++ b/pkg/alertmanager/alertmanager.go @@ -106,7 +106,7 @@ func (s Service) configure(ctx context.Context, alertmanagerConfigContent []byte if err != nil { return errors.WithStack(fmt.Errorf("alertmanager: failed to marshal yaml: %w", err)) } - dateLen := len(data) + dataLen := len(data) url := s.alertmanagerURL + alertmanagerAPIPath logger.WithValues("url", url, "data_size", dataLen, "config_size", len(alertmanagerConfigContent), "templates_count", len(templates)).Info("Alertmanager: sending configuration") @@ -117,7 +117,7 @@ func (s Service) configure(ctx context.Context, alertmanagerConfigContent []byte return errors.WithStack(fmt.Errorf("alertmanager: failed to create request: %w", err)) } req.Header.Set(common.OrgIDHeader, tenantID) - req.ContentLength = int64(dateLen) + req.ContentLength = int64(dataLen) resp, err := http.DefaultClient.Do(req) if err != nil { From 5ee639c88cc125bb4583db93d95537282fc78abc Mon Sep 17 00:00:00 2001 From: Theo Brigitte Date: Tue, 17 Dec 2024 12:18:21 +0100 Subject: [PATCH 4/5] add comments --- pkg/alertmanager/alertmanager.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/alertmanager/alertmanager.go b/pkg/alertmanager/alertmanager.go index 8d49248c..2f022cfa 100644 --- a/pkg/alertmanager/alertmanager.go +++ b/pkg/alertmanager/alertmanager.go @@ -87,11 +87,13 @@ func (s Service) Configure(ctx context.Context, secret *v1.Secret) error { } // configure sends the configuration and templates to Mimir Alertmanager's API +// It is the caller responsibility to make sure templates names are valid (do not contain any path), and that templates are referenced in the configuration. // https://grafana.com/docs/mimir/latest/references/http-api/#set-alertmanager-configuration func (s Service) configure(ctx context.Context, alertmanagerConfigContent []byte, templates map[string]string, tenantID string) error { logger := log.FromContext(ctx) - // Load alertmanager configuration + // Validate Alertmanager configuration + // The returned config is not used, as transforming it via String() would produce an invalid configuration with all secrets replaced with . _, err := config.Load(string(alertmanagerConfigContent)) if err != nil { return errors.WithStack(fmt.Errorf("alertmanager: failed to load configuration: %w", err)) From 2f69bb96a0a545deac0d636d09d738a2878e954c Mon Sep 17 00:00:00 2001 From: Theo Brigitte Date: Tue, 17 Dec 2024 12:29:35 +0100 Subject: [PATCH 5/5] no pointer needed --- pkg/alertmanager/alertmanager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/alertmanager/alertmanager.go b/pkg/alertmanager/alertmanager.go index 2f022cfa..07ad4ca4 100644 --- a/pkg/alertmanager/alertmanager.go +++ b/pkg/alertmanager/alertmanager.go @@ -100,7 +100,7 @@ func (s Service) configure(ctx context.Context, alertmanagerConfigContent []byte } // Prepare request for Alertmanager API - requestData := &configRequest{ + requestData := configRequest{ AlertmanagerConfig: string(alertmanagerConfigContent), TemplateFiles: templates, }