feat(constraints): Implementation of constraint filtering (cmd + toml + matching functions), implementation proposal with consul

This commit is contained in:
Samuel BERTHE 2016-05-30 15:05:58 +02:00
parent 39fa8f7be4
commit ac087921d8
17 changed files with 209 additions and 25 deletions

View file

@ -5,25 +5,46 @@ import (
"io/ioutil"
"strings"
"text/template"
"unicode"
"github.com/BurntSushi/toml"
"github.com/containous/traefik/autogen"
"github.com/containous/traefik/safe"
"github.com/containous/traefik/types"
"unicode"
)
// Provider defines methods of a provider.
type Provider interface {
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool) error
Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error
}
// BaseProvider should be inherited by providers
type BaseProvider struct {
Watch bool `description:"Watch provider"`
Filename string `description:"Override default configuration template. For advanced users :)"`
Constraints []*types.Constraint `description:"Filter services by constraint, matching with Traefik tags."`
}
// MatchConstraints must match with EVERY single contraint
// returns first constraint that do not match or nil
// returns errors for future use (regex)
func (p *BaseProvider) MatchConstraints(tags []string) (bool, *types.Constraint, error) {
// if there is no tags and no contraints, filtering is disabled
if len(tags) == 0 && len(p.Constraints) == 0 {
return true, nil, nil
}
for _, constraint := range p.Constraints {
if ok := constraint.MatchConstraintWithAtLeastOneTag(tags); xor(ok == true, constraint.MustMatch == true) {
return false, constraint, nil
}
}
// If no constraint or every constraints matching
return true, nil, nil
>>>>>>> e844462... feat(constraints): Implementation of constraints (cmd + toml + matching functions), implementation proposal with consul
}
func (p *BaseProvider) getConfiguration(defaultTemplateFile string, funcMap template.FuncMap, templateObjects interface{}) (*types.Configuration, error) {
@ -77,3 +98,8 @@ func normalize(name string) string {
// get function
return strings.Join(strings.FieldsFunc(name, fargs), "-")
}
// golang does not support ^ operator
func xor(cond1 bool, cond2 bool) bool {
return cond1 != cond2
}