2015-11-01 15:35:01 +00:00
|
|
|
package provider
|
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2016-05-30 13:05:58 +00:00
|
|
|
"unicode"
|
2015-11-13 10:50:32 +00:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/autogen"
|
2016-04-13 18:36:23 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2015-11-13 10:50:32 +00:00
|
|
|
)
|
2015-11-01 15:35:01 +00:00
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
// Provider defines methods of a provider.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Provider interface {
|
2015-11-01 18:29:47 +00:00
|
|
|
// Provide allows the provider to provide configurations to traefik
|
|
|
|
// using the given configuration channel.
|
2016-05-31 07:54:42 +00:00
|
|
|
Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
2015-11-13 10:50:32 +00:00
|
|
|
|
2016-01-13 21:46:44 +00:00
|
|
|
// BaseProvider should be inherited by providers
|
|
|
|
type BaseProvider struct {
|
2016-06-02 13:17:04 +00:00
|
|
|
Watch bool `description:"Watch provider"`
|
|
|
|
Filename string `description:"Override default configuration template. For advanced users :)"`
|
|
|
|
Constraints types.Constraints `description:"Filter services by constraint, matching with Traefik tags."`
|
2016-05-30 13:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MatchConstraints must match with EVERY single contraint
|
|
|
|
// returns first constraint that do not match or nil
|
2016-05-20 15:17:38 +00:00
|
|
|
func (p *BaseProvider) MatchConstraints(tags []string) (bool, *types.Constraint) {
|
2016-05-30 13:05:58 +00:00
|
|
|
// if there is no tags and no contraints, filtering is disabled
|
|
|
|
if len(tags) == 0 && len(p.Constraints) == 0 {
|
2016-05-20 15:17:38 +00:00
|
|
|
return true, nil
|
2016-05-30 13:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, constraint := range p.Constraints {
|
2016-05-20 15:17:38 +00:00
|
|
|
// xor: if ok and constraint.MustMatch are equal, then no tag is currently matching with the constraint
|
|
|
|
if ok := constraint.MatchConstraintWithAtLeastOneTag(tags); ok != constraint.MustMatch {
|
2016-05-31 07:54:42 +00:00
|
|
|
return false, &constraint
|
2016-05-30 13:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no constraint or every constraints matching
|
2016-05-20 15:17:38 +00:00
|
|
|
return true, nil
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:46:44 +00:00
|
|
|
func (p *BaseProvider) getConfiguration(defaultTemplateFile string, funcMap template.FuncMap, templateObjects interface{}) (*types.Configuration, error) {
|
2015-11-13 10:50:32 +00:00
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
configuration := new(types.Configuration)
|
|
|
|
tmpl := template.New(p.Filename).Funcs(funcMap)
|
|
|
|
if len(p.Filename) > 0 {
|
|
|
|
buf, err = ioutil.ReadFile(p.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf, err = autogen.Asset(defaultTemplateFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = tmpl.Parse(string(buf))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
err = tmpl.Execute(&buffer, templateObjects)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := toml.Decode(buffer.String(), configuration); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return configuration, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func replace(s1 string, s2 string, s3 string) string {
|
|
|
|
return strings.Replace(s3, s1, s2, -1)
|
|
|
|
}
|
|
|
|
|
2016-03-27 00:05:17 +00:00
|
|
|
func normalize(name string) string {
|
|
|
|
fargs := func(c rune) bool {
|
|
|
|
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
|
|
|
|
}
|
|
|
|
// get function
|
|
|
|
return strings.Join(strings.FieldsFunc(name, fargs), "-")
|
|
|
|
}
|