MCPcopy Index your code
hub / github.com/cortexproject/cortex / validateAlertmanagerConfig

Function validateAlertmanagerConfig

pkg/alertmanager/api.go:377–448  ·  view source on GitHub ↗

validateAlertmanagerConfig recursively scans the input config looking for data types for which we have a specific validation and, whenever encountered, it runs their validation. Returns the first error or nil if validation succeeds.

(cfg any)

Source from the content-addressed store, hash-verified

375// we have a specific validation and, whenever encountered, it runs their validation. Returns the
376// first error or nil if validation succeeds.
377func validateAlertmanagerConfig(cfg any) error {
378 v := reflect.ValueOf(cfg)
379 t := v.Type()
380
381 // Skip invalid, the zero value or a nil pointer (checked by zero value).
382 if !v.IsValid() || v.IsZero() {
383 return nil
384 }
385
386 // If the input config is a pointer then we need to get its value.
387 // At this point the pointer value can't be nil.
388 if v.Kind() == reflect.Ptr {
389 v = v.Elem()
390 t = v.Type()
391 }
392
393 // Check if the input config is a data type for which we have a specific validation.
394 if validator, ok := configValidators[t]; ok {
395 if validator != nil {
396 if err := validator(v.Interface()); err != nil {
397 return err
398 }
399 }
400 } else if strings.Contains(t.PkgPath(), "alertmanager/config") &&
401 strings.HasSuffix(t.Name(), "Config") {
402 // Unhandled config type - fail to ensure we don't miss new types.
403 return fmt.Errorf("unsupported receiver config type: %s", t.Name())
404 }
405
406 // If the input config is a struct, recursively iterate on all fields.
407 if t.Kind() == reflect.Struct {
408 for i := 0; i < t.NumField(); i++ {
409 field := t.Field(i)
410 fieldValue := v.FieldByIndex(field.Index)
411
412 // Skip any field value which can't be converted to interface (eg. primitive types).
413 if fieldValue.CanInterface() {
414 if err := validateAlertmanagerConfig(fieldValue.Interface()); err != nil {
415 return err
416 }
417 }
418 }
419 }
420
421 if t.Kind() == reflect.Slice || t.Kind() == reflect.Array {
422 for i := 0; i < v.Len(); i++ {
423 fieldValue := v.Index(i)
424
425 // Skip any field value which can't be converted to interface (eg. primitive types).
426 if fieldValue.CanInterface() {
427 if err := validateAlertmanagerConfig(fieldValue.Interface()); err != nil {
428 return err
429 }
430 }
431 }
432 }
433
434 if t.Kind() == reflect.Map {

Callers 2

validateUserConfigFunction · 0.70

Calls 4

TypeMethod · 0.80
IsValidMethod · 0.80
NameMethod · 0.65
LenMethod · 0.45

Tested by 1