From c2733565b786c1f47e47b47d7d50eefd476c35bb Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:57:07 +0100 Subject: [PATCH 1/2] Update module github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring to v0.79.1 (#206) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index cbf739a6..d828ea9b 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/onsi/gomega v1.36.1 github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.23 github.com/pkg/errors v0.9.1 - github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.79.0 + github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.79.1 github.com/prometheus/alertmanager v0.27.0 github.com/prometheus/client_golang v1.20.5 github.com/prometheus/common v0.61.0 diff --git a/go.sum b/go.sum index 3c0258c3..c1457dce 100644 --- a/go.sum +++ b/go.sum @@ -324,8 +324,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.79.0 h1:IiCqr23V8SexkXkPmK+6tS/Ped/oCVhXSSmLacEATy4= -github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.79.0/go.mod h1:AVMP4QEW8xuGWnxaWSpI3kKjP9fDA31nO68zsyREJZA= +github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.79.1 h1:Dwk9xYZOd8gq+nhlZREvHbQ6enj3yjC5HPFOdcReqGw= +github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.79.1/go.mod h1:AVMP4QEW8xuGWnxaWSpI3kKjP9fDA31nO68zsyREJZA= github.com/prometheus/alertmanager v0.27.0 h1:V6nTa2J5V4s8TG4C4HtrBP/WNSebCCTYGGv4qecA/+I= github.com/prometheus/alertmanager v0.27.0/go.mod h1:8Ia/R3urPmbzJ8OsdvmZvIprDwvwmYCmUbwBL+jlPOE= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= From ae3a61108bbd5c4c78cefdc57840f03e14f18c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Brigitte?= Date: Tue, 17 Dec 2024 14:16:44 +0100 Subject: [PATCH 2/2] Fix sending invalid Alertmanager configuration (#205) * Set alertmanager controller name --- .../controller/alertmanager_controller.go | 1 + pkg/alertmanager/alertmanager.go | 19 ++++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) 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) diff --git a/pkg/alertmanager/alertmanager.go b/pkg/alertmanager/alertmanager.go index a2c71864..07ad4ca4 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" @@ -89,33 +87,31 @@ 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 - alertmanagerConfig, err := config.Load(string(alertmanagerConfigContent)) + // 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)) } - // 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, + 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)) } + dataLen := 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 +119,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(dataLen) resp, err := http.DefaultClient.Do(req) if err != nil {