github.com/helmwave/helmwave@v0.36.4-0.20240509190856-b35563eba4c6/pkg/monitor/interface.go (about)

     1  package monitor
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/helmwave/helmwave/pkg/helper"
     7  	"github.com/helmwave/helmwave/pkg/log"
     8  	"github.com/invopop/jsonschema"
     9  	"github.com/sirupsen/logrus"
    10  	"gopkg.in/yaml.v3"
    11  )
    12  
    13  // SubConfig is an interface to manage particular typed monitor.
    14  type SubConfig interface {
    15  	Init(context.Context, *logrus.Entry) error
    16  	Run(context.Context) error
    17  	Validate() error
    18  }
    19  
    20  // Config is an interface to manage particular monitor.
    21  type Config interface {
    22  	log.LoggerGetter
    23  	Name() string
    24  	Run(context.Context) error
    25  	Validate() error
    26  }
    27  
    28  // Configs type of array Config.
    29  type Configs []Config
    30  
    31  // UnmarshalYAML is an unmarshaller for gopkg.in/yaml.v3 to parse YAML into `Config` interface.
    32  func (r *Configs) UnmarshalYAML(node *yaml.Node) error {
    33  	rr := make([]*config, 0)
    34  	err := node.Decode(&rr)
    35  	if err != nil {
    36  		return NewYAMLDecodeError(err)
    37  	}
    38  
    39  	*r = helper.SlicesMap(rr, func(h *config) Config { return h })
    40  
    41  	return nil
    42  }
    43  
    44  func (Configs) JSONSchema() *jsonschema.Schema {
    45  	r := &jsonschema.Reflector{
    46  		DoNotReference:             true,
    47  		RequiredFromJSONSchemaTags: true,
    48  	}
    49  	var l []*config
    50  
    51  	return r.Reflect(&l)
    52  }