Skip to content

Commit

Permalink
add per path config getter in the ann mapper
Browse files Browse the repository at this point in the history
A small refactor in the annotation mapper to allow a direct reference to the per path configs. Most of the other methods should be removed after convert the annotation parsers.
  • Loading branch information
jcmoraisjr committed Sep 10, 2020
1 parent b6d5cba commit 9aa50f9
Showing 1 changed file with 63 additions and 27 deletions.
90 changes: 63 additions & 27 deletions pkg/converters/ingress/annotations/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ type MapBuilder struct {
// Mapper ...
type Mapper struct {
MapBuilder
maps map[string][]*Map
maps map[string][]*Map
configs map[hatypes.PathLink]*AnnConfig
}

// AnnConfig ...
type AnnConfig struct {
mapper *Mapper
keys map[string]*ConfigValue
}

// Map ...
//
// TODO rename URI to Hostpath -- currently this is a little mess.
// Fix also testCase data in order to represent a hostname+path.
// Hostname is the domain name. Path is the declared starting path on ingress
// Together they populate a map_beg() converter in order to match HAProxy's
// `base` sample fetch method.
//
type Map struct {
Source *Source
Link hatypes.PathLink
Expand Down Expand Up @@ -84,41 +84,56 @@ func NewMapBuilder(logger types.Logger, annPrefix string, annDefaults map[string
func (b *MapBuilder) NewMapper() *Mapper {
return &Mapper{
MapBuilder: *b,
maps: map[string][]*Map{},
//
maps: map[string][]*Map{},
configs: map[hatypes.PathLink]*AnnConfig{},
}
}

func (c *Mapper) addAnnotation(source *Source, link hatypes.PathLink, key, value string) (conflict bool) {
conflict = false
annMaps, found := c.maps[key]
func newAnnConfig(mapper *Mapper) *AnnConfig {
return &AnnConfig{
mapper: mapper,
keys: map[string]*ConfigValue{},
}
}

// Add a new annotation to the current mapper.
// Return the conflict state: true if a conflict was found, false if the annotation was assigned or at least handled
func (c *Mapper) addAnnotation(source *Source, link hatypes.PathLink, key, value string) bool {
if link.IsEmpty() {
// empty means default value
// empty means default value, cannot register as an annotation
panic("path link cannot be empty")
}
if found {
for _, annMap := range annMaps {
if annMap.Link == link {
return annMap.Value != value
}
}
// check overlap
config, configfound := c.configs[link]
if !configfound {
config = newAnnConfig(c)
c.configs[link] = config
}
var realValue string
var ok bool
validator, found := validators[key]
if found {
if cfg, found := config.keys[key]; found {
return cfg.Value != value
}
// validate (bool; int; ...) and normalize (int "01" => "1"; ...)
realValue := value
if validator, found := validators[key]; found {
var ok bool
if realValue, ok = validator(validate{logger: c.logger, source: source, key: key, value: value}); !ok {
return
return false
}
} else {
realValue = value
}
// update internal fields
config.keys[key] = &ConfigValue{
Source: source,
Value: realValue,
}
annMaps, _ := c.maps[key]
annMaps = append(annMaps, &Map{
Source: source,
Link: link,
Value: realValue,
})
c.maps[key] = annMaps
return
return false
}

// AddAnnotations ...
Expand All @@ -145,6 +160,16 @@ func (c *Mapper) GetStrMap(key string) ([]*Map, bool) {
return nil, false
}

// GetConfig ...
func (c *Mapper) GetConfig(link hatypes.PathLink) *AnnConfig {
if config, found := c.configs[link]; found {
return config
}
config := newAnnConfig(c)
c.configs[link] = config
return config
}

// Get ...
func (c *Mapper) Get(key string) *ConfigValue {
annMaps, found := c.GetStrMap(key)
Expand Down Expand Up @@ -262,6 +287,17 @@ func findConfig(config []*BackendConfig, kv map[string]*ConfigValue) *BackendCon
return nil
}

// Get ...
func (c *AnnConfig) Get(key string) *ConfigValue {
if value, found := c.keys[key]; found {
return value
}
if value, found := c.mapper.annDefaults[key]; found {
return &ConfigValue{Value: value}
}
return &ConfigValue{}
}

// ConfigEquals ...
func (b *BackendConfig) ConfigEquals(other map[string]*ConfigValue) bool {
if len(b.Config) != len(other) {
Expand Down

0 comments on commit 9aa50f9

Please sign in to comment.