Remote Alertmanager: Remove unneeded SmtpFrom and StaticHeaders fields (#108781)

* Remote Alertmanager: Remove unneeded SmtpFrom and StaticHeaders fields

* remove smtpFrom (am struct)

* move smtp field (am struct)

* fix test
This commit is contained in:
Santiago
2025-07-29 12:14:42 +02:00
committed by GitHub
parent 51a5b0ab65
commit 3c4c3b3f5c
5 changed files with 11 additions and 56 deletions
-4
View File
@@ -216,10 +216,6 @@ func (ng *AlertNG) init() error {
ExternalURL: ng.Cfg.AppURL,
SmtpConfig: smtpCfg,
Timeout: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.Timeout,
// TODO: Remove once everything can be sent in the 'smtp_config' field.
SmtpFrom: ng.Cfg.Smtp.FromAddress,
StaticHeaders: ng.Cfg.Smtp.StaticHeaders,
}
autogenFn := func(ctx context.Context, logger log.Logger, orgID int64, cfg *definitions.PostableApiAlertingConfig, skipInvalid bool) error {
return notifier.AddAutogenConfig(ctx, logger, ng.store, orgID, cfg, skipInvalid)
+2 -23
View File
@@ -63,7 +63,7 @@ type Alertmanager struct {
orgID int64
ready bool
sender *sender.ExternalAlertmanager
smtpFrom string
smtp remoteClient.SmtpConfig
state stateStore
tenantID string
url string
@@ -73,8 +73,6 @@ type Alertmanager struct {
amClient *remoteClient.Alertmanager
mimirClient remoteClient.MimirClient
smtp remoteClient.SmtpConfig
}
type AlertmanagerConfig struct {
@@ -100,11 +98,6 @@ type AlertmanagerConfig struct {
// Timeout for the HTTP client.
Timeout time.Duration
// TODO: Remove once everything can be send in the 'smtp_config' field.
// SmtpFrom and StaticHeaders are used in email notifications sent by the remote Alertmanager.
SmtpFrom string
StaticHeaders map[string]string
}
func (cfg *AlertmanagerConfig) Validate() error {
@@ -140,12 +133,7 @@ func NewAlertmanager(ctx context.Context, cfg AlertmanagerConfig, store stateSto
URL: u,
PromoteConfig: cfg.PromoteConfig,
ExternalURL: cfg.ExternalURL,
Smtp: cfg.SmtpConfig,
// TODO: Remove once everything can be sent in the 'smtp_config' field.
SmtpFrom: cfg.SmtpFrom,
StaticHeaders: cfg.StaticHeaders,
Smtp: cfg.SmtpConfig,
}
mc, err := remoteClient.New(mcCfg, metrics, tracer)
if err != nil {
@@ -201,9 +189,6 @@ func NewAlertmanager(ctx context.Context, cfg AlertmanagerConfig, store stateSto
tenantID: cfg.TenantID,
url: cfg.URL,
smtp: cfg.SmtpConfig,
// TODO: Remove once it can be sent only in the 'smtp_config' field.
smtpFrom: cfg.SmtpFrom,
}
// Parse the default configuration once and remember its hash so we can compare it later.
@@ -722,12 +707,6 @@ func (am *Alertmanager) shouldSendConfig(ctx context.Context, hash string) bool
return true
}
// TODO: Remove when the from address can be sent only in the 'smtp_config' field.
if rc.SmtpFrom != am.smtpFrom {
am.log.Debug("SMTP 'from' address is different, sending the configuration to the remote Alertmanager", "remote", rc.SmtpFrom, "local", am.smtpFrom)
return true
}
// Compare SMTP configs.
if rc.SmtpConfig.EhloIdentity != am.smtp.EhloIdentity ||
rc.SmtpConfig.Password != am.smtp.Password ||
@@ -281,7 +281,9 @@ func TestIntegrationApplyConfig(t *testing.T) {
if r.Method == http.MethodPost {
if strings.Contains(r.URL.Path, "/config") {
require.NoError(t, json.NewDecoder(r.Body).Decode(&configSent))
var cfg client.UserGrafanaConfig
require.NoError(t, json.NewDecoder(r.Body).Decode(&cfg))
configSent = cfg
configSyncs++
} else {
stateSyncs++
@@ -324,11 +326,9 @@ func TestIntegrationApplyConfig(t *testing.T) {
SyncInterval: 1 * time.Hour,
ExternalURL: "https://test.grafana.com",
SmtpConfig: client.SmtpConfig{
FromAddress: "test-instance@grafana.net",
FromAddress: "test-instance@grafana.net",
StaticHeaders: map[string]string{"Header-1": "Value-1", "Header-2": "Value-2"},
},
SmtpFrom: "test-instance@grafana.net",
StaticHeaders: map[string]string{"Header-1": "Value-1", "Header-2": "Value-2"},
}
ctx := context.Background()
@@ -365,8 +365,8 @@ func TestIntegrationApplyConfig(t *testing.T) {
// Grafana's URL, email "from" address, and static headers should be sent alongside the configuration.
require.Equal(t, cfg.ExternalURL, configSent.ExternalURL)
require.Equal(t, cfg.SmtpFrom, configSent.SmtpFrom)
require.Equal(t, cfg.StaticHeaders, configSent.StaticHeaders)
require.Equal(t, cfg.SmtpConfig.FromAddress, configSent.SmtpConfig.FromAddress)
require.Equal(t, cfg.SmtpConfig.StaticHeaders, configSent.SmtpConfig.StaticHeaders)
// If we already got a 200 status code response and the sync interval hasn't elapsed,
// we shouldn't send the state/configuration again.
@@ -391,12 +391,12 @@ func TestIntegrationApplyConfig(t *testing.T) {
require.Equal(t, 2, configSyncs)
// Changing the "from" address should result in the configuration being updated.
cfg.SmtpFrom = "new-address@test.com"
cfg.SmtpConfig.FromAddress = "new-address@test.com"
am, err = NewAlertmanager(context.Background(), cfg, fstore, notifier.NewCrypto(secretsService, nil, log.NewNopLogger()), NoopAutogenFn, m, tracing.InitializeTracerForTest())
require.NoError(t, err)
require.NoError(t, am.ApplyConfig(ctx, config))
require.Equal(t, 3, configSyncs)
require.Equal(t, am.smtpFrom, configSent.SmtpFrom)
require.Equal(t, am.smtp.FromAddress, configSent.SmtpConfig.FromAddress)
// Changing fields in the SMTP config should result in the configuration being updated.
cfg.SmtpConfig = client.SmtpConfig{
@@ -37,10 +37,6 @@ type UserGrafanaConfig struct {
Promoted bool `json:"promoted"`
ExternalURL string `json:"external_url"`
SmtpConfig SmtpConfig `json:"smtp_config"`
// TODO: Remove once everything can be sent in the 'SmtpConfig' field.
SmtpFrom string `json:"smtp_from"`
StaticHeaders map[string]string `json:"static_headers"`
}
func (mc *Mimir) ShouldPromoteConfig() bool {
@@ -75,10 +71,6 @@ func (mc *Mimir) CreateGrafanaAlertmanagerConfig(ctx context.Context, cfg Grafan
Promoted: mc.promoteConfig,
ExternalURL: mc.externalURL,
SmtpConfig: mc.smtpConfig,
// TODO: Remove once everything can be sent only in the 'smtp_config' field.
SmtpFrom: mc.smtpFrom,
StaticHeaders: mc.staticHeaders,
})
if err != nil {
return err
@@ -50,10 +50,6 @@ type Mimir struct {
promoteConfig bool
externalURL string
smtpConfig SmtpConfig
// TODO: Remove once everything can be sent in the 'smtp' field.
smtpFrom string
staticHeaders map[string]string
}
type SmtpConfig struct {
@@ -77,10 +73,6 @@ type Config struct {
PromoteConfig bool
ExternalURL string
Smtp SmtpConfig
// TODO: Remove once everything can be sent in the 'smtp_config' field.
SmtpFrom string
StaticHeaders map[string]string
}
// successResponse represents a successful response from the Mimir API.
@@ -125,10 +117,6 @@ func New(cfg *Config, metrics *metrics.RemoteAlertmanager, tracer tracing.Tracer
promoteConfig: cfg.PromoteConfig,
externalURL: cfg.ExternalURL,
smtpConfig: cfg.Smtp,
// TODO: Remove once everything can be sent in the 'smtp_config' field.
smtpFrom: cfg.SmtpFrom,
staticHeaders: cfg.StaticHeaders,
}, nil
}