Skip to content

Commit

Permalink
feat: allow to load params from file using the paramsFromFile
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Chodur <[email protected]>
  • Loading branch information
FUSAKLA committed Mar 4, 2024
1 parent 3ce2f85 commit 5a20a17
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
- Fixed error messages for the `hasSourceTenantsForMetrics` and `expressionDoesNotUseIrate` validators.

- Added new option `paramsFromFile` to the validators, so the params can be loaded from a YAML file.
Example promruval config:
```yaml
- type: labelHasAllowedValue
paramsFromFile: ./examples/allowed_values_params.yaml
```
Content of `./examples/allowed_values_params.yaml`:
```yaml
label: "severity"
allowedValues: ["info", "warning", "critical"]
```

- Added new config option `additionalDetails` to all validators providing possibility to add custom details about the error and how to solve it.
Those will be appended to the validator error message in a parenthesis if provided.
Example configuration:
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,13 @@ validationRules:
- type: hasLabels
# Additional detaild that will be appended to the default error message. Useful to customize the error message.
additionalDetails: "We do this because ..."
# Parameters of the validation.
# Parameters of the validation. See the /docs/validations.md for details on params of each validation.
params:
labels: [ "severity" ]
# OPTIONAL If you want to load the parameters from a separate file, you can use this option.
# The content of the file must be in the exact form as the expected params would be.
# The option is mutually exclusive with the `params` option.
# paramsFromFile: /path/to/file.yaml
...
```
Expand Down
2 changes: 2 additions & 0 deletions examples/allowed_values_params.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
label: "severity"
allowedValues: ["info", "warning", "critical"]
4 changes: 1 addition & 3 deletions examples/validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ validationRules:
params:
labels: ["severity"]
- type: labelHasAllowedValue
params:
label: "severity"
allowedValues: ["info", "warning", "critical"]
paramsFromFile: ./examples/allowed_values_params.yaml
- type: exclusiveLabels
params:
firstLabel: severity
Expand Down
24 changes: 24 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"os"
"time"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -57,6 +58,29 @@ type ValidatorConfig struct {
ValidatorType string `yaml:"type"`
AdditionalDetails string `yaml:"additionalDetails"`
Params yaml.Node `yaml:"params"`
ParamsFromFile string `yaml:"paramsFromFile"`
}

func (c *ValidatorConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain ValidatorConfig
err := unmarshal((*plain)(c))
if err != nil {
return err
}
if c.ParamsFromFile != "" {
if !c.Params.IsZero() {
return fmt.Errorf("cannot use both `params` and `paramsFromFile`")
}
fileData, err := os.ReadFile(c.ParamsFromFile)
if err != nil {
return fmt.Errorf("cannot read params from file %s: %w", c.ParamsFromFile, err)
}
err = yaml.Unmarshal(fileData, &c.Params)
if err != nil {
return err
}
}
return nil
}

type ValidationScope string
Expand Down

0 comments on commit 5a20a17

Please sign in to comment.