diff --git a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go index cc3b57713c4..aa698d53f25 100644 --- a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go +++ b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go @@ -739,12 +739,12 @@ func (c *GettableApiAlertingConfig) GetReceivers() []*GettableApiReceiver { return c.Receivers } -func (c *GettableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals } - func (c *GettableApiAlertingConfig) GetMuteTimeIntervals() []config.MuteTimeInterval { return c.MuteTimeIntervals } +func (c *GettableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals } + func (c *GettableApiAlertingConfig) GetRoute() *Route { return c.Route } @@ -802,11 +802,12 @@ func (c *GettableApiAlertingConfig) validate() error { // Config is the top-level configuration for Alertmanager's config files. type Config struct { - Global *config.GlobalConfig `yaml:"global,omitempty" json:"global,omitempty"` - Route *Route `yaml:"route,omitempty" json:"route,omitempty"` - InhibitRules []config.InhibitRule `yaml:"inhibit_rules,omitempty" json:"inhibit_rules,omitempty"` - TimeIntervals []config.TimeInterval `yaml:"time_intervals,omitempty" json:"time_intervals,omitempty"` + Global *config.GlobalConfig `yaml:"global,omitempty" json:"global,omitempty"` + Route *Route `yaml:"route,omitempty" json:"route,omitempty"` + InhibitRules []config.InhibitRule `yaml:"inhibit_rules,omitempty" json:"inhibit_rules,omitempty"` + // MuteTimeIntervals is deprecated and will be removed before Alertmanager 1.0. MuteTimeIntervals []config.MuteTimeInterval `yaml:"mute_time_intervals,omitempty" json:"mute_time_intervals,omitempty"` + TimeIntervals []config.TimeInterval `yaml:"time_intervals,omitempty" json:"time_intervals,omitempty"` Templates []string `yaml:"templates" json:"templates"` } @@ -939,15 +940,6 @@ func (c *Config) UnmarshalJSON(b []byte) error { } tiNames := make(map[string]struct{}) - for _, ti := range c.TimeIntervals { - if ti.Name == "" { - return fmt.Errorf("missing name in time interval") - } - if _, ok := tiNames[ti.Name]; ok { - return fmt.Errorf("time interval %q is not unique", ti.Name) - } - tiNames[ti.Name] = struct{}{} - } for _, mt := range c.MuteTimeIntervals { if mt.Name == "" { return fmt.Errorf("missing name in mute time interval") @@ -957,6 +949,15 @@ func (c *Config) UnmarshalJSON(b []byte) error { } tiNames[mt.Name] = struct{}{} } + for _, ti := range c.TimeIntervals { + if ti.Name == "" { + return fmt.Errorf("missing name in time interval") + } + if _, ok := tiNames[ti.Name]; ok { + return fmt.Errorf("time interval %q is not unique", ti.Name) + } + tiNames[ti.Name] = struct{}{} + } return checkTimeInterval(c.Route, tiNames) } @@ -988,12 +989,12 @@ func (c *PostableApiAlertingConfig) GetReceivers() []*PostableApiReceiver { return c.Receivers } -func (c *PostableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals } - func (c *PostableApiAlertingConfig) GetMuteTimeIntervals() []config.MuteTimeInterval { return c.MuteTimeIntervals } +func (c *PostableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals } + func (c *PostableApiAlertingConfig) GetRoute() *Route { return c.Route } diff --git a/pkg/services/ngalert/api/tooling/definitions/alertmanager_test.go b/pkg/services/ngalert/api/tooling/definitions/alertmanager_test.go index e38506ca5df..4f0504e350a 100644 --- a/pkg/services/ngalert/api/tooling/definitions/alertmanager_test.go +++ b/pkg/services/ngalert/api/tooling/definitions/alertmanager_test.go @@ -485,49 +485,6 @@ func Test_ConfigUnmashaling(t *testing.T) { err error }{ { - desc: "missing time interval name should error", - err: errors.New("missing name in time interval"), - input: ` - { - "route": { - "receiver": "grafana-default-email" - }, - "time_intervals": [ - { - "name": "", - "time_intervals": [ - { - "times": [ - { - "start_time": "00:00", - "end_time": "12:00" - } - ] - } - ] - } - ], - "templates": null, - "receivers": [ - { - "name": "grafana-default-email", - "grafana_managed_receiver_configs": [ - { - "uid": "uxwfZvtnz", - "name": "email receiver", - "type": "email", - "disableResolveMessage": false, - "settings": { - "addresses": "" - }, - "secureFields": {} - } - ] - } - ] - } - `, - }, { desc: "missing mute time interval name should error", err: errors.New("missing name in mute time interval"), input: ` @@ -572,8 +529,8 @@ func Test_ConfigUnmashaling(t *testing.T) { `, }, { - desc: "duplicate time interval names should error", - err: errors.New("time interval \"test1\" is not unique"), + desc: "missing time interval name should error", + err: errors.New("missing name in time interval"), input: ` { "route": { @@ -581,7 +538,7 @@ func Test_ConfigUnmashaling(t *testing.T) { }, "time_intervals": [ { - "name": "test1", + "name": "", "time_intervals": [ { "times": [ @@ -592,20 +549,7 @@ func Test_ConfigUnmashaling(t *testing.T) { ] } ] - }, - { - "name": "test1", - "time_intervals": [ - { - "times": [ - { - "start_time": "00:00", - "end_time": "12:00" - } - ] - } - ] - } + } ], "templates": null, "receivers": [ @@ -685,9 +629,66 @@ func Test_ConfigUnmashaling(t *testing.T) { } `, }, + { + desc: "duplicate time interval names should error", + err: errors.New("time interval \"test1\" is not unique"), + input: ` + { + "route": { + "receiver": "grafana-default-email" + }, + "time_intervals": [ + { + "name": "test1", + "time_intervals": [ + { + "times": [ + { + "start_time": "00:00", + "end_time": "12:00" + } + ] + } + ] + }, + { + "name": "test1", + "time_intervals": [ + { + "times": [ + { + "start_time": "00:00", + "end_time": "12:00" + } + ] + } + ] + } + ], + "templates": null, + "receivers": [ + { + "name": "grafana-default-email", + "grafana_managed_receiver_configs": [ + { + "uid": "uxwfZvtnz", + "name": "email receiver", + "type": "email", + "disableResolveMessage": false, + "settings": { + "addresses": "" + }, + "secureFields": {} + } + ] + } + ] + } + `, + }, { desc: "duplicate time and mute time interval names should error", - err: errors.New("mute time interval \"test1\" is not unique"), + err: errors.New("time interval \"test1\" is not unique"), input: ` { "route": { diff --git a/pkg/services/ngalert/notifier/config.go b/pkg/services/ngalert/notifier/config.go index 43b70760a2f..0ea24acf64b 100644 --- a/pkg/services/ngalert/notifier/config.go +++ b/pkg/services/ngalert/notifier/config.go @@ -111,14 +111,14 @@ func (a AlertingConfiguration) InhibitRules() []alertingNotify.InhibitRule { return a.alertmanagerConfig.InhibitRules } -func (a AlertingConfiguration) TimeIntervals() []alertingNotify.TimeInterval { - return a.alertmanagerConfig.TimeIntervals -} - func (a AlertingConfiguration) MuteTimeIntervals() []alertingNotify.MuteTimeInterval { return a.alertmanagerConfig.MuteTimeIntervals } +func (a AlertingConfiguration) TimeIntervals() []alertingNotify.TimeInterval { + return a.alertmanagerConfig.TimeIntervals +} + func (a AlertingConfiguration) Receivers() []*alertingNotify.APIReceiver { return a.receivers } diff --git a/pkg/services/ngalert/notifier/validation.go b/pkg/services/ngalert/notifier/validation.go index 84964214a23..48e0f20d2ad 100644 --- a/pkg/services/ngalert/notifier/validation.go +++ b/pkg/services/ngalert/notifier/validation.go @@ -27,8 +27,8 @@ type staticValidator struct { // apiAlertingConfig contains the methods required to validate NotificationSettings and create autogen routes. type apiAlertingConfig[R receiver] interface { GetReceivers() []R - GetTimeIntervals() []config.TimeInterval GetMuteTimeIntervals() []config.MuteTimeInterval + GetTimeIntervals() []config.TimeInterval GetRoute() *definitions.Route } @@ -44,10 +44,10 @@ func NewNotificationSettingsValidator[R receiver](am apiAlertingConfig[R]) Notif } availableTimeIntervals := make(map[string]struct{}) - for _, interval := range am.GetTimeIntervals() { + for _, interval := range am.GetMuteTimeIntervals() { availableTimeIntervals[interval.Name] = struct{}{} } - for _, interval := range am.GetMuteTimeIntervals() { + for _, interval := range am.GetTimeIntervals() { availableTimeIntervals[interval.Name] = struct{}{} } diff --git a/pkg/tests/api/alerting/api_alertmanager_configuration_test.go b/pkg/tests/api/alerting/api_alertmanager_configuration_test.go index f4e09604938..52a2ad46941 100644 --- a/pkg/tests/api/alerting/api_alertmanager_configuration_test.go +++ b/pkg/tests/api/alerting/api_alertmanager_configuration_test.go @@ -194,7 +194,10 @@ func TestIntegrationAlertmanagerConfiguration(t *testing.T) { }, }, }, { - name: "configuration with time intervals", + // TODO: Mute time intervals is deprecated in Alertmanager and scheduled to be + // removed before version 1.0. Remove this test when support for mute time + // intervals is removed. + name: "configuration with mute time intervals", cfg: apimodels.PostableUserConfig{ AlertmanagerConfig: apimodels.PostableApiAlertingConfig{ Config: apimodels.Config{ @@ -204,7 +207,7 @@ func TestIntegrationAlertmanagerConfiguration(t *testing.T) { MuteTimeIntervals: []string{"weekends"}, }}, }, - TimeIntervals: []config.TimeInterval{{ + MuteTimeIntervals: []config.MuteTimeInterval{{ Name: "weekends", TimeIntervals: []timeinterval.TimeInterval{{ Weekdays: []timeinterval.WeekdayRange{{ @@ -224,10 +227,7 @@ func TestIntegrationAlertmanagerConfiguration(t *testing.T) { }, }, }, { - // TODO: Mute time intervals is deprecated in Alertmanager and scheduled to be - // removed before version 1.0. Remove this test when support for mute time - // intervals is removed. - name: "configuration with mute time intervals", + name: "configuration with time intervals", cfg: apimodels.PostableUserConfig{ AlertmanagerConfig: apimodels.PostableApiAlertingConfig{ Config: apimodels.Config{ @@ -237,7 +237,7 @@ func TestIntegrationAlertmanagerConfiguration(t *testing.T) { MuteTimeIntervals: []string{"weekends"}, }}, }, - MuteTimeIntervals: []config.MuteTimeInterval{{ + TimeIntervals: []config.TimeInterval{{ Name: "weekends", TimeIntervals: []timeinterval.TimeInterval{{ Weekdays: []timeinterval.WeekdayRange{{