Skip to content

Commit

Permalink
Move alert description validation to be optional
Browse files Browse the repository at this point in the history
Signed-off-by: Assaf Admi <aadmi@redhat.com>
  • Loading branch information
assafad committed Mar 5, 2024
1 parent 4df875e commit 5c468ca
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 41 deletions.
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"))
})
})
})

0 comments on commit 5c468ca

Please sign in to comment.