-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathalertmanager_validation.go
98 lines (85 loc) · 2.58 KB
/
alertmanager_validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package definition
import (
"fmt"
"time"
"github.com/prometheus/common/model"
)
// groupByAll is a special value defined by alertmanager that can be used in a Route's GroupBy field to aggregate by all possible labels.
const groupByAll = "..."
// ValidateChild normalizes a possibly nested Route r, and returns errors if r is invalid.
func (r *Route) ValidateChild() error {
r.GroupBy = nil
r.GroupByAll = false
for _, l := range r.GroupByStr {
if l == groupByAll {
r.GroupByAll = true
} else {
r.GroupBy = append(r.GroupBy, model.LabelName(l))
}
}
if len(r.GroupBy) > 0 && r.GroupByAll {
return fmt.Errorf("cannot have wildcard group_by (`...`) and other other labels at the same time")
}
groupBy := map[model.LabelName]struct{}{}
for _, ln := range r.GroupBy {
if _, ok := groupBy[ln]; ok {
return fmt.Errorf("duplicated label %q in group_by, %s %s", ln, r.Receiver, r.GroupBy)
}
groupBy[ln] = struct{}{}
}
if r.GroupInterval != nil && time.Duration(*r.GroupInterval) == time.Duration(0) {
return fmt.Errorf("group_interval cannot be zero")
}
if r.RepeatInterval != nil && time.Duration(*r.RepeatInterval) == time.Duration(0) {
return fmt.Errorf("repeat_interval cannot be zero")
}
// Routes are a self-referential structure.
if r.Routes != nil {
for _, child := range r.Routes {
err := child.ValidateChild()
if err != nil {
return err
}
}
}
return nil
}
// Validate normalizes a Route r, and returns errors if r is an invalid root route. Root routes must satisfy a few additional conditions.
func (r *Route) Validate() error {
if len(r.Receiver) == 0 {
return fmt.Errorf("root route must specify a default receiver")
}
if len(r.Match) > 0 || len(r.MatchRE) > 0 {
return fmt.Errorf("root route must not have any matchers")
}
if len(r.MuteTimeIntervals) > 0 {
return fmt.Errorf("root route must not have any mute time intervals")
}
return r.ValidateChild()
}
func (r *Route) ValidateReceivers(receivers map[string]struct{}) error {
if _, exists := receivers[r.Receiver]; !exists {
return fmt.Errorf("receiver '%s' does not exist", r.Receiver)
}
for _, children := range r.Routes {
err := children.ValidateReceivers(receivers)
if err != nil {
return err
}
}
return nil
}
func (r *Route) ValidateMuteTimes(muteTimes map[string]struct{}) error {
for _, name := range r.MuteTimeIntervals {
if _, exists := muteTimes[name]; !exists {
return fmt.Errorf("mute time interval '%s' does not exist", name)
}
}
for _, child := range r.Routes {
err := child.ValidateMuteTimes(muteTimes)
if err != nil {
return err
}
}
return nil
}