2017-04-17 10:50:02 +00:00
|
|
|
package file
|
2015-09-07 13:25:13 +00:00
|
|
|
|
|
|
|
import (
|
2018-11-14 09:18:03 +00:00
|
|
|
"context"
|
2017-05-26 13:32:03 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-11-09 11:16:03 +00:00
|
|
|
"os"
|
2017-05-26 13:32:03 +00:00
|
|
|
"path"
|
2017-11-09 11:16:03 +00:00
|
|
|
"path/filepath"
|
2015-09-07 15:39:22 +00:00
|
|
|
"strings"
|
2018-03-22 15:14:04 +00:00
|
|
|
"text/template"
|
2015-09-24 15:16:13 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/containous/traefik/config"
|
2016-08-18 12:20:11 +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"
|
2017-11-09 11:16:03 +00:00
|
|
|
"github.com/containous/traefik/tls"
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2018-05-22 10:02:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-09-24 15:16:13 +00:00
|
|
|
"gopkg.in/fsnotify.v1"
|
2015-09-07 13:25:13 +00:00
|
|
|
)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
const providerName = "file"
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
var _ provider.Provider = (*Provider)(nil)
|
2016-08-16 17:13:18 +00:00
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provider holds configurations of the provider.
|
|
|
|
type Provider struct {
|
2017-10-02 08:32:02 +00:00
|
|
|
provider.BaseProvider `mapstructure:",squash" export:"true"`
|
|
|
|
Directory string `description:"Load configuration from one or more .toml files in a directory" export:"true"`
|
2018-05-22 10:02:03 +00:00
|
|
|
TraefikFile string
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 07:08:03 +00:00
|
|
|
// Init the provider
|
|
|
|
func (p *Provider) Init(constraints types.Constraints) error {
|
|
|
|
return p.BaseProvider.Init(constraints)
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provide allows the file provider to provide configurations to traefik
|
2015-11-01 18:29:47 +00:00
|
|
|
// using the given configuration channel.
|
2018-11-14 09:18:03 +00:00
|
|
|
func (p *Provider) Provide(configurationChan chan<- config.Message, pool *safe.Pool) error {
|
2017-12-02 18:25:29 +00:00
|
|
|
configuration, err := p.BuildConfiguration()
|
2017-05-26 13:32:03 +00:00
|
|
|
|
2015-09-07 13:25:13 +00:00
|
|
|
if err != nil {
|
2015-10-01 10:04:25 +00:00
|
|
|
return err
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 13:32:03 +00:00
|
|
|
if p.Watch {
|
|
|
|
var watchItem string
|
|
|
|
|
2018-05-22 10:02:03 +00:00
|
|
|
if len(p.Directory) > 0 {
|
2017-05-26 13:32:03 +00:00
|
|
|
watchItem = p.Directory
|
2018-05-22 10:02:03 +00:00
|
|
|
} else if len(p.Filename) > 0 {
|
2017-11-09 11:16:03 +00:00
|
|
|
watchItem = filepath.Dir(p.Filename)
|
2018-05-22 10:02:03 +00:00
|
|
|
} else {
|
|
|
|
watchItem = filepath.Dir(p.TraefikFile)
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.addWatcher(pool, watchItem, configurationChan, p.watcherCallback); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendConfigToChannel(configurationChan, configuration)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:25:29 +00:00
|
|
|
// BuildConfiguration loads configuration either from file or a directory specified by 'Filename'/'Directory'
|
2017-11-21 09:24:03 +00:00
|
|
|
// and returns a 'Configuration' object
|
2018-11-14 09:18:03 +00:00
|
|
|
func (p *Provider) BuildConfiguration() (*config.Configuration, error) {
|
|
|
|
ctx := log.With(context.Background(), log.Str(log.ProviderName, providerName))
|
|
|
|
|
2018-05-22 10:02:03 +00:00
|
|
|
if len(p.Directory) > 0 {
|
2018-11-14 09:18:03 +00:00
|
|
|
return p.loadFileConfigFromDirectory(ctx, p.Directory, nil)
|
2017-11-21 09:24:03 +00:00
|
|
|
}
|
2018-05-22 10:02:03 +00:00
|
|
|
|
|
|
|
if len(p.Filename) > 0 {
|
|
|
|
return p.loadFileConfig(p.Filename, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.TraefikFile) > 0 {
|
|
|
|
return p.loadFileConfig(p.TraefikFile, false)
|
|
|
|
}
|
|
|
|
|
2018-08-06 18:00:03 +00:00
|
|
|
return nil, errors.New("error using file configuration backend, no filename defined")
|
2017-11-21 09:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func (p *Provider) addWatcher(pool *safe.Pool, directory string, configurationChan chan<- config.Message, callback func(chan<- config.Message, fsnotify.Event)) error {
|
2017-05-26 13:32:03 +00:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
2015-09-07 13:25:13 +00:00
|
|
|
if err != nil {
|
2017-05-26 13:32:03 +00:00
|
|
|
return fmt.Errorf("error creating file watcher: %s", err)
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 10:02:03 +00:00
|
|
|
err = watcher.Add(directory)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error adding file watcher: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-26 13:32:03 +00:00
|
|
|
// Process events
|
|
|
|
pool.Go(func(stop chan bool) {
|
|
|
|
defer watcher.Close()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
case evt := <-watcher.Events:
|
2017-11-09 11:16:03 +00:00
|
|
|
if p.Directory == "" {
|
2018-05-22 10:02:03 +00:00
|
|
|
var filename string
|
|
|
|
if len(p.Filename) > 0 {
|
|
|
|
filename = p.Filename
|
|
|
|
} else {
|
|
|
|
filename = p.TraefikFile
|
|
|
|
}
|
|
|
|
|
2017-11-09 11:16:03 +00:00
|
|
|
_, evtFileName := filepath.Split(evt.Name)
|
2018-05-22 10:02:03 +00:00
|
|
|
_, confFileName := filepath.Split(filename)
|
2017-11-09 11:16:03 +00:00
|
|
|
if evtFileName == confFileName {
|
|
|
|
callback(configurationChan, evt)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback(configurationChan, evt)
|
|
|
|
}
|
2017-05-26 13:32:03 +00:00
|
|
|
case err := <-watcher.Errors:
|
2018-11-14 09:18:03 +00:00
|
|
|
log.WithoutContext().WithField(log.ProviderName, providerName).Errorf("Watcher event error: %s", err)
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
2015-10-03 14:50:53 +00:00
|
|
|
}
|
2017-05-26 13:32:03 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func (p *Provider) watcherCallback(configurationChan chan<- config.Message, event fsnotify.Event) {
|
2018-05-22 10:02:03 +00:00
|
|
|
watchItem := p.TraefikFile
|
|
|
|
if len(p.Directory) > 0 {
|
2017-11-21 09:24:03 +00:00
|
|
|
watchItem = p.Directory
|
2018-05-22 10:02:03 +00:00
|
|
|
} else if len(p.Filename) > 0 {
|
|
|
|
watchItem = p.Filename
|
2017-11-21 09:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
logger := log.WithoutContext().WithField(log.ProviderName, providerName)
|
|
|
|
|
2017-11-21 09:24:03 +00:00
|
|
|
if _, err := os.Stat(watchItem); err != nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Errorf("Unable to watch %s : %v", watchItem, err)
|
2017-11-21 09:24:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:25:29 +00:00
|
|
|
configuration, err := p.BuildConfiguration()
|
2017-11-21 09:24:03 +00:00
|
|
|
if err != nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Errorf("Error occurred during watcher callback: %s", err)
|
2017-11-21 09:24:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendConfigToChannel(configurationChan, configuration)
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func sendConfigToChannel(configurationChan chan<- config.Message, configuration *config.Configuration) {
|
|
|
|
configurationChan <- config.Message{
|
2015-11-13 10:50:32 +00:00
|
|
|
ProviderName: "file",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 15:14:04 +00:00
|
|
|
func readFile(filename string) (string, error) {
|
|
|
|
if len(filename) > 0 {
|
|
|
|
buf, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(buf), nil
|
2017-12-21 20:24:03 +00:00
|
|
|
}
|
2018-03-22 15:14:04 +00:00
|
|
|
return "", fmt.Errorf("invalid filename: %s", filename)
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func (p *Provider) loadFileConfig(filename string, parseTemplate bool) (*config.Configuration, error) {
|
2018-03-22 15:14:04 +00:00
|
|
|
fileContent, err := readFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading configuration file: %s - %s", filename, err)
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
2018-05-22 10:02:03 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
var configuration *config.Configuration
|
2018-05-22 10:02:03 +00:00
|
|
|
if parseTemplate {
|
|
|
|
configuration, err = p.CreateConfiguration(fileContent, template.FuncMap{}, false)
|
|
|
|
} else {
|
|
|
|
configuration, err = p.DecodeConfiguration(fileContent)
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:14:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
if configuration == nil || configuration.Routers == nil && configuration.Middlewares == nil && configuration.Services == nil && configuration.TLS == nil {
|
|
|
|
configuration = &config.Configuration{
|
|
|
|
Routers: make(map[string]*config.Router),
|
|
|
|
Middlewares: make(map[string]*config.Middleware),
|
|
|
|
Services: make(map[string]*config.Service),
|
2018-03-22 15:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return configuration, err
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func (p *Provider) loadFileConfigFromDirectory(ctx context.Context, directory string, configuration *config.Configuration) (*config.Configuration, error) {
|
|
|
|
logger := log.FromContext(ctx)
|
|
|
|
|
2017-05-26 13:32:03 +00:00
|
|
|
fileList, err := ioutil.ReadDir(directory)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-11-09 11:16:03 +00:00
|
|
|
return configuration, fmt.Errorf("unable to read directory %s: %v", directory, err)
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 11:16:03 +00:00
|
|
|
if configuration == nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
configuration = &config.Configuration{
|
|
|
|
Routers: make(map[string]*config.Router),
|
|
|
|
Middlewares: make(map[string]*config.Middleware),
|
|
|
|
Services: make(map[string]*config.Service),
|
2017-11-09 11:16:03 +00:00
|
|
|
}
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
2017-05-26 13:32:03 +00:00
|
|
|
|
2017-11-09 11:16:03 +00:00
|
|
|
configTLSMaps := make(map[*tls.Configuration]struct{})
|
|
|
|
for _, item := range fileList {
|
|
|
|
|
|
|
|
if item.IsDir() {
|
2018-11-14 09:18:03 +00:00
|
|
|
configuration, err = p.loadFileConfigFromDirectory(ctx, filepath.Join(directory, item.Name()), configuration)
|
2017-11-09 11:16:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return configuration, fmt.Errorf("unable to load content configuration from subdirectory %s: %v", item, err)
|
|
|
|
}
|
|
|
|
continue
|
2018-03-22 15:14:04 +00:00
|
|
|
} else if !strings.HasSuffix(item.Name(), ".toml") && !strings.HasSuffix(item.Name(), ".tmpl") {
|
2017-05-26 13:32:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
var c *config.Configuration
|
2018-05-22 10:02:03 +00:00
|
|
|
c, err = p.loadFileConfig(path.Join(directory, item.Name()), true)
|
2017-05-26 13:32:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2017-11-09 11:16:03 +00:00
|
|
|
return configuration, err
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
for name, conf := range c.Routers {
|
|
|
|
if _, exists := configuration.Routers[name]; exists {
|
|
|
|
logger.WithField(log.RouterName, name).Warn("Router already configured, skipping")
|
2017-05-26 13:32:03 +00:00
|
|
|
} else {
|
2018-11-14 09:18:03 +00:00
|
|
|
configuration.Routers[name] = conf
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
for name, conf := range c.Middlewares {
|
|
|
|
if _, exists := configuration.Middlewares[name]; exists {
|
|
|
|
logger.WithField(log.MiddlewareName, name).Warn("Middleware already configured, skipping")
|
2017-05-26 13:32:03 +00:00
|
|
|
} else {
|
2018-11-14 09:18:03 +00:00
|
|
|
configuration.Middlewares[name] = conf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, conf := range c.Services {
|
|
|
|
if _, exists := configuration.Services[name]; exists {
|
|
|
|
logger.WithField(log.ServiceName, name).Warn("Service already configured, skipping")
|
|
|
|
} else {
|
|
|
|
configuration.Services[name] = conf
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 15:30:07 +00:00
|
|
|
for _, conf := range c.TLS {
|
2017-11-09 11:16:03 +00:00
|
|
|
if _, exists := configTLSMaps[conf]; exists {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Warnf("TLS Configuration %v already configured, skipping", conf)
|
2017-11-09 11:16:03 +00:00
|
|
|
} else {
|
|
|
|
configTLSMaps[conf] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2017-11-09 11:16:03 +00:00
|
|
|
for conf := range configTLSMaps {
|
2018-01-23 15:30:07 +00:00
|
|
|
configuration.TLS = append(configuration.TLS, conf)
|
2017-11-09 11:16:03 +00:00
|
|
|
}
|
2017-05-26 13:32:03 +00:00
|
|
|
return configuration, nil
|
|
|
|
}
|