Alerting: Keep order of time and mute time intervals consistent (#83257)

This commit is contained in:
George Robinson
2024-02-22 16:57:20 +00:00
committed by GitHub
parent 1ef2e8d366
commit a564c8c439
5 changed files with 94 additions and 92 deletions

View File

@ -739,12 +739,12 @@ func (c *GettableApiAlertingConfig) GetReceivers() []*GettableApiReceiver {
return c.Receivers return c.Receivers
} }
func (c *GettableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals }
func (c *GettableApiAlertingConfig) GetMuteTimeIntervals() []config.MuteTimeInterval { func (c *GettableApiAlertingConfig) GetMuteTimeIntervals() []config.MuteTimeInterval {
return c.MuteTimeIntervals return c.MuteTimeIntervals
} }
func (c *GettableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals }
func (c *GettableApiAlertingConfig) GetRoute() *Route { func (c *GettableApiAlertingConfig) GetRoute() *Route {
return c.Route return c.Route
} }
@ -802,11 +802,12 @@ func (c *GettableApiAlertingConfig) validate() error {
// Config is the top-level configuration for Alertmanager's config files. // Config is the top-level configuration for Alertmanager's config files.
type Config struct { type Config struct {
Global *config.GlobalConfig `yaml:"global,omitempty" json:"global,omitempty"` Global *config.GlobalConfig `yaml:"global,omitempty" json:"global,omitempty"`
Route *Route `yaml:"route,omitempty" json:"route,omitempty"` Route *Route `yaml:"route,omitempty" json:"route,omitempty"`
InhibitRules []config.InhibitRule `yaml:"inhibit_rules,omitempty" json:"inhibit_rules,omitempty"` InhibitRules []config.InhibitRule `yaml:"inhibit_rules,omitempty" json:"inhibit_rules,omitempty"`
TimeIntervals []config.TimeInterval `yaml:"time_intervals,omitempty" json:"time_intervals,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"` 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"` Templates []string `yaml:"templates" json:"templates"`
} }
@ -939,15 +940,6 @@ func (c *Config) UnmarshalJSON(b []byte) error {
} }
tiNames := make(map[string]struct{}) 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 { for _, mt := range c.MuteTimeIntervals {
if mt.Name == "" { if mt.Name == "" {
return fmt.Errorf("missing name in mute time interval") return fmt.Errorf("missing name in mute time interval")
@ -957,6 +949,15 @@ func (c *Config) UnmarshalJSON(b []byte) error {
} }
tiNames[mt.Name] = struct{}{} 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) return checkTimeInterval(c.Route, tiNames)
} }
@ -988,12 +989,12 @@ func (c *PostableApiAlertingConfig) GetReceivers() []*PostableApiReceiver {
return c.Receivers return c.Receivers
} }
func (c *PostableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals }
func (c *PostableApiAlertingConfig) GetMuteTimeIntervals() []config.MuteTimeInterval { func (c *PostableApiAlertingConfig) GetMuteTimeIntervals() []config.MuteTimeInterval {
return c.MuteTimeIntervals return c.MuteTimeIntervals
} }
func (c *PostableApiAlertingConfig) GetTimeIntervals() []config.TimeInterval { return c.TimeIntervals }
func (c *PostableApiAlertingConfig) GetRoute() *Route { func (c *PostableApiAlertingConfig) GetRoute() *Route {
return c.Route return c.Route
} }

View File

@ -485,49 +485,6 @@ func Test_ConfigUnmashaling(t *testing.T) {
err error 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": "<example@email.com>"
},
"secureFields": {}
}
]
}
]
}
`,
}, {
desc: "missing mute time interval name should error", desc: "missing mute time interval name should error",
err: errors.New("missing name in mute time interval"), err: errors.New("missing name in mute time interval"),
input: ` input: `
@ -572,8 +529,8 @@ func Test_ConfigUnmashaling(t *testing.T) {
`, `,
}, },
{ {
desc: "duplicate time interval names should error", desc: "missing time interval name should error",
err: errors.New("time interval \"test1\" is not unique"), err: errors.New("missing name in time interval"),
input: ` input: `
{ {
"route": { "route": {
@ -581,7 +538,7 @@ func Test_ConfigUnmashaling(t *testing.T) {
}, },
"time_intervals": [ "time_intervals": [
{ {
"name": "test1", "name": "",
"time_intervals": [ "time_intervals": [
{ {
"times": [ "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, "templates": null,
"receivers": [ "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": "<example@email.com>"
},
"secureFields": {}
}
]
}
]
}
`,
},
{ {
desc: "duplicate time and mute time interval names should error", 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: ` input: `
{ {
"route": { "route": {

View File

@ -111,14 +111,14 @@ func (a AlertingConfiguration) InhibitRules() []alertingNotify.InhibitRule {
return a.alertmanagerConfig.InhibitRules return a.alertmanagerConfig.InhibitRules
} }
func (a AlertingConfiguration) TimeIntervals() []alertingNotify.TimeInterval {
return a.alertmanagerConfig.TimeIntervals
}
func (a AlertingConfiguration) MuteTimeIntervals() []alertingNotify.MuteTimeInterval { func (a AlertingConfiguration) MuteTimeIntervals() []alertingNotify.MuteTimeInterval {
return a.alertmanagerConfig.MuteTimeIntervals return a.alertmanagerConfig.MuteTimeIntervals
} }
func (a AlertingConfiguration) TimeIntervals() []alertingNotify.TimeInterval {
return a.alertmanagerConfig.TimeIntervals
}
func (a AlertingConfiguration) Receivers() []*alertingNotify.APIReceiver { func (a AlertingConfiguration) Receivers() []*alertingNotify.APIReceiver {
return a.receivers return a.receivers
} }

View File

@ -27,8 +27,8 @@ type staticValidator struct {
// apiAlertingConfig contains the methods required to validate NotificationSettings and create autogen routes. // apiAlertingConfig contains the methods required to validate NotificationSettings and create autogen routes.
type apiAlertingConfig[R receiver] interface { type apiAlertingConfig[R receiver] interface {
GetReceivers() []R GetReceivers() []R
GetTimeIntervals() []config.TimeInterval
GetMuteTimeIntervals() []config.MuteTimeInterval GetMuteTimeIntervals() []config.MuteTimeInterval
GetTimeIntervals() []config.TimeInterval
GetRoute() *definitions.Route GetRoute() *definitions.Route
} }
@ -44,10 +44,10 @@ func NewNotificationSettingsValidator[R receiver](am apiAlertingConfig[R]) Notif
} }
availableTimeIntervals := make(map[string]struct{}) availableTimeIntervals := make(map[string]struct{})
for _, interval := range am.GetTimeIntervals() { for _, interval := range am.GetMuteTimeIntervals() {
availableTimeIntervals[interval.Name] = struct{}{} availableTimeIntervals[interval.Name] = struct{}{}
} }
for _, interval := range am.GetMuteTimeIntervals() { for _, interval := range am.GetTimeIntervals() {
availableTimeIntervals[interval.Name] = struct{}{} availableTimeIntervals[interval.Name] = struct{}{}
} }

View File

@ -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{ cfg: apimodels.PostableUserConfig{
AlertmanagerConfig: apimodels.PostableApiAlertingConfig{ AlertmanagerConfig: apimodels.PostableApiAlertingConfig{
Config: apimodels.Config{ Config: apimodels.Config{
@ -204,7 +207,7 @@ func TestIntegrationAlertmanagerConfiguration(t *testing.T) {
MuteTimeIntervals: []string{"weekends"}, MuteTimeIntervals: []string{"weekends"},
}}, }},
}, },
TimeIntervals: []config.TimeInterval{{ MuteTimeIntervals: []config.MuteTimeInterval{{
Name: "weekends", Name: "weekends",
TimeIntervals: []timeinterval.TimeInterval{{ TimeIntervals: []timeinterval.TimeInterval{{
Weekdays: []timeinterval.WeekdayRange{{ Weekdays: []timeinterval.WeekdayRange{{
@ -224,10 +227,7 @@ func TestIntegrationAlertmanagerConfiguration(t *testing.T) {
}, },
}, },
}, { }, {
// TODO: Mute time intervals is deprecated in Alertmanager and scheduled to be name: "configuration with time intervals",
// 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{ cfg: apimodels.PostableUserConfig{
AlertmanagerConfig: apimodels.PostableApiAlertingConfig{ AlertmanagerConfig: apimodels.PostableApiAlertingConfig{
Config: apimodels.Config{ Config: apimodels.Config{
@ -237,7 +237,7 @@ func TestIntegrationAlertmanagerConfiguration(t *testing.T) {
MuteTimeIntervals: []string{"weekends"}, MuteTimeIntervals: []string{"weekends"},
}}, }},
}, },
MuteTimeIntervals: []config.MuteTimeInterval{{ TimeIntervals: []config.TimeInterval{{
Name: "weekends", Name: "weekends",
TimeIntervals: []timeinterval.TimeInterval{{ TimeIntervals: []timeinterval.TimeInterval{{
Weekdays: []timeinterval.WeekdayRange{{ Weekdays: []timeinterval.WeekdayRange{{