Alerting: Implement SaveAndApplyDefaultConfig in the forked Alertmanager (remote primary mode) (#85668)

* Alerting: Implement SaveAndApplyDefaultConfig in the forked Alertmanager (remote primary)

* log the error for the internal AM instead of returning it
This commit is contained in:
Santiago 2024-04-23 14:36:40 +02:00 committed by GitHub
parent 0ec9c3e01a
commit 8b7c2a459b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 20 deletions

View File

@ -107,7 +107,7 @@ func TestForkedAlertmanager_ModeRemoteSecondary(t *testing.T) {
})
t.Run("SaveAndApplyDefaultConfig", func(tt *testing.T) {
// SaveAndApplyDefaultConfig should only be called on the remote Alertmanager.
// SaveAndApplyDefaultConfig should only be called on the internal Alertmanager.
// State and configuration are updated on an interval.
internal, _, forked := genTestAlertmanagers(tt, modeRemoteSecondary)
internal.EXPECT().SaveAndApplyDefaultConfig(ctx).Return(nil).Once()
@ -368,25 +368,42 @@ func TestForkedAlertmanager_ModeRemotePrimary(t *testing.T) {
expErr := errors.New("test error")
t.Run("ApplyConfig", func(tt *testing.T) {
{
// If the remote Alertmanager is not ready, ApplyConfig should be called on both Alertmanagers,
// first on the remote, then on the internal.
internal, remote, forked := genTestAlertmanagers(tt, modeRemotePrimary)
remoteCall := remote.EXPECT().ApplyConfig(ctx, mock.Anything).Return(nil).Once()
internal.EXPECT().ApplyConfig(ctx, mock.Anything).Return(nil).Once().NotBefore(remoteCall)
require.NoError(tt, forked.ApplyConfig(ctx, &models.AlertConfiguration{}))
// If the remote Alertmanager is not ready, ApplyConfig should be called on both Alertmanagers,
// first on the remote, then on the internal.
internal, remote, forked := genTestAlertmanagers(tt, modeRemotePrimary)
remoteCall := remote.EXPECT().ApplyConfig(ctx, mock.Anything).Return(nil).Once()
internal.EXPECT().ApplyConfig(ctx, mock.Anything).Return(nil).Once().NotBefore(remoteCall)
require.NoError(tt, forked.ApplyConfig(ctx, &models.AlertConfiguration{}))
// An error in the remote Alertmanager should be returned.
_, remote, forked = genTestAlertmanagers(tt, modeRemotePrimary)
remote.EXPECT().ApplyConfig(ctx, mock.Anything).Return(expErr).Once()
require.ErrorIs(tt, forked.ApplyConfig(ctx, &models.AlertConfiguration{}), expErr)
// An error in the remote Alertmanager should be returned.
_, remote, forked = genTestAlertmanagers(tt, modeRemotePrimary)
remote.EXPECT().ApplyConfig(ctx, mock.Anything).Return(expErr).Once()
require.ErrorIs(tt, forked.ApplyConfig(ctx, &models.AlertConfiguration{}), expErr)
// An error in the internal Alertmanager should not be returned.
internal, remote, forked = genTestAlertmanagers(tt, modeRemotePrimary)
remote.EXPECT().ApplyConfig(ctx, mock.Anything).Return(nil).Once()
internal.EXPECT().ApplyConfig(ctx, mock.Anything).Return(expErr).Once()
require.NoError(tt, forked.ApplyConfig(ctx, &models.AlertConfiguration{}))
}
// An error in the internal Alertmanager should not be returned.
internal, remote, forked = genTestAlertmanagers(tt, modeRemotePrimary)
remote.EXPECT().ApplyConfig(ctx, mock.Anything).Return(nil).Once()
internal.EXPECT().ApplyConfig(ctx, mock.Anything).Return(expErr).Once()
require.NoError(tt, forked.ApplyConfig(ctx, &models.AlertConfiguration{}))
})
t.Run("SaveAndApplyDefaultConfig", func(tt *testing.T) {
// SaveAndApplyDefaultConfig should be called on both Alertmanagers.
internal, remote, forked := genTestAlertmanagers(tt, modeRemotePrimary)
remote.EXPECT().SaveAndApplyDefaultConfig(ctx).Return(nil).Once()
internal.EXPECT().SaveAndApplyDefaultConfig(ctx).Return(nil).Once()
require.NoError(tt, forked.SaveAndApplyDefaultConfig(ctx))
// An error in the remote Alertmanager should be returned.
_, remote, forked = genTestAlertmanagers(tt, modeRemotePrimary)
remote.EXPECT().SaveAndApplyDefaultConfig(ctx).Return(expErr).Once()
require.ErrorIs(tt, forked.SaveAndApplyDefaultConfig(ctx), expErr)
// An error in the internal Alertmanager should not be returned.
internal, remote, forked = genTestAlertmanagers(tt, modeRemotePrimary)
remote.EXPECT().SaveAndApplyDefaultConfig(ctx).Return(nil).Once()
internal.EXPECT().SaveAndApplyDefaultConfig(ctx).Return(expErr).Once()
require.NoError(tt, forked.SaveAndApplyDefaultConfig(ctx))
})
t.Run("GetStatus", func(tt *testing.T) {

View File

@ -39,13 +39,18 @@ func (fam *RemotePrimaryForkedAlertmanager) ApplyConfig(ctx context.Context, con
return nil
}
// TODO: save the new configuration hash in memory.
func (fam *RemotePrimaryForkedAlertmanager) SaveAndApplyConfig(ctx context.Context, config *apimodels.PostableUserConfig) error {
return nil
}
// TODO: save the new configuration hash in memory.
func (fam *RemotePrimaryForkedAlertmanager) SaveAndApplyDefaultConfig(ctx context.Context) error {
if err := fam.remote.SaveAndApplyDefaultConfig(ctx); err != nil {
return fmt.Errorf("failed to send the default configuration to the remote Alertmanager: %w", err)
}
if err := fam.internal.SaveAndApplyDefaultConfig(ctx); err != nil {
fam.log.Error("Error applying the default configuration to the internal Alertmanager", "err", err)
}
return nil
}