2017-04-17 10:50:02 +00:00
|
|
|
package kv
|
2015-10-01 10:04:25 +00:00
|
|
|
|
|
|
|
import (
|
2016-12-30 08:21:13 +00:00
|
|
|
"errors"
|
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
|
|
|
|
|
|
|
"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"
|
2017-04-17 10:50:02 +00:00
|
|
|
"github.com/containous/traefik/provider"
|
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"
|
|
|
|
)
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provider holds common configurations of key-value providers.
|
|
|
|
type Provider struct {
|
2017-10-02 08:32:02 +00:00
|
|
|
provider.BaseProvider `mapstructure:",squash" export:"true"`
|
2017-08-25 16:22:03 +00:00
|
|
|
Endpoint string `description:"Comma separated server endpoints"`
|
2017-10-02 08:32:02 +00:00
|
|
|
Prefix string `description:"Prefix used for KV store" export:"true"`
|
|
|
|
TLS *types.ClientTLS `description:"Enable TLS support" export:"true"`
|
2017-08-25 16:22:03 +00:00
|
|
|
Username string `description:"KV Username"`
|
|
|
|
Password string `description:"KV Password"`
|
2017-05-11 17:09:06 +00:00
|
|
|
storeType store.Backend
|
|
|
|
kvclient store.Store
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// CreateStore create the K/V store
|
|
|
|
func (p *Provider) CreateStore() (store.Store, error) {
|
2016-07-13 15:18:55 +00:00
|
|
|
storeConfig := &store.Config{
|
|
|
|
ConnectionTimeout: 30 * time.Second,
|
|
|
|
Bucket: "traefik",
|
2017-03-28 15:54:48 +00:00
|
|
|
Username: p.Username,
|
|
|
|
Password: p.Password,
|
2016-07-13 15:18:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
if p.TLS != nil {
|
2016-07-13 15:18:55 +00:00
|
|
|
var err error
|
2017-04-17 10:50:02 +00:00
|
|
|
storeConfig.TLS, err = p.TLS.CreateTLSConfig()
|
2016-07-13 15:18:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return libkv.NewStore(
|
2017-05-11 17:09:06 +00:00
|
|
|
p.storeType,
|
2017-04-17 10:50:02 +00:00
|
|
|
strings.Split(p.Endpoint, ","),
|
2016-07-13 15:18:55 +00:00
|
|
|
storeConfig,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-05-11 17:09:06 +00:00
|
|
|
// SetStoreType storeType setter
|
|
|
|
func (p *Provider) SetStoreType(storeType store.Backend) {
|
|
|
|
p.storeType = storeType
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKVClient kvclient setter
|
|
|
|
func (p *Provider) SetKVClient(kvClient store.Store) {
|
|
|
|
p.kvclient = kvClient
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) watchKv(configurationChan chan<- types.ConfigMessage, prefix string, stop chan bool) error {
|
2016-04-19 20:06:33 +00:00
|
|
|
operation := func() error {
|
2017-05-11 17:09:06 +00:00
|
|
|
events, err := p.kvclient.WatchTree(p.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")
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
configuration := p.loadConfig()
|
2016-04-19 20:06:33 +00:00
|
|
|
if configuration != nil {
|
|
|
|
configurationChan <- types.ConfigMessage{
|
2017-05-11 17:09:06 +00:00
|
|
|
ProviderName: string(p.storeType),
|
2016-04-19 20:06:33 +00:00
|
|
|
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-12-08 12:32:12 +00:00
|
|
|
err := backoff.RetryNotify(safe.OperationWithRecover(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
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provide provides the configuration to traefik via the configuration channel
|
|
|
|
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
|
|
|
|
p.Constraints = append(p.Constraints, constraints...)
|
2016-04-19 20:06:33 +00:00
|
|
|
operation := func() error {
|
2017-11-14 10:38:03 +00:00
|
|
|
if _, err := p.kvclient.Exists(p.Prefix + "/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
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
if p.Watch {
|
2016-04-19 20:06:33 +00:00
|
|
|
pool.Go(func(stop chan bool) {
|
2017-04-17 10:50:02 +00:00
|
|
|
err := p.watchKv(configurationChan, p.Prefix, stop)
|
2016-04-27 23:43:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot watch KV store: %v", err)
|
|
|
|
}
|
2016-04-19 20:06:33 +00:00
|
|
|
})
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
configuration := p.loadConfig()
|
2016-04-19 20:06:33 +00:00
|
|
|
configurationChan <- types.ConfigMessage{
|
2017-05-11 17:09:06 +00:00
|
|
|
ProviderName: string(p.storeType),
|
2016-04-19 20:06:33 +00:00
|
|
|
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-12-08 12:32:12 +00:00
|
|
|
err := backoff.RetryNotify(safe.OperationWithRecover(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
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) loadConfig() *types.Configuration {
|
2015-10-01 10:04:25 +00:00
|
|
|
templateObjects := struct {
|
|
|
|
Prefix string
|
|
|
|
}{
|
2017-04-17 10:50:02 +00:00
|
|
|
// Allow `/traefik/alias` to superesede `p.Prefix`
|
|
|
|
strings.TrimSuffix(p.get(p.Prefix, p.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{
|
2017-10-10 09:10:02 +00:00
|
|
|
"List": p.list,
|
|
|
|
"ListServers": p.listServers,
|
|
|
|
"Get": p.get,
|
|
|
|
"SplitGet": p.splitGet,
|
|
|
|
"Last": p.last,
|
2017-10-16 15:38:03 +00:00
|
|
|
"getSticky": p.getSticky,
|
2017-10-10 09:10:02 +00:00
|
|
|
"hasStickinessLabel": p.hasStickinessLabel,
|
|
|
|
"getStickinessCookieName": p.getStickinessCookieName,
|
2015-10-01 10:04:25 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
configuration, err := p.GetConfiguration("templates/kv.tmpl", KvFuncMap, templateObjects)
|
2015-11-13 10:50:32 +00:00
|
|
|
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 {
|
2017-08-18 00:18:02 +00:00
|
|
|
if _, ok := configuration.Backends[frontend.Backend]; !ok {
|
2016-06-20 23:00:52 +00:00
|
|
|
delete(configuration.Frontends, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
return configuration
|
|
|
|
}
|
2015-10-01 10:04:25 +00:00
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) list(keys ...string) []string {
|
2015-11-13 10:50:32 +00:00
|
|
|
joinedKeys := strings.Join(keys, "")
|
2017-05-11 17:09:06 +00:00
|
|
|
keysPairs, err := p.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
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) listServers(backend string) []string {
|
|
|
|
serverNames := p.list(backend, "/servers/")
|
2016-06-20 22:58:39 +00:00
|
|
|
return fun.Filter(func(serverName string) bool {
|
2017-03-07 16:11:43 +00:00
|
|
|
key := fmt.Sprint(serverName, "/url")
|
2017-05-11 17:09:06 +00:00
|
|
|
if _, err := p.kvclient.Get(key); err != nil {
|
2017-03-07 16:11:43 +00:00
|
|
|
if err != store.ErrKeyNotFound {
|
|
|
|
log.Errorf("Failed to retrieve value for key %s: %s", key, err)
|
|
|
|
}
|
2017-02-26 23:25:54 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
return p.checkConstraints(serverName, "/tags")
|
2016-06-20 22:58:39 +00:00
|
|
|
}, serverNames).([]string)
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) get(defaultValue string, keys ...string) string {
|
2015-11-13 10:50:32 +00:00
|
|
|
joinedKeys := strings.Join(keys, "")
|
2017-05-11 17:09:06 +00:00
|
|
|
keyPair, err := p.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
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) splitGet(keys ...string) []string {
|
2016-02-01 15:08:58 +00:00
|
|
|
joinedKeys := strings.Join(keys, "")
|
2017-05-11 17:09:06 +00:00
|
|
|
keyPair, err := p.kvclient.Get(joinedKeys)
|
2016-02-01 15:08:58 +00:00
|
|
|
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), ",")
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) last(key string) string {
|
2015-11-13 10:50:32 +00:00
|
|
|
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
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) checkConstraints(keys ...string) bool {
|
2016-06-20 23:00:52 +00:00
|
|
|
joinedKeys := strings.Join(keys, "")
|
2017-05-11 17:09:06 +00:00
|
|
|
keyPair, err := p.kvclient.Get(joinedKeys)
|
2016-06-20 23:00:52 +00:00
|
|
|
|
|
|
|
value := ""
|
|
|
|
if err == nil && keyPair != nil && keyPair.Value != nil {
|
|
|
|
value = string(keyPair.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
constraintTags := strings.Split(value, ",")
|
2017-04-17 10:50:02 +00:00
|
|
|
ok, failingConstraint := p.MatchConstraints(constraintTags)
|
2017-08-18 00:18:02 +00:00
|
|
|
if !ok {
|
2016-06-20 23:00:52 +00:00
|
|
|
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
|
|
|
}
|
2017-10-10 09:10:02 +00:00
|
|
|
|
2017-10-16 15:38:03 +00:00
|
|
|
func (p *Provider) getSticky(rootPath string) string {
|
|
|
|
stickyValue := p.get("", rootPath, "/loadbalancer", "/sticky")
|
|
|
|
if len(stickyValue) > 0 {
|
2017-10-12 15:50:03 +00:00
|
|
|
log.Warnf("Deprecated configuration found: %s. Please use %s.", "loadbalancer/sticky", "loadbalancer/stickiness")
|
2017-10-16 15:38:03 +00:00
|
|
|
} else {
|
|
|
|
stickyValue = "false"
|
2017-10-10 09:10:02 +00:00
|
|
|
}
|
2017-10-16 15:38:03 +00:00
|
|
|
return stickyValue
|
|
|
|
}
|
2017-10-10 09:10:02 +00:00
|
|
|
|
2017-10-16 15:38:03 +00:00
|
|
|
func (p *Provider) hasStickinessLabel(rootPath string) bool {
|
2017-10-12 15:50:03 +00:00
|
|
|
stickinessValue := p.get("false", rootPath, "/loadbalancer", "/stickiness")
|
2017-10-16 15:38:03 +00:00
|
|
|
return len(stickinessValue) > 0 && strings.EqualFold(strings.TrimSpace(stickinessValue), "true")
|
2017-10-10 09:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) getStickinessCookieName(rootPath string) string {
|
|
|
|
return p.get("", rootPath, "/loadbalancer", "/stickiness", "/cookiename")
|
|
|
|
}
|