From 316c8b50bcb6c34dc18faa7240701cfd3e1eb8e6 Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 30 Nov 2023 15:18:56 +0100 Subject: [PATCH] Alerting: Add SaveAndApply methods to the forked Alertmanager (remote secondary) (#78827) * Alerting: Add configuration methods to the forked Alertmanager for remote secondary modes * update comments --- .../remote/forked_alertmanager_test.go | 30 +++++++++++++++++-- .../remote_secondary_forked_alertmanager.go | 6 ++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/pkg/services/ngalert/remote/forked_alertmanager_test.go b/pkg/services/ngalert/remote/forked_alertmanager_test.go index de1af60b623..e89d9a89e2b 100644 --- a/pkg/services/ngalert/remote/forked_alertmanager_test.go +++ b/pkg/services/ngalert/remote/forked_alertmanager_test.go @@ -21,6 +21,32 @@ func TestForkedAlertmanager_ModeRemoteSecondary(t *testing.T) { ctx := context.Background() expErr := errors.New("test error") + t.Run("SaveAndApplyConfig", func(tt *testing.T) { + // SaveAndApplyConfig should only be called on the remote Alertmanager. + // State and configuration are updated on an interval. + internal, _, forked := genTestAlertmanagers(tt, modeRemoteSecondary) + internal.EXPECT().SaveAndApplyConfig(ctx, mock.Anything).Return(nil).Once() + require.NoError(tt, forked.SaveAndApplyConfig(ctx, &apimodels.PostableUserConfig{})) + + // If there's an error, it should be returned. + internal, _, forked = genTestAlertmanagers(tt, modeRemoteSecondary) + internal.EXPECT().SaveAndApplyConfig(ctx, mock.Anything).Return(expErr).Once() + require.ErrorIs(tt, forked.SaveAndApplyConfig(ctx, &apimodels.PostableUserConfig{}), expErr) + }) + + t.Run("SaveAndApplyDefaultConfig", func(tt *testing.T) { + // SaveAndApplyDefaultConfig should only be called on the remote Alertmanager. + // State and configuration are updated on an interval. + internal, _, forked := genTestAlertmanagers(tt, modeRemoteSecondary) + internal.EXPECT().SaveAndApplyDefaultConfig(ctx).Return(nil).Once() + require.NoError(tt, forked.SaveAndApplyDefaultConfig(ctx)) + + // If there's an error, it should be returned. + internal, _, forked = genTestAlertmanagers(tt, modeRemoteSecondary) + internal.EXPECT().SaveAndApplyDefaultConfig(ctx).Return(expErr).Once() + require.ErrorIs(tt, forked.SaveAndApplyDefaultConfig(ctx), expErr) + }) + t.Run("GetStatus", func(tt *testing.T) { // We care about the status of the internal Alertmanager. internal, _, forked := genTestAlertmanagers(tt, modeRemoteSecondary) @@ -225,7 +251,7 @@ func TestForkedAlertmanager_ModeRemoteSecondary(t *testing.T) { }) t.Run("StopAndWait", func(tt *testing.T) { - // StopAndWait should be called in both Alertmanagers. + // StopAndWait should be called on both Alertmanagers. internal, remote, forked := genTestAlertmanagers(tt, modeRemotePrimary) internal.EXPECT().StopAndWait().Once() remote.EXPECT().StopAndWait().Once() @@ -457,7 +483,7 @@ func TestForkedAlertmanager_ModeRemotePrimary(t *testing.T) { }) t.Run("StopAndWait", func(tt *testing.T) { - // StopAndWait should be called in both Alertmanagers. + // StopAndWait should be called on both Alertmanagers. internal, remote, forked := genTestAlertmanagers(tt, modeRemotePrimary) internal.EXPECT().StopAndWait().Once() remote.EXPECT().StopAndWait().Once() diff --git a/pkg/services/ngalert/remote/remote_secondary_forked_alertmanager.go b/pkg/services/ngalert/remote/remote_secondary_forked_alertmanager.go index 4d0f64e84f7..d913a89f16e 100644 --- a/pkg/services/ngalert/remote/remote_secondary_forked_alertmanager.go +++ b/pkg/services/ngalert/remote/remote_secondary_forked_alertmanager.go @@ -24,12 +24,14 @@ func (fam *RemoteSecondaryForkedAlertmanager) ApplyConfig(ctx context.Context, c return nil } +// SaveAndApplyConfig is only called on the internal Alertmanager when running in remote secondary mode. func (fam *RemoteSecondaryForkedAlertmanager) SaveAndApplyConfig(ctx context.Context, config *apimodels.PostableUserConfig) error { - return nil + return fam.internal.SaveAndApplyConfig(ctx, config) } +// SaveAndApplyDefaultConfig is only called on the internal Alertmanager when running in remote secondary mode. func (fam *RemoteSecondaryForkedAlertmanager) SaveAndApplyDefaultConfig(ctx context.Context) error { - return nil + return fam.internal.SaveAndApplyDefaultConfig(ctx) } func (fam *RemoteSecondaryForkedAlertmanager) GetStatus() apimodels.GettableStatus {