refacto(constraints): Migration to Flaeg cli library

This commit is contained in:
Samuel BERTHE 2016-05-31 09:54:42 +02:00
parent f46accc74d
commit 1de5434e1a
19 changed files with 73 additions and 99 deletions

View file

@ -26,7 +26,7 @@ type GlobalConfiguration struct {
TraefikLogsFile string `description:"Traefik logs file"` TraefikLogsFile string `description:"Traefik logs file"`
LogLevel string `short:"l" description:"Log level"` LogLevel string `short:"l" description:"Log level"`
EntryPoints EntryPoints `description:"Entrypoints definition using format: --entryPoints='Name:http Address::8000 Redirect.EntryPoint:https' --entryPoints='Name:https Address::4442 TLS:tests/traefik.crt,tests/traefik.key'"` EntryPoints EntryPoints `description:"Entrypoints definition using format: --entryPoints='Name:http Address::8000 Redirect.EntryPoint:https' --entryPoints='Name:https Address::4442 TLS:tests/traefik.crt,tests/traefik.key'"`
Constraints []*types.Constraint `description:"Filter services by constraint, matching with service tags."` Constraints Constraints `description:"Filter services by constraint, matching with service tags."`
ACME *acme.ACME `description:"Enable ACME (Let's Encrypt): automatic SSL"` ACME *acme.ACME `description:"Enable ACME (Let's Encrypt): automatic SSL"`
DefaultEntryPoints DefaultEntryPoints `description:"Entrypoints to be used by frontends that do not specify any entrypoint"` DefaultEntryPoints DefaultEntryPoints `description:"Entrypoints to be used by frontends that do not specify any entrypoint"`
ProvidersThrottleDuration time.Duration `description:"Backends throttle duration: minimum duration between 2 events from providers before applying a new configuration. It avoids unnecessary reloads if multiples events are sent in a short amount of time."` ProvidersThrottleDuration time.Duration `description:"Backends throttle duration: minimum duration between 2 events from providers before applying a new configuration. It avoids unnecessary reloads if multiples events are sent in a short amount of time."`
@ -146,6 +146,41 @@ func (ep *EntryPoints) Type() string {
return fmt.Sprint("entrypoints²") return fmt.Sprint("entrypoints²")
} }
// Constraints holds a Constraint parser
type Constraints []types.Constraint
//Set []*Constraint
func (cs *Constraints) Set(str string) error {
exps := strings.Split(str, ",")
if len(exps) == 0 {
return errors.New("Bad Constraint format: " + str)
}
for _, exp := range exps {
constraint, err := types.NewConstraint(exp)
if err != nil {
return err
}
*cs = append(*cs, *constraint)
}
return nil
}
//Get []*Constraint
func (cs *Constraints) Get() interface{} { return []types.Constraint(*cs) }
//String returns []*Constraint in string
func (cs *Constraints) String() string { return fmt.Sprintf("%+v", *cs) }
//SetValue sets []*Constraint into the parser
func (cs *Constraints) SetValue(val interface{}) {
*cs = Constraints(val.([]types.Constraint))
}
// Type exports the Constraints type as a string
func (cs *Constraints) Type() string {
return fmt.Sprint("constraint²")
}
// EntryPoint holds an entry point configuration of the reverse proxy (ip, port, TLS...) // EntryPoint holds an entry point configuration of the reverse proxy (ip, port, TLS...)
type EntryPoint struct { type EntryPoint struct {
Network string Network string
@ -232,6 +267,7 @@ func NewTraefikDefaultPointersConfiguration() *TraefikConfiguration {
defaultMarathon.Watch = true defaultMarathon.Watch = true
defaultMarathon.Endpoint = "http://127.0.0.1:8080" defaultMarathon.Endpoint = "http://127.0.0.1:8080"
defaultMarathon.ExposedByDefault = true defaultMarathon.ExposedByDefault = true
defaultMarathon.Constraints = []types.Constraint{}
// default Consul // default Consul
var defaultConsul provider.Consul var defaultConsul provider.Consul
@ -239,10 +275,12 @@ func NewTraefikDefaultPointersConfiguration() *TraefikConfiguration {
defaultConsul.Endpoint = "127.0.0.1:8500" defaultConsul.Endpoint = "127.0.0.1:8500"
defaultConsul.Prefix = "/traefik" defaultConsul.Prefix = "/traefik"
defaultConsul.TLS = &provider.KvTLS{} defaultConsul.TLS = &provider.KvTLS{}
defaultConsul.Constraints = []types.Constraint{}
// default ConsulCatalog // default ConsulCatalog
var defaultConsulCatalog provider.ConsulCatalog var defaultConsulCatalog provider.ConsulCatalog
defaultConsulCatalog.Endpoint = "127.0.0.1:8500" defaultConsulCatalog.Endpoint = "127.0.0.1:8500"
defaultConsulCatalog.Constraints = []types.Constraint{}
// default Etcd // default Etcd
var defaultEtcd provider.Etcd var defaultEtcd provider.Etcd
@ -250,23 +288,27 @@ func NewTraefikDefaultPointersConfiguration() *TraefikConfiguration {
defaultEtcd.Endpoint = "127.0.0.1:400" defaultEtcd.Endpoint = "127.0.0.1:400"
defaultEtcd.Prefix = "/traefik" defaultEtcd.Prefix = "/traefik"
defaultEtcd.TLS = &provider.KvTLS{} defaultEtcd.TLS = &provider.KvTLS{}
defaultEtcd.Constraints = []types.Constraint{}
//default Zookeeper //default Zookeeper
var defaultZookeeper provider.Zookepper var defaultZookeeper provider.Zookepper
defaultZookeeper.Watch = true defaultZookeeper.Watch = true
defaultZookeeper.Endpoint = "127.0.0.1:2181" defaultZookeeper.Endpoint = "127.0.0.1:2181"
defaultZookeeper.Prefix = "/traefik" defaultZookeeper.Prefix = "/traefik"
defaultZookeeper.Constraints = []types.Constraint{}
//default Boltdb //default Boltdb
var defaultBoltDb provider.BoltDb var defaultBoltDb provider.BoltDb
defaultBoltDb.Watch = true defaultBoltDb.Watch = true
defaultBoltDb.Endpoint = "127.0.0.1:4001" defaultBoltDb.Endpoint = "127.0.0.1:4001"
defaultBoltDb.Prefix = "/traefik" defaultBoltDb.Prefix = "/traefik"
defaultBoltDb.Constraints = []types.Constraint{}
//default Kubernetes //default Kubernetes
var defaultKubernetes provider.Kubernetes var defaultKubernetes provider.Kubernetes
defaultKubernetes.Watch = true defaultKubernetes.Watch = true
defaultKubernetes.Endpoint = "127.0.0.1:8080" defaultKubernetes.Endpoint = "127.0.0.1:8080"
defaultKubernetes.Constraints = []types.Constraint{}
defaultConfiguration := GlobalConfiguration{ defaultConfiguration := GlobalConfiguration{
Docker: &defaultDocker, Docker: &defaultDocker,
@ -295,7 +337,7 @@ func NewTraefikConfiguration() *TraefikConfiguration {
TraefikLogsFile: "", TraefikLogsFile: "",
LogLevel: "ERROR", LogLevel: "ERROR",
EntryPoints: map[string]*EntryPoint{}, EntryPoints: map[string]*EntryPoint{},
Constraints: []*Constraint, Constraints: []types.Constraint{},
DefaultEntryPoints: []string{}, DefaultEntryPoints: []string{},
ProvidersThrottleDuration: time.Duration(2 * time.Second), ProvidersThrottleDuration: time.Duration(2 * time.Second),
MaxIdleConnsPerHost: 200, MaxIdleConnsPerHost: 200,

3
glide.lock generated
View file

@ -213,7 +213,6 @@ imports:
subpackages: subpackages:
- cipher - cipher
- json - json
- name: gopkg.in/yaml.v2
version: 7ad95dd0798a40da1ccdff6dff35fd177b5edf40
- name: github.com/ryanuber/go-glob - name: github.com/ryanuber/go-glob
version: 572520ed46dbddaed19ea3d9541bdd0494163693 version: 572520ed46dbddaed19ea3d9541bdd0494163693
devImports: []

View file

@ -1,8 +1,6 @@
package main package main
import ( import (
//"io/ioutil"
//"fmt"
"net/http" "net/http"
"os/exec" "os/exec"
"time" "time"

View file

@ -14,7 +14,7 @@ type BoltDb struct {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *BoltDb) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error { func (provider *BoltDb) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
provider.storeType = store.BOLTDB provider.storeType = store.BOLTDB
boltdb.Register() boltdb.Register()
return provider.provide(configurationChan, pool, constraints) return provider.provide(configurationChan, pool, constraints)

View file

@ -14,7 +14,7 @@ type Consul struct {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *Consul) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error { func (provider *Consul) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
provider.storeType = store.CONSUL provider.storeType = store.CONSUL
consul.Register() consul.Register()
return provider.provide(configurationChan, pool, constraints) return provider.provide(configurationChan, pool, constraints)

View file

@ -271,7 +271,7 @@ func (provider *ConsulCatalog) watch(configurationChan chan<- types.ConfigMessag
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *ConsulCatalog) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error { func (provider *ConsulCatalog) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
config := api.DefaultConfig() config := api.DefaultConfig()
config.Address = provider.Endpoint config.Address = provider.Endpoint
client, err := api.NewClient(config) client, err := api.NewClient(config)

View file

@ -79,7 +79,7 @@ func (provider *Docker) createClient() (client.APIClient, error) {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error { func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
provider.Constraints = append(provider.Constraints, constraints...) provider.Constraints = append(provider.Constraints, constraints...)
// TODO register this routine in pool, and watch for stop channel // TODO register this routine in pool, and watch for stop channel
safe.Go(func() { safe.Go(func() {

View file

@ -14,7 +14,7 @@ type Etcd struct {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *Etcd) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error { func (provider *Etcd) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
provider.storeType = store.ETCD provider.storeType = store.ETCD
etcd.Register() etcd.Register()
return provider.provide(configurationChan, pool, constraints) return provider.provide(configurationChan, pool, constraints)

View file

@ -19,7 +19,7 @@ type File struct {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *File) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ []*types.Constraint) error { func (provider *File) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ []types.Constraint) error {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
log.Error("Error creating file watcher", err) log.Error("Error creating file watcher", err)

View file

@ -81,7 +81,7 @@ func (provider *Kubernetes) createClient() (k8s.Client, error) {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *Kubernetes) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error { func (provider *Kubernetes) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
k8sClient, err := provider.createClient() k8sClient, err := provider.createClient()
if err != nil { if err != nil {
return err return err

View file

@ -73,7 +73,7 @@ func (provider *Kv) watchKv(configurationChan chan<- types.ConfigMessage, prefix
return nil return nil
} }
func (provider *Kv) provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error { func (provider *Kv) provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
storeConfig := &store.Config{ storeConfig := &store.Config{
ConnectionTimeout: 30 * time.Second, ConnectionTimeout: 30 * time.Second,
Bucket: "traefik", Bucket: "traefik",

View file

@ -42,7 +42,7 @@ type lightMarathonClient interface {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *Marathon) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error { func (provider *Marathon) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
provider.Constraints = append(provider.Constraints, constraints...) provider.Constraints = append(provider.Constraints, constraints...)
operation := func() error { operation := func() error {
config := marathon.NewDefaultConfig() config := marathon.NewDefaultConfig()

View file

@ -17,14 +17,14 @@ import (
type Provider interface { type Provider interface {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error
} }
// BaseProvider should be inherited by providers // BaseProvider should be inherited by providers
type BaseProvider struct { type BaseProvider struct {
Watch bool `description:"Watch provider"` Watch bool `description:"Watch provider"`
Filename string `description:"Override default configuration template. For advanced users :)"` Filename string `description:"Override default configuration template. For advanced users :)"`
Constraints []*types.Constraint `description:"Filter services by constraint, matching with Traefik tags."` Constraints []types.Constraint `description:"Filter services by constraint, matching with Traefik tags."`
} }
// MatchConstraints must match with EVERY single contraint // MatchConstraints must match with EVERY single contraint
@ -38,7 +38,7 @@ func (p *BaseProvider) MatchConstraints(tags []string) (bool, *types.Constraint)
for _, constraint := range p.Constraints { for _, constraint := range p.Constraints {
// xor: if ok and constraint.MustMatch are equal, then no tag is currently matching with the constraint // 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 { if ok := constraint.MatchConstraintWithAtLeastOneTag(tags); ok != constraint.MustMatch {
return false, constraint return false, &constraint
} }
} }

View file

@ -211,13 +211,13 @@ func TestGetConfigurationReturnsCorrectMaxConnConfiguration(t *testing.T) {
func TestMatchingConstraints(t *testing.T) { func TestMatchingConstraints(t *testing.T) {
cases := []struct { cases := []struct {
constraints []*types.Constraint constraints []types.Constraint
tags []string tags []string
expected bool expected bool
}{ }{
// simple test: must match // simple test: must match
{ {
constraints: []*types.Constraint{ constraints: []types.Constraint{
{ {
Key: "tag", Key: "tag",
MustMatch: true, MustMatch: true,
@ -231,7 +231,7 @@ func TestMatchingConstraints(t *testing.T) {
}, },
// simple test: must match but does not match // simple test: must match but does not match
{ {
constraints: []*types.Constraint{ constraints: []types.Constraint{
{ {
Key: "tag", Key: "tag",
MustMatch: true, MustMatch: true,
@ -245,7 +245,7 @@ func TestMatchingConstraints(t *testing.T) {
}, },
// simple test: must not match // simple test: must not match
{ {
constraints: []*types.Constraint{ constraints: []types.Constraint{
{ {
Key: "tag", Key: "tag",
MustMatch: false, MustMatch: false,
@ -259,7 +259,7 @@ func TestMatchingConstraints(t *testing.T) {
}, },
// complex test: globbing // complex test: globbing
{ {
constraints: []*types.Constraint{ constraints: []types.Constraint{
{ {
Key: "tag", Key: "tag",
MustMatch: true, MustMatch: true,
@ -273,7 +273,7 @@ func TestMatchingConstraints(t *testing.T) {
}, },
// complex test: multiple constraints // complex test: multiple constraints
{ {
constraints: []*types.Constraint{ constraints: []types.Constraint{
{ {
Key: "tag", Key: "tag",
MustMatch: true, MustMatch: true,

View file

@ -14,7 +14,7 @@ type Zookepper struct {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *Zookepper) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []*types.Constraint) error { func (provider *Zookepper) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints []types.Constraint) error {
provider.storeType = store.ZK provider.storeType = store.ZK
zookeeper.Register() zookeeper.Register()
return provider.provide(configurationChan, pool, constraints) return provider.provide(configurationChan, pool, constraints)

View file

@ -248,7 +248,7 @@ func (server *Server) startProviders() {
log.Infof("Starting provider %v %s", reflect.TypeOf(provider), jsonConf) log.Infof("Starting provider %v %s", reflect.TypeOf(provider), jsonConf)
currentProvider := provider currentProvider := provider
safe.Go(func() { safe.Go(func() {
err := currentProvider.Provide(server.configurationChan, &server.routinesPool, server.globalConfiguration.Constraints) err := currentProvider.Provide(server.configurationChan, &server.routinesPool, server.globalConfiguration.Constraints.Get().([]types.Constraint))
if err != nil { if err != nil {
log.Errorf("Error starting provider %s", err) log.Errorf("Error starting provider %s", err)
} }

View file

@ -8,6 +8,7 @@ import (
"github.com/containous/traefik/acme" "github.com/containous/traefik/acme"
"github.com/containous/traefik/middlewares" "github.com/containous/traefik/middlewares"
"github.com/containous/traefik/provider" "github.com/containous/traefik/provider"
"github.com/containous/traefik/types"
fmtlog "log" fmtlog "log"
"net/http" "net/http"
"os" "os"
@ -52,6 +53,8 @@ Complete documentation is available at https://traefik.io`,
//add custom parsers //add custom parsers
f.AddParser(reflect.TypeOf(EntryPoints{}), &EntryPoints{}) f.AddParser(reflect.TypeOf(EntryPoints{}), &EntryPoints{})
f.AddParser(reflect.TypeOf(DefaultEntryPoints{}), &DefaultEntryPoints{}) f.AddParser(reflect.TypeOf(DefaultEntryPoints{}), &DefaultEntryPoints{})
f.AddParser(reflect.TypeOf([]types.Constraint{}), &Constraints{})
f.AddParser(reflect.TypeOf(Constraints{}), &Constraints{})
f.AddParser(reflect.TypeOf(provider.Namespaces{}), &provider.Namespaces{}) f.AddParser(reflect.TypeOf(provider.Namespaces{}), &provider.Namespaces{})
f.AddParser(reflect.TypeOf([]acme.Domain{}), &acme.Domains{}) f.AddParser(reflect.TypeOf([]acme.Domain{}), &acme.Domains{})

View file

@ -2,10 +2,7 @@ package types
import ( import (
"errors" "errors"
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/ryanuber/go-glob" "github.com/ryanuber/go-glob"
"reflect"
"strings" "strings"
) )
@ -103,7 +100,8 @@ type Constraint struct {
Key string Key string
// MustMatch is true if operator is "==" or false if operator is "!=" // MustMatch is true if operator is "==" or false if operator is "!="
MustMatch bool MustMatch bool
Regex string // TODO: support regex
Regex string
} }
// NewConstraint receive a string and return a *Constraint, after checking syntax and parsing the constraint expression // NewConstraint receive a string and return a *Constraint, after checking syntax and parsing the constraint expression
@ -152,69 +150,3 @@ func (c *Constraint) MatchConstraintWithAtLeastOneTag(tags []string) bool {
} }
return false return false
} }
// StringToConstraintHookFunc returns a DecodeHookFunc that converts strings to Constraint.
// This hook is triggered during the configuration file unmarshal-ing
func StringToConstraintHookFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(&Constraint{}) {
return data, nil
}
constraint, err := NewConstraint(data.(string))
if err != nil {
return data, err
}
return constraint, nil
}
}
// Constraints own a pointer on globalConfiguration.Constraints and supports a Set() method (not possible on a slice)
// interface:
type Constraints struct {
value *[]*Constraint
changed bool
}
// Set receive a cli argument and add it to globalConfiguration
func (cs *Constraints) Set(value string) error {
exps := strings.Split(value, ",")
if len(exps) == 0 {
return errors.New("Bad Constraint format: " + value)
}
for _, exp := range exps {
constraint, err := NewConstraint(exp)
if err != nil {
return err
}
*cs.value = append(*cs.value, constraint)
}
return nil
}
// Type exports the Constraints type as a string
func (cs *Constraints) Type() string {
return "constraints"
}
func (cs *Constraints) String() string {
return fmt.Sprintln("%v", *cs.value)
}
// NewConstraintSliceValue make an alias of []*Constraint to Constraints for the command line
// Viper does not supprt SliceVar value types
// Constraints.Set called by viper will fill the []*Constraint slice
func NewConstraintSliceValue(p *[]*Constraint) *Constraints {
cs := new(Constraints)
cs.value = p
if p == nil {
*cs.value = []*Constraint{}
}
return cs
}

2
web.go
View file

@ -46,7 +46,7 @@ func goroutines() interface{} {
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *WebProvider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ []*types.Constraint) error { func (provider *WebProvider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ []types.Constraint) error {
systemRouter := mux.NewRouter() systemRouter := mux.NewRouter()
// health route // health route