Skip to content

Commit

Permalink
fix: load paramsFromFile as a relative path to the config file location
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Chodur <[email protected]>
  • Loading branch information
FUSAKLA committed Mar 7, 2024
1 parent d274c08 commit 6d5c48e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.12.0] - 2024-03-07
- Fixed resolving of the path in `paramsFromFile`. Formerly it was resolved from the current working directory, now it must be a relative path, that will be resolved from the config file location.

## [2.11.0] - 2024-03-07
- :warning: CHANGED: Params of the `hasSourceTenantsForMetrics` validator (again FACEPALM). Now the tenant can have multiple regexp matchers.
See its [docs](docs/validations.md#hassourcetenantsformetrics).
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ validationRules:
params:
labels: [ "severity" ]
# OPTIONAL If you want to load the parameters from a separate file, you can use this option.
# Its value must be a relative path to the file from the location of the config file.
# 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: 1 addition & 1 deletion examples/validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ validationRules:
params:
labels: ["severity"]
- type: labelHasAllowedValue
paramsFromFile: ./examples/allowed_values_params.yaml
paramsFromFile: ./allowed_values_params.yaml
- type: exclusiveLabels
params:
firstLabel: severity
Expand Down
14 changes: 2 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/fusakla/promruval/v2/pkg/validationrule"
"github.com/fusakla/promruval/v2/pkg/validator"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

var (
Expand Down Expand Up @@ -42,17 +41,8 @@ var (
)

func loadConfigFile(configFilePath string) (*config.Config, error) {
configFile, err := os.Open(configFilePath)
if err != nil {
return nil, fmt.Errorf("open config file: %w", err)
}
validationConfig := &config.Config{}
decoder := yaml.NewDecoder(configFile)
decoder.KnownFields(true)
if err := decoder.Decode(validationConfig); err != nil {
return nil, fmt.Errorf("loading config file: %w", err)
}
return validationConfig, nil
configLoader := config.NewLoader(configFilePath)
return configLoader.Load()
}

func validationRulesFromConfig(validationConfig *config.Config) ([]*validationrule.ValidationRule, error) {
Expand Down
46 changes: 45 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"fmt"
"os"
"path"
"sync"
"time"

"gopkg.in/yaml.v3"
Expand All @@ -19,6 +21,45 @@ const (

var ValidationScopes = []ValidationScope{Group, AlertScope, RecordingRuleScope, AllRulesScope}

// Ugly hack with a global variable to be able to use it in UnmarshalYAML.
// Not sure how to better propagate some context to the UnmarshalYAML function.
var (
configDir string
configDirMtx sync.Mutex
)

func init() {
configDirMtx = sync.Mutex{}
}

func NewLoader(cfgPath string) Loader {
return Loader{ConfigPath: cfgPath}
}

type Loader struct {
ConfigPath string
}

func (l *Loader) Load() (*Config, error) {
configFile, err := os.Open(l.ConfigPath)
if err != nil {
return nil, fmt.Errorf("open config file: %w", err)
}
configDirMtx.Lock()
configDir = path.Dir(l.ConfigPath)
defer func() {
configDir = ""
configDirMtx.Unlock()
}()
validationConfig := Config{}
decoder := yaml.NewDecoder(configFile)
decoder.KnownFields(true)
if err := decoder.Decode(&validationConfig); err != nil {
return nil, fmt.Errorf("loading config file: %w", err)
}
return &validationConfig, nil
}

type Config struct {
CustomExcludeAnnotation string `yaml:"customExcludeAnnotation"`
CustomDisableComment string `yaml:"customDisableComment"`
Expand Down Expand Up @@ -71,7 +112,10 @@ func (c *ValidatorConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
if !c.Params.IsZero() {
return fmt.Errorf("cannot use both `params` and `paramsFromFile`")
}
fileData, err := os.ReadFile(c.ParamsFromFile)
if path.IsAbs(c.ParamsFromFile) {
return fmt.Errorf("`paramsFromFile` must be a relative path to the config file")
}
fileData, err := os.ReadFile(path.Join(configDir, c.ParamsFromFile))
if err != nil {
return fmt.Errorf("cannot read params from file %s: %w", c.ParamsFromFile, err)
}
Expand Down

0 comments on commit 6d5c48e

Please sign in to comment.