Skip to content

Commit

Permalink
Merge pull request prometheus-operator#6362 from mouad-eh/issue6353
Browse files Browse the repository at this point in the history
fix: Crd AltermanagerConfig field muteTimeIntervals.timeIntervals.months regex pattern |[1-12] isn't correct (prometheus-operator#6353)
  • Loading branch information
simonpasquier committed Mar 28, 2024
2 parents c110e25 + 7d69cce commit 6e94864
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bundle.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jsonnet/prometheus-operator/alertmanagerconfigs-crd.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
"description": "Months is a list of MonthRange",
"items": {
"description": "MonthRange is an inclusive range of months of the year beginning in January Months can be specified by name (e.g 'January') by numerical month (e.g '1') or as an inclusive range (e.g 'January:March', '1:3', '1:March')",
"pattern": "^((?i)january|february|march|april|may|june|july|august|september|october|november|december|[1-12])(?:((:((?i)january|february|march|april|may|june|july|august|september|october|november|december|[1-12]))$)|$)",
"pattern": "^((?i)january|february|march|april|may|june|july|august|september|october|november|december|1[0-2]|[1-9])(?:((:((?i)january|february|march|april|may|june|july|august|september|october|november|december|1[0-2]|[1-9]))$)|$)",
"type": "string"
},
"type": "array"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5778,7 +5778,7 @@
description: 'Months is a list of MonthRange',
items: {
description: "MonthRange is an inclusive range of months of the year beginning in January Months can be specified by name (e.g 'January') by numerical month (e.g '1') or as an inclusive range (e.g 'January:March', '1:3', '1:March')",
pattern: '^((?i)january|february|march|april|may|june|july|august|september|october|november|december|[1-12])(?:((:((?i)january|february|march|april|may|june|july|august|september|october|november|december|[1-12]))$)|$)',
pattern: '^((?i)january|february|march|april|may|june|july|august|september|october|november|december|1[0-2]|[1-9])(?:((:((?i)january|february|march|april|may|june|july|august|september|october|november|december|1[0-2]|[1-9]))$)|$)',
type: 'string',
},
type: 'array',
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/monitoring/v1alpha1/alertmanager_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ type DayOfMonthRange struct {

// MonthRange is an inclusive range of months of the year beginning in January
// Months can be specified by name (e.g 'January') by numerical month (e.g '1') or as an inclusive range (e.g 'January:March', '1:3', '1:March')
// +kubebuilder:validation:Pattern=`^((?i)january|february|march|april|may|june|july|august|september|october|november|december|[1-12])(?:((:((?i)january|february|march|april|may|june|july|august|september|october|november|december|[1-12]))$)|$)`
// +kubebuilder:validation:Pattern=`^((?i)january|february|march|april|may|june|july|august|september|october|november|december|1[0-2]|[1-9])(?:((:((?i)january|february|march|april|may|june|july|august|september|october|november|december|1[0-2]|[1-9]))$)|$)`
type MonthRange string

// YearRange is an inclusive range of years
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/monitoring/v1alpha1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,16 @@ func (w Weekday) Int() (int, error) {
func (m Month) Int() (int, error) {
normaliseMonth := Month(strings.ToLower(string(m)))

day, found := months[normaliseMonth]
month, found := months[normaliseMonth]
if !found {
i, err := strconv.Atoi(string(normaliseMonth))
if err != nil {
return day, fmt.Errorf("%s is an invalid month", m)
if err != nil || i < 1 || i > 12 {
return month, fmt.Errorf("%s is an invalid month", m)
}
day = i
month = i
}

return day, nil
return month, nil
}

// Validate the DayOfMonthRange
Expand Down
57 changes: 52 additions & 5 deletions pkg/apis/monitoring/v1alpha1/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,36 +97,83 @@ func TestMonthRange_Parse(t *testing.T) {
expectErr: true,
},
{
name: "Test invalid months returns error",
name: "Test invalid named months returns error",
in: MonthRange("januarE"),
expectErr: true,
},
{
name: "Test invalid months in range returns error",
name: "Test invalid numerical months returns error",
in: MonthRange("13"),
expectErr: true,
},
{
name: "Test invalid named months in range returns error",
in: MonthRange("january:Merch"),
expectErr: true,
},
{
name: "Test invalid range - end before start returns error",
name: "Test invalid numerical months in range returns error",
in: MonthRange("1:13"),
expectErr: true,
},
{
name: "Test invalid named range - end before start returns error",
in: MonthRange("march:january"),
expectErr: true,
},
{
name: "Test happy path",
name: "Test invalid numerical range - end before start returns error",
in: MonthRange("3:1"),
expectErr: true,
},
{
name: "Test happy named path",
in: MonthRange("january"),
expectResult: &ParsedRange{
Start: 1,
End: 1,
},
},
{
name: "Test happy path range",
name: "Test happy one digit numerical path",
in: MonthRange("1"),
expectResult: &ParsedRange{
Start: 1,
End: 1,
},
},
{
name: "Test happy two digits numerical path",
in: MonthRange("12"),
expectResult: &ParsedRange{
Start: 12,
End: 12,
},
},
{
name: "Test happy named path range",
in: MonthRange("january:march"),
expectResult: &ParsedRange{
Start: 1,
End: 3,
},
},
{
name: "Test happy numerical path range",
in: MonthRange("1:12"),
expectResult: &ParsedRange{
Start: 1,
End: 12,
},
},
{
name: "Test happy mixed path range",
in: MonthRange("1:march"),
expectResult: &ParsedRange{
Start: 1,
End: 3,
},
},
}

for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/monitoring/v1beta1/alertmanager_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ type DayOfMonthRange struct {

// MonthRange is an inclusive range of months of the year beginning in January
// Months can be specified by name (e.g 'January') by numerical month (e.g '1') or as an inclusive range (e.g 'January:March', '1:3', '1:March')
// +kubebuilder:validation:Pattern=`^((?i)january|february|march|april|may|june|july|august|september|october|november|december|[1-12])(?:((:((?i)january|february|march|april|may|june|july|august|september|october|november|december|[1-12]))$)|$)`
// +kubebuilder:validation:Pattern=`^((?i)january|february|march|april|may|june|july|august|september|october|november|december|1[0-2]|[1-9])(?:((:((?i)january|february|march|april|may|june|july|august|september|october|november|december|1[0-2]|[1-9]))$)|$)`
type MonthRange string

// YearRange is an inclusive range of years
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/monitoring/v1beta1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,16 @@ func (w Weekday) Int() (int, error) {
func (m Month) Int() (int, error) {
normaliseMonth := Month(strings.ToLower(string(m)))

day, found := months[normaliseMonth]
month, found := months[normaliseMonth]
if !found {
i, err := strconv.Atoi(string(normaliseMonth))
if err != nil {
return day, fmt.Errorf("%s is an invalid month", m)
if err != nil || i < 1 || i > 12 {
return month, fmt.Errorf("%s is an invalid month", m)
}
day = i
month = i
}

return day, nil
return month, nil
}

// Validate the DayOfMonthRange
Expand Down
57 changes: 52 additions & 5 deletions pkg/apis/monitoring/v1beta1/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,36 +97,83 @@ func TestMonthRange_Parse(t *testing.T) {
expectErr: true,
},
{
name: "Test invalid months returns error",
name: "Test invalid named months returns error",
in: MonthRange("januarE"),
expectErr: true,
},
{
name: "Test invalid months in range returns error",
name: "Test invalid numerical months returns error",
in: MonthRange("13"),
expectErr: true,
},
{
name: "Test invalid named months in range returns error",
in: MonthRange("january:Merch"),
expectErr: true,
},
{
name: "Test invalid range - end before start returns error",
name: "Test invalid numerical months in range returns error",
in: MonthRange("1:13"),
expectErr: true,
},
{
name: "Test invalid named range - end before start returns error",
in: MonthRange("march:january"),
expectErr: true,
},
{
name: "Test happy path",
name: "Test invalid numerical range - end before start returns error",
in: MonthRange("3:1"),
expectErr: true,
},
{
name: "Test happy named path",
in: MonthRange("january"),
expectResult: &ParsedRange{
Start: 1,
End: 1,
},
},
{
name: "Test happy path range",
name: "Test happy one digit numerical path",
in: MonthRange("1"),
expectResult: &ParsedRange{
Start: 1,
End: 1,
},
},
{
name: "Test happy two digits numerical path",
in: MonthRange("12"),
expectResult: &ParsedRange{
Start: 12,
End: 12,
},
},
{
name: "Test happy named path range",
in: MonthRange("january:march"),
expectResult: &ParsedRange{
Start: 1,
End: 3,
},
},
{
name: "Test happy numerical path range",
in: MonthRange("1:12"),
expectResult: &ParsedRange{
Start: 1,
End: 12,
},
},
{
name: "Test happy mixed path range",
in: MonthRange("1:march"),
expectResult: &ParsedRange{
Start: 1,
End: 3,
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 6e94864

Please sign in to comment.