/
manager_entry.go
74 lines (62 loc) 路 2.51 KB
/
manager_entry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cluster
import "fmt"
type (
// APIObjectGenerator returns an implementor of the APIObject interface.
APIObjectGenerator func() APIObject
// ParsedProcessor fills the Config struct from the parsed API objects in ObjectLookup.
ParsedProcessor func(*Config, ObjectLookup)
// Validation performs a validation over the Config object.
Validation func(*Config) error
// Defaulter sets defaults in a Config object.
Defaulter func(*Config) error
)
// ConfigManagerEntry allows to declare the necessary configuration to parse
// from yaml, set defaults and validate a Cluster struct for one or more types.
// It is semantically equivalent to use the individual register methods and its
// only purpose is convenience.
type ConfigManagerEntry struct {
APIObjectMapping map[string]APIObjectGenerator
Processors []ParsedProcessor
Validations []Validation
Defaulters []Defaulter
}
// NewConfigManagerEntry builds a ConfigManagerEntry with empty configuration.
func NewConfigManagerEntry() *ConfigManagerEntry {
return &ConfigManagerEntry{
APIObjectMapping: map[string]APIObjectGenerator{},
}
}
// Merge combines the configuration declared in multiple ConfigManagerEntry.
func (c *ConfigManagerEntry) Merge(entries ...*ConfigManagerEntry) error {
for _, config := range entries {
for k, v := range config.APIObjectMapping {
if err := c.RegisterMapping(k, v); err != nil {
return err
}
}
c.RegisterProcessors(config.Processors...)
c.RegisterDefaulters(config.Defaulters...)
c.RegisterValidations(config.Validations...)
}
return nil
}
// RegisterMapping records the mapping between a kubernetes Kind and an API concrete type.
func (c *ConfigManagerEntry) RegisterMapping(kind string, generator APIObjectGenerator) error {
if _, ok := c.APIObjectMapping[kind]; ok {
return fmt.Errorf("mapping for api object %s already registered", kind)
}
c.APIObjectMapping[kind] = generator
return nil
}
// RegisterProcessors records setters to fill the Config struct from the parsed API objects.
func (c *ConfigManagerEntry) RegisterProcessors(processors ...ParsedProcessor) {
c.Processors = append(c.Processors, processors...)
}
// RegisterValidations records validations for a Config struct.
func (c *ConfigManagerEntry) RegisterValidations(validations ...Validation) {
c.Validations = append(c.Validations, validations...)
}
// RegisterDefaulters records defaults for a Config struct.
func (c *ConfigManagerEntry) RegisterDefaulters(defaulters ...Defaulter) {
c.Defaulters = append(c.Defaulters, defaulters...)
}