Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move alert description validation to be optional #6

Merged
merged 1 commit into from Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 23 additions & 10 deletions pkg/testutil/alert_custom_validation_test.go
Expand Up @@ -32,8 +32,8 @@ var _ = Describe("Custom Validators", func() {
"runbook_url": "example/runbook/url",
},
}
linter.AddCustomAlertValidations(ValidateAlertNameLength, ValidateAlertRunbookURLAnnotation,
ValidateAlertHealthImpactLabel, ValidateAlertPartOfAndComponentLabels)
linter.AddCustomAlertValidations(ValidateAlertNameLength, ValidateAlertHasDescriptionAnnotation,
ValidateAlertRunbookURLAnnotation, ValidateAlertHealthImpactLabel, ValidateAlertPartOfAndComponentLabels)
problems := linter.LintAlert(alert)
Expect(problems).To(BeEmpty())
})
Expand All @@ -46,8 +46,7 @@ var _ = Describe("Custom Validators", func() {
"severity": "critical",
},
Annotations: map[string]string{
"summary": "Example summary",
"description": "Example description",
"summary": "Example summary",
},
}
linter.AddCustomAlertValidations(ValidateAlertNameLength)
Expand All @@ -56,6 +55,23 @@ var _ = Describe("Custom Validators", func() {
Expect(problems[0].Description).To(ContainSubstring("alert name exceeds 50 characters"))
})

It("should return error if description custom validation was added and is missing", func() {
alert := &promv1.Rule{
Alert: "ExampleAlert",
Expr: intstr.FromString("sum(rate(http_requests_total[5m]))"),
Labels: map[string]string{
"severity": "critical",
},
Annotations: map[string]string{
"summary": "Example summary",
},
}
linter.AddCustomAlertValidations(ValidateAlertHasDescriptionAnnotation)
problems := linter.LintAlert(alert)
Expect(problems).To(HaveLen(1))
Expect(problems[0].Description).To(ContainSubstring("alert must have a description annotation"))
})

It("should return error if runbook_url custom validation was added and is missing", func() {
alert := &promv1.Rule{
Alert: "ExampleAlert",
Expand All @@ -64,8 +80,7 @@ var _ = Describe("Custom Validators", func() {
"severity": "critical",
},
Annotations: map[string]string{
"summary": "Example summary",
"description": "Example description",
"summary": "Example summary",
},
}
linter.AddCustomAlertValidations(ValidateAlertRunbookURLAnnotation)
Expand All @@ -83,8 +98,7 @@ var _ = Describe("Custom Validators", func() {
"operator_health_impact": "invalid_operator_health_impact",
},
Annotations: map[string]string{
"summary": "Example summary",
"description": "Example description",
"summary": "Example summary",
},
}
linter.AddCustomAlertValidations(ValidateAlertHealthImpactLabel)
Expand All @@ -101,8 +115,7 @@ var _ = Describe("Custom Validators", func() {
"severity": "critical",
},
Annotations: map[string]string{
"summary": "Example summary",
"description": "Example description",
"summary": "Example summary",
},
}
linter.AddCustomAlertValidations(ValidateAlertPartOfAndComponentLabels)
Expand Down
14 changes: 14 additions & 0 deletions pkg/testutil/alert_custom_validations.go
Expand Up @@ -17,6 +17,20 @@ func ValidateAlertNameLength(alert *promv1.Rule) []Problem {
return result
}

func ValidateAlertHasDescriptionAnnotation(alert *promv1.Rule) []Problem {
var result []Problem

description := alert.Annotations["description"]
if description == "" {
result = append(result, Problem{
ResourceName: alert.Alert,
Description: "alert must have a description annotation",
})
}

return result
}

func ValidateAlertRunbookURLAnnotation(alert *promv1.Rule) []Problem {
var result []Problem

Expand Down
15 changes: 0 additions & 15 deletions pkg/testutil/alert_validation.go
Expand Up @@ -13,7 +13,6 @@ var defaultAlertValidations = []AlertValidation{
validateAlertHasExpression,
validateAlertHasSeverityLabel,
validateAlertHasSummaryAnnotation,
validateAlertHasDescriptionAnnotation,
}

func validateAlertName(alert *promv1.Rule) []Problem {
Expand Down Expand Up @@ -70,20 +69,6 @@ func validateAlertHasSummaryAnnotation(alert *promv1.Rule) []Problem {
return result
}

func validateAlertHasDescriptionAnnotation(alert *promv1.Rule) []Problem {
var result []Problem

description := alert.Annotations["description"]
if description == "" {
result = append(result, Problem{
ResourceName: alert.Alert,
Description: "alert must have a description annotation",
})
}

return result
}

func isPascalCase(s string) bool {
pascalCasePattern := `^[A-Z][a-zA-Z0-9]*(?:[A-Z][a-zA-Z0-9]*)*$`
pascalCaseRegex := regexp.MustCompile(pascalCasePattern)
Expand Down
16 changes: 0 additions & 16 deletions pkg/testutil/alert_validation_test.go
Expand Up @@ -98,21 +98,5 @@ var _ = Describe("Default Validators", func() {
Expect(problems).To(HaveLen(1))
Expect(problems[0].Description).To(ContainSubstring("alert must have a summary annotation"))
})

It("should return error if description annotation is missing", func() {
alert := &promv1.Rule{
Alert: "ExampleAlert",
Expr: intstr.FromString("sum(rate(http_requests_total[5m]))"),
Labels: map[string]string{
"severity": "critical",
},
Annotations: map[string]string{
"summary": "Example summary",
},
}
problems := linter.LintAlert(alert)
Expect(problems).To(HaveLen(1))
Expect(problems[0].Description).To(ContainSubstring("alert must have a description annotation"))
})
})
})