From 2c2deeb39ebcb94217a65eebf9ae2f70b49c69b5 Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Thu, 21 Jul 2022 11:12:53 +0100 Subject: [PATCH] Don't fail to parse YAML on template errors --- cmd/pint/tests/0081_rulefmt.txt | 6 +-- .../tests/0086_rulefmt_ignored_errors.txt | 44 +++++++++++++++++++ docs/changelog.md | 5 +++ internal/discovery/discovery.go | 2 + 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 cmd/pint/tests/0086_rulefmt_ignored_errors.txt diff --git a/cmd/pint/tests/0081_rulefmt.txt b/cmd/pint/tests/0081_rulefmt.txt index 25bfc770..37e2ebae 100644 --- a/cmd/pint/tests/0081_rulefmt.txt +++ b/cmd/pint/tests/0081_rulefmt.txt @@ -3,9 +3,9 @@ pint.error --no-color lint rules cmp stderr stderr.txt -- stderr.txt -- -level=error msg="Failed to unmarshal file content" error="0:0: group \"foo\", rule 1, \"foo\": field 'expr' must be set in rule" lines=1-4 path=rules/strict.yml -rules/strict.yml:1: field 'expr' must be set in rule (yaml/parse) -groups: +level=info msg="File parsed" path=rules/strict.yml rules=1 +rules/strict.yml:4: missing expr key (yaml/parse) + - record: foo level=info msg="Problems found" Fatal=1 level=fatal msg="Fatal error" error="problems found" diff --git a/cmd/pint/tests/0086_rulefmt_ignored_errors.txt b/cmd/pint/tests/0086_rulefmt_ignored_errors.txt new file mode 100644 index 00000000..0011a960 --- /dev/null +++ b/cmd/pint/tests/0086_rulefmt_ignored_errors.txt @@ -0,0 +1,44 @@ +pint.error --no-color lint rules +! stdout . +cmp stderr stderr.txt + +-- stderr.txt -- +level=info msg="File parsed" path=rules/strict.yml rules=5 +rules/strict.yml:4: incomplete rule, no alert or record key (yaml/parse) + - expr: MissingAlertOrRecord + +rules/strict.yml:7: syntax error: no expression found in input (promql/syntax) + expr: + +rules/strict.yml:10: syntax error: unknown function with name "sumz" (promql/syntax) + expr: sumz(0) + +rules/strict.yml:15: template parse error: function "bogus" not defined (alerts/template) + dashboard: '{{ bogus }}' + +rules/strict.yml:20: template parse error: function "bogus" not defined (alerts/template) + dashboard: '{{ bogus }}' + +level=info msg="Problems found" Fatal=5 +level=fatal msg="Fatal error" error="problems found" +-- rules/strict.yml -- +groups: +- name: foo + rules: + - expr: MissingAlertOrRecord + + - alert: MissingExpr + expr: + + - record: BadPromQL + expr: sumz(0) + + - alert: IgnoreTemplateErrors + expr: up == 0 + annotations: + dashboard: '{{ bogus }}' + + - alert: IgnoreTemplateErrors + expr: up == 0 + labels: + dashboard: '{{ bogus }}' diff --git a/docs/changelog.md b/docs/changelog.md index 60245f34..f1a3bc23 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,11 @@ ## v0.26.0 +### Fixed + +- Strict parsing mode shouldn't fail on template errors, those will be later + reported by `alerts/template` check. + ### Added - [promql/range_query](checks/promql/range_query.md) check. diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index b049bdc2..e26cf57c 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -21,8 +21,10 @@ const ( var ignoredErrors = []string{ "one of 'record' or 'alert' must be set", + "field 'expr' must be set in rule", "could not parse expression: ", "cannot unmarshal !!seq into rulefmt.ruleGroups", + ": template: __", } func isStrictIgnored(err error) bool {