Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sending invalid Alertmanager configuration #205

Merged
merged 7 commits into from
Dec 17, 2024
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
1 change: 1 addition & 0 deletions internal/controller/alertmanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func SetupAlertmanagerReconciler(mgr ctrl.Manager, conf config.Config) error {

// Setup the controller
return ctrl.NewControllerManagedBy(mgr).
Named("alertmanager").
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this otherwise the controller shows up as "secret" in the logs

image

For(&v1.Secret{}, builder.WithPredicates(secretPredicate)).
Watches(&v1.Pod{}, p, builder.WithPredicates(podPredicate)).
Complete(r)
Expand Down
19 changes: 8 additions & 11 deletions pkg/alertmanager/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"context"
"fmt"
"io"
"maps"
"net/http"
"path"
"slices"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -89,40 +87,39 @@ 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 <redacted>.
_, 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))
if err != nil {
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 {
Expand Down
Loading