MCPcopy Create free account
hub / github.com/cortexproject/cortex / validateYAMLEmptyNodes

Method validateYAMLEmptyNodes

pkg/cortex/cortex.go:272–293  ·  view source on GitHub ↗

validateYAMLEmptyNodes ensure that no empty node has been specified in the YAML config file. When an empty node is defined in YAML, the YAML parser sets the whole struct to its zero value and so we loose all default values. It's very difficult to detect this case for the user, so we try to prevent i

()

Source from the content-addressed store, hash-verified

270// and so we loose all default values. It's very difficult to detect this case for the user, so we
271// try to prevent it (on the root level) with this custom validation.
272func (c *Config) validateYAMLEmptyNodes() error {
273 defaults := Config{}
274 flagext.DefaultValues(&defaults)
275
276 defStruct := reflect.ValueOf(defaults)
277 cfgStruct := reflect.ValueOf(*c)
278
279 // We expect all structs are the exact same. This check should never fail.
280 if cfgStruct.NumField() != defStruct.NumField() {
281 return errors.New("unable to validate configuration because of mismatching internal config data structure")
282 }
283
284 for i := 0; i < cfgStruct.NumField(); i++ {
285 // If the struct has been reset due to empty YAML value and the zero struct value
286 // doesn't match the default one, then we should warn the user about the issue.
287 if cfgStruct.Field(i).Kind() == reflect.Struct && cfgStruct.Field(i).IsZero() && !defStruct.Field(i).IsZero() {
288 return fmt.Errorf("the %s configuration in YAML has been specified as an empty YAML node", cfgStruct.Type().Field(i).Name)
289 }
290 }
291
292 return nil
293}
294
295func (c *Config) registerServerFlagsWithChangedDefaultValues(fs *flag.FlagSet) {
296 throwaway := flag.NewFlagSet("throwaway", flag.PanicOnError)

Callers 1

ValidateMethod · 0.95

Calls 2

DefaultValuesFunction · 0.92
TypeMethod · 0.80

Tested by

no test coverage detected