2015-11-01 18:29:47 +00:00
|
|
|
// Package provider holds the different provider implementation.
|
2015-11-01 15:35:01 +00:00
|
|
|
package provider
|
2015-10-01 10:04:25 +00:00
|
|
|
|
|
|
|
import (
|
2016-02-19 16:10:48 +00:00
|
|
|
"fmt"
|
2015-10-01 10:04:25 +00:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
2015-11-01 15:35:01 +00:00
|
|
|
"time"
|
2015-10-01 10:04:25 +00:00
|
|
|
|
2016-04-19 20:06:33 +00:00
|
|
|
"errors"
|
2015-10-01 10:04:25 +00:00
|
|
|
"github.com/BurntSushi/ty/fun"
|
2016-09-19 17:08:39 +00:00
|
|
|
"github.com/cenk/backoff"
|
|
|
|
"github.com/containous/traefik/job"
|
2016-09-23 16:27:01 +00:00
|
|
|
"github.com/containous/traefik/log"
|
2016-03-31 16:57:08 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2015-11-01 15:35:01 +00:00
|
|
|
"github.com/docker/libkv"
|
2015-10-01 10:04:25 +00:00
|
|
|
"github.com/docker/libkv/store"
|
|
|
|
)
|
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
// Kv holds common configurations of key-value providers.
|
2015-11-02 18:48:34 +00:00
|
|
|
type Kv struct {
|
2016-06-24 07:58:42 +00:00
|
|
|
BaseProvider `mapstructure:",squash"`
|
2016-06-27 14:14:56 +00:00
|
|
|
Endpoint string `description:"Comma sepparated server endpoints"`
|
|
|
|
Prefix string `description:"Prefix used for KV store"`
|
|
|
|
TLS *ClientTLS `description:"Enable TLS support"`
|
2016-06-24 07:58:42 +00:00
|
|
|
storeType store.Backend
|
|
|
|
kvclient store.Store
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 15:18:55 +00:00
|
|
|
func (provider *Kv) createStore() (store.Store, error) {
|
|
|
|
storeConfig := &store.Config{
|
|
|
|
ConnectionTimeout: 30 * time.Second,
|
|
|
|
Bucket: "traefik",
|
|
|
|
}
|
|
|
|
|
|
|
|
if provider.TLS != nil {
|
|
|
|
var err error
|
|
|
|
storeConfig.TLS, err = provider.TLS.CreateTLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return libkv.NewStore(
|
|
|
|
provider.storeType,
|
|
|
|
strings.Split(provider.Endpoint, ","),
|
|
|
|
storeConfig,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:43:43 +00:00
|
|
|
func (provider *Kv) watchKv(configurationChan chan<- types.ConfigMessage, prefix string, stop chan bool) error {
|
2016-04-19 20:06:33 +00:00
|
|
|
operation := func() error {
|
2016-04-27 23:43:43 +00:00
|
|
|
events, err := provider.kvclient.WatchTree(provider.Prefix, make(chan struct{}))
|
2016-03-04 22:52:08 +00:00
|
|
|
if err != nil {
|
2016-04-27 23:43:43 +00:00
|
|
|
return fmt.Errorf("Failed to KV WatchTree: %v", err)
|
2016-03-04 22:52:08 +00:00
|
|
|
}
|
2016-04-19 20:06:33 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return nil
|
|
|
|
case _, ok := <-events:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("watchtree channel closed")
|
|
|
|
}
|
|
|
|
configuration := provider.loadConfig()
|
|
|
|
if configuration != nil {
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: string(provider.storeType),
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
2016-03-04 22:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 20:06:33 +00:00
|
|
|
|
|
|
|
notify := func(err error, time time.Duration) {
|
2016-04-27 23:43:43 +00:00
|
|
|
log.Errorf("KV connection error: %+v, retrying in %s", err, time)
|
2016-04-19 20:06:33 +00:00
|
|
|
}
|
2016-09-19 17:08:39 +00:00
|
|
|
err := backoff.RetryNotify(operation, job.NewBackOff(backoff.NewExponentialBackOff()), notify)
|
2016-04-19 20:06:33 +00:00
|
|
|
if err != nil {
|
2016-04-27 23:43:43 +00:00
|
|
|
return fmt.Errorf("Cannot connect to KV server: %v", err)
|
2016-04-19 20:06:33 +00:00
|
|
|
}
|
2016-04-27 23:43:43 +00:00
|
|
|
return nil
|
2016-03-04 22:52:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 18:27:04 +00:00
|
|
|
func (provider *Kv) provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
|
2016-06-20 23:00:52 +00:00
|
|
|
provider.Constraints = append(provider.Constraints, constraints...)
|
2016-04-19 20:06:33 +00:00
|
|
|
operation := func() error {
|
2016-07-13 15:18:55 +00:00
|
|
|
if _, err := provider.kvclient.Exists("qmslkjdfmqlskdjfmqlksjazçueznbvbwzlkajzebvkwjdcqmlsfj"); err != nil {
|
2016-04-27 23:43:43 +00:00
|
|
|
return fmt.Errorf("Failed to test KV store connection: %v", err)
|
2016-04-19 20:06:33 +00:00
|
|
|
}
|
|
|
|
if provider.Watch {
|
|
|
|
pool.Go(func(stop chan bool) {
|
2016-04-27 23:43:43 +00:00
|
|
|
err := provider.watchKv(configurationChan, provider.Prefix, stop)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot watch KV store: %v", err)
|
|
|
|
}
|
2016-04-19 20:06:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
configuration := provider.loadConfig()
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: string(provider.storeType),
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
|
|
|
return nil
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
2016-04-19 20:06:33 +00:00
|
|
|
notify := func(err error, time time.Duration) {
|
2016-04-27 23:43:43 +00:00
|
|
|
log.Errorf("KV connection error: %+v, retrying in %s", err, time)
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
2016-09-19 17:08:39 +00:00
|
|
|
err := backoff.RetryNotify(operation, job.NewBackOff(backoff.NewExponentialBackOff()), notify)
|
2016-04-19 20:06:33 +00:00
|
|
|
if err != nil {
|
2016-04-27 23:43:43 +00:00
|
|
|
return fmt.Errorf("Cannot connect to KV server: %v", err)
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
2015-10-01 10:04:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:48:34 +00:00
|
|
|
func (provider *Kv) loadConfig() *types.Configuration {
|
2015-10-01 10:04:25 +00:00
|
|
|
templateObjects := struct {
|
|
|
|
Prefix string
|
|
|
|
}{
|
2016-02-15 23:14:21 +00:00
|
|
|
// Allow `/traefik/alias` to superesede `provider.Prefix`
|
|
|
|
strings.TrimSuffix(provider.get(provider.Prefix, provider.Prefix+"/alias"), "/"),
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
2016-06-20 23:00:52 +00:00
|
|
|
|
2015-10-01 10:04:25 +00:00
|
|
|
var KvFuncMap = template.FuncMap{
|
2016-06-20 22:58:39 +00:00
|
|
|
"List": provider.list,
|
|
|
|
"ListServers": provider.listServers,
|
|
|
|
"Get": provider.get,
|
|
|
|
"SplitGet": provider.splitGet,
|
|
|
|
"Last": provider.last,
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
configuration, err := provider.getConfiguration("templates/kv.tmpl", KvFuncMap, templateObjects)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
2016-06-20 23:00:52 +00:00
|
|
|
|
|
|
|
for key, frontend := range configuration.Frontends {
|
|
|
|
if _, ok := configuration.Backends[frontend.Backend]; ok == false {
|
|
|
|
delete(configuration.Frontends, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
return configuration
|
|
|
|
}
|
2015-10-01 10:04:25 +00:00
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
func (provider *Kv) list(keys ...string) []string {
|
|
|
|
joinedKeys := strings.Join(keys, "")
|
|
|
|
keysPairs, err := provider.kvclient.List(joinedKeys)
|
2015-10-01 10:04:25 +00:00
|
|
|
if err != nil {
|
2016-09-23 16:27:01 +00:00
|
|
|
log.Debugf("Cannot get keys %s %s ", joinedKeys, err)
|
2015-10-01 10:04:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-11-13 10:50:32 +00:00
|
|
|
directoryKeys := make(map[string]string)
|
|
|
|
for _, key := range keysPairs {
|
2016-04-27 20:28:02 +00:00
|
|
|
directory := strings.Split(strings.TrimPrefix(key.Key, joinedKeys), "/")[0]
|
2015-11-13 10:50:32 +00:00
|
|
|
directoryKeys[directory] = joinedKeys + directory
|
|
|
|
}
|
|
|
|
return fun.Values(directoryKeys).([]string)
|
|
|
|
}
|
2015-10-01 10:04:25 +00:00
|
|
|
|
2016-06-20 22:58:39 +00:00
|
|
|
func (provider *Kv) listServers(backend string) []string {
|
|
|
|
serverNames := provider.list(backend, "/servers/")
|
|
|
|
return fun.Filter(func(serverName string) bool {
|
|
|
|
return provider.checkConstraints(serverName, "/tags")
|
|
|
|
}, serverNames).([]string)
|
|
|
|
}
|
|
|
|
|
2016-02-01 15:08:58 +00:00
|
|
|
func (provider *Kv) get(defaultValue string, keys ...string) string {
|
2015-11-13 10:50:32 +00:00
|
|
|
joinedKeys := strings.Join(keys, "")
|
2016-04-27 20:28:02 +00:00
|
|
|
keyPair, err := provider.kvclient.Get(strings.TrimPrefix(joinedKeys, "/"))
|
2015-11-13 10:50:32 +00:00
|
|
|
if err != nil {
|
2016-09-23 16:27:01 +00:00
|
|
|
log.Debugf("Cannot get key %s %s, setting default %s", joinedKeys, err, defaultValue)
|
2016-02-01 15:08:58 +00:00
|
|
|
return defaultValue
|
2015-11-13 10:50:32 +00:00
|
|
|
} else if keyPair == nil {
|
2016-09-23 16:27:01 +00:00
|
|
|
log.Debugf("Cannot get key %s, setting default %s", joinedKeys, defaultValue)
|
2016-02-01 15:08:58 +00:00
|
|
|
return defaultValue
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
2015-11-13 10:50:32 +00:00
|
|
|
return string(keyPair.Value)
|
|
|
|
}
|
2015-10-01 10:04:25 +00:00
|
|
|
|
2016-02-01 15:08:58 +00:00
|
|
|
func (provider *Kv) splitGet(keys ...string) []string {
|
|
|
|
joinedKeys := strings.Join(keys, "")
|
|
|
|
keyPair, err := provider.kvclient.Get(joinedKeys)
|
|
|
|
if err != nil {
|
2016-09-23 16:27:01 +00:00
|
|
|
log.Debugf("Cannot get key %s %s, setting default empty", joinedKeys, err)
|
2016-02-01 15:08:58 +00:00
|
|
|
return []string{}
|
|
|
|
} else if keyPair == nil {
|
2016-09-23 16:27:01 +00:00
|
|
|
log.Debugf("Cannot get key %s, setting default %empty", joinedKeys)
|
2016-02-01 15:08:58 +00:00
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return strings.Split(string(keyPair.Value), ",")
|
|
|
|
}
|
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
func (provider *Kv) last(key string) string {
|
|
|
|
splittedKey := strings.Split(key, "/")
|
|
|
|
return splittedKey[len(splittedKey)-1]
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
2016-06-20 23:00:52 +00:00
|
|
|
|
2016-06-20 22:58:39 +00:00
|
|
|
func (provider *Kv) checkConstraints(keys ...string) bool {
|
2016-06-20 23:00:52 +00:00
|
|
|
joinedKeys := strings.Join(keys, "")
|
|
|
|
keyPair, err := provider.kvclient.Get(joinedKeys)
|
|
|
|
|
|
|
|
value := ""
|
|
|
|
if err == nil && keyPair != nil && keyPair.Value != nil {
|
|
|
|
value = string(keyPair.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
constraintTags := strings.Split(value, ",")
|
|
|
|
ok, failingConstraint := provider.MatchConstraints(constraintTags)
|
|
|
|
if ok == false {
|
|
|
|
if failingConstraint != nil {
|
|
|
|
log.Debugf("Constraint %v not matching with following tags: %v", failingConstraint.String(), value)
|
|
|
|
}
|
2016-06-20 22:58:39 +00:00
|
|
|
return false
|
2016-06-20 23:00:52 +00:00
|
|
|
}
|
2016-06-20 22:58:39 +00:00
|
|
|
return true
|
2016-06-20 23:00:52 +00:00
|
|
|
}
|