cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.22.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/checks/alerts_annotation.go

81lines · modecode

1package checks
2
3import (
4 "context"
5 "fmt"
6
7 "github.com/cloudflare/pint/internal/discovery"
8 "github.com/cloudflare/pint/internal/parser"
9)
10
11const (
12 AnnotationCheckName = "alerts/annotation"
13)
14
15func NewAnnotationCheck(key string, valueRe *TemplatedRegexp, isReguired bool, severity Severity) AnnotationCheck {
16 return AnnotationCheck{key: key, valueRe: valueRe, isReguired: isReguired, severity: severity}
17}
18
19type AnnotationCheck struct {
20 key string
21 valueRe *TemplatedRegexp
22 isReguired bool
23 severity Severity
24}
25
26func (c AnnotationCheck) String() string {
27 if c.valueRe != nil {
28 return fmt.Sprintf("%s(%s=~%s:%v)", AnnotationCheckName, c.key, c.valueRe.anchored, c.isReguired)
29 }
30 return fmt.Sprintf("%s(%s:%v)", AnnotationCheckName, c.key, c.isReguired)
31}
32
33func (c AnnotationCheck) Reporter() string {
34 return AnnotationCheckName
35}
36
37func (c AnnotationCheck) Check(ctx context.Context, rule parser.Rule, entries []discovery.Entry) (problems []Problem) {
38 if rule.AlertingRule == nil {
39 return nil
40 }
41
42 if rule.AlertingRule.Annotations == nil {
43 if c.isReguired {
44 problems = append(problems, Problem{
45 Fragment: fmt.Sprintf("%s: %s", rule.AlertingRule.Alert.Key.Value, rule.AlertingRule.Alert.Value.Value),
46 Lines: rule.Lines(),
47 Reporter: c.Reporter(),
48 Text: fmt.Sprintf("%s annotation is required", c.key),
49 Severity: c.severity,
50 })
51 }
52 return
53 }
54
55 val := rule.AlertingRule.Annotations.GetValue(c.key)
56 if val == nil {
57 if c.isReguired {
58 problems = append(problems, Problem{
59 Fragment: fmt.Sprintf("%s:", rule.AlertingRule.Annotations.Key.Value),
60 Lines: rule.AlertingRule.Annotations.Lines(),
61 Reporter: c.Reporter(),
62 Text: fmt.Sprintf("%s annotation is required", c.key),
63 Severity: c.severity,
64 })
65 }
66 return
67 }
68
69 if c.valueRe != nil && !c.valueRe.MustExpand(rule).MatchString(val.Value) {
70 problems = append(problems, Problem{
71 Fragment: fmt.Sprintf("%s: %s", c.key, val.Value),
72 Lines: val.Position.Lines,
73 Reporter: c.Reporter(),
74 Text: fmt.Sprintf("%s annotation value must match %q", c.key, c.valueRe.anchored),
75 Severity: c.severity,
76 })
77 return
78 }
79
80 return nil
81}
82