Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic when validation unsupported file type #95

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added: New validation `expressionDoesNotUseClassicHistogramBucketOperations` to avoid queries fragile because of the classic histogram bucket operations.
See the docs for more info [expressionDoesNotUseClassicHistogramBucketOperations](docs/validations.md#expressiondoesnotuseclassichistogrambucketoperations)
- Changed: :warning: revert the ENV expansion in config file since it was breaking and caused issues (it was a stupid idea)
- Fix: Avoid panic when validating unsupported file type and revert to previous behavior when any file was treated as a rule file regardless of the extension.
The only exception are `.jsonnet` files that are evaluated first.

## [3.4.0] - 2024-10-30
- Fixed: :warning: Ignore white spaces around rule names in the `disabled_validation_rules` annotation CSV format (Thanks @jmichalek13 !)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ build:

E2E_TESTS_VALIDATIONS_FILE := examples/validation.yaml
E2E_TESTS_ADDITIONAL_VALIDATIONS_FILE := examples/additional-validation.yaml
E2E_TESTS_RULES_FILES := examples/rules/*.yaml
E2E_TESTS_RULES_FILES := examples/rules/*
E2E_TESTS_DOCS_FILE_MD := examples/human_readable.md
E2E_TESTS_DOCS_FILE_HTML := examples/human_readable.html

Expand Down
17 changes: 17 additions & 0 deletions examples/rules/foo.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
groups: [
{
name: 'foo',
limit: 1,
rules: [
{
alert: 'foo',
expr: |||
# ignore_validations: hasLabels,hasAnyOfAnnotations,hasAnnotations,hasAllowedLimit
1
|||,
},
],
},
],
}
6 changes: 6 additions & 0 deletions examples/rules/foo.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ignore_validations: hasLabels,hasAnyOfAnnotations,hasAnnotations,hasAllowedLimit
groups:
- name: foo
rules:
- alert: foo
expr: 1
19 changes: 10 additions & 9 deletions pkg/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,26 @@ func Files(fileNames []string, validationRules []*validationrule.ValidationRule,
validationReport.FilesCount++
fileReport := validationReport.NewFileReport(fileName)
var yamlReader io.Reader
if strings.HasSuffix(fileName, ".yaml") || strings.HasSuffix(fileName, ".yml") {
var err error
yamlReader, err = os.Open(fileName)
switch {
case strings.HasSuffix(fileName, ".jsonnet"):
log.Debugf("evaluating jsonnet file %s", fileName)
jsonnetOutput, err := jsonnetVM.EvaluateFile(fileName)
if err != nil {
validationReport.Failed = true
fileReport.Valid = false
fileReport.Errors = []error{fmt.Errorf("cannot read file %s: %w", fileName, err)}
fileReport.Errors = []error{fmt.Errorf("cannot evaluate jsonnet file %s: %w", fileName, err)}
continue
}
} else if strings.HasSuffix(fileName, ".jsonnet") {
log.Debugf("evaluating jsonnet file %s", fileName)
jsonnetOutput, err := jsonnetVM.EvaluateFile(fileName)
yamlReader = strings.NewReader(jsonnetOutput)
default:
var err error
yamlReader, err = os.Open(fileName)
if err != nil {
validationReport.Failed = true
fileReport.Valid = false
fileReport.Errors = []error{fmt.Errorf("cannot evaluate jsonnet file %s: %w", fileName, err)}
fileReport.Errors = []error{fmt.Errorf("cannot read file %s: %w", fileName, err)}
continue
}
yamlReader = strings.NewReader(jsonnetOutput)
}
var rf unmarshaler.RulesFileWithComment
decoder := yaml.NewDecoder(yamlReader)
Expand Down