vmalert - validate template in labels (#439)

This commit is contained in:
kreedom 2020-04-26 13:53:57 +03:00 committed by GitHub
parent eeadfccdc5
commit 5f61d43db9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 1 deletions

View file

@ -40,7 +40,10 @@ func Parse(pathPatterns []string, validateAnnotations bool) ([]Group, error) {
rule.alerts = make(map[uint64]*notifier.Alert) rule.alerts = make(map[uint64]*notifier.Alert)
if validateAnnotations { if validateAnnotations {
if err = notifier.ValidateAnnotations(rule.Annotations); err != nil { if err = notifier.ValidateAnnotations(rule.Annotations); err != nil {
return nil, fmt.Errorf("invalida annotations filepath:%s, group %s:%w", file, group.Name, err) return nil, fmt.Errorf("invalid annotations filepath:%s, group %s:%w", file, group.Name, err)
}
if err = notifier.ValidateAnnotations(rule.Labels); err != nil {
return nil, fmt.Errorf("invalid labels filepath:%s, group %s:%w", file, group.Name, err)
} }
} }
rule.group = &group rule.group = &group

View file

@ -30,6 +30,9 @@ func TestParseBad(t *testing.T) {
if _, err := Parse([]string{"testdata/dir/rules1-bad.rules"}, true); err == nil { if _, err := Parse([]string{"testdata/dir/rules1-bad.rules"}, true); err == nil {
t.Errorf("expected same group error") t.Errorf("expected same group error")
} }
if _, err := Parse([]string{"testdata/dir/rules2-bad.rules"}, true); err == nil {
t.Errorf("expected template label error")
}
if _, err := Parse([]string{"testdata/*.yaml"}, true); err == nil { if _, err := Parse([]string{"testdata/*.yaml"}, true); err == nil {
t.Errorf("expected empty group") t.Errorf("expected empty group")
} }

View file

@ -0,0 +1,11 @@
groups:
- name: group
rules:
- alert: UnkownLabelFunction
for: 5m
expr: vm_rows > 0
labels:
label: bar
summary: "{{ value|query }}"
annotations:
description: "{{$labels}}"

View file

@ -6,6 +6,7 @@ groups:
expr: vm_rows > 0 expr: vm_rows > 0
labels: labels:
label: bar label: bar
template: "{{ $value|humanize }}"
annotations: annotations:
summary: "{{ $value|humanize }}" summary: "{{ $value|humanize }}"
description: "{{$labels}}" description: "{{$labels}}"