Allow file provider to load config from files in a directory.
This commit is contained in:
parent
73e10c96cc
commit
4128c1ac8d
8 changed files with 442 additions and 45 deletions
|
@ -1,8 +1,9 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
|
@ -18,68 +19,138 @@ var _ provider.Provider = (*Provider)(nil)
|
|||
// Provider holds configurations of the provider.
|
||||
type Provider struct {
|
||||
provider.BaseProvider `mapstructure:",squash"`
|
||||
Directory string `description:"Load configuration from one or more .toml files in a directory"`
|
||||
}
|
||||
|
||||
// Provide allows the file provider to provide configurations to traefik
|
||||
// using the given configuration channel.
|
||||
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Error("Error creating file watcher", err)
|
||||
return err
|
||||
}
|
||||
configuration, err := p.loadConfig()
|
||||
|
||||
file, err := os.Open(p.Filename)
|
||||
if err != nil {
|
||||
log.Error("Error opening file", err)
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if p.Watch {
|
||||
// Process events
|
||||
pool.Go(func(stop chan bool) {
|
||||
defer watcher.Close()
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
case event := <-watcher.Events:
|
||||
if strings.Contains(event.Name, file.Name()) {
|
||||
log.Debug("Provider event:", event)
|
||||
configuration := p.loadFileConfig(file.Name())
|
||||
if configuration != nil {
|
||||
configurationChan <- types.ConfigMessage{
|
||||
ProviderName: "file",
|
||||
Configuration: configuration,
|
||||
}
|
||||
}
|
||||
}
|
||||
case error := <-watcher.Errors:
|
||||
log.Error("Watcher event error", error)
|
||||
}
|
||||
}
|
||||
})
|
||||
err = watcher.Add(filepath.Dir(file.Name()))
|
||||
if err != nil {
|
||||
log.Error("Error adding file watcher", err)
|
||||
var watchItem string
|
||||
|
||||
if p.Directory != "" {
|
||||
watchItem = p.Directory
|
||||
} else {
|
||||
watchItem = p.Filename
|
||||
}
|
||||
|
||||
if err := p.addWatcher(pool, watchItem, configurationChan, p.watcherCallback); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
configuration := p.loadFileConfig(file.Name())
|
||||
sendConfigToChannel(configurationChan, configuration)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Provider) addWatcher(pool *safe.Pool, directory string, configurationChan chan<- types.ConfigMessage, callback func(chan<- types.ConfigMessage, fsnotify.Event)) error {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating file watcher: %s", err)
|
||||
}
|
||||
|
||||
// Process events
|
||||
pool.Go(func(stop chan bool) {
|
||||
defer watcher.Close()
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
case evt := <-watcher.Events:
|
||||
callback(configurationChan, evt)
|
||||
case err := <-watcher.Errors:
|
||||
log.Errorf("Watcher event error: %s", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
err = watcher.Add(directory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error adding file watcher: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendConfigToChannel(configurationChan chan<- types.ConfigMessage, configuration *types.Configuration) {
|
||||
configurationChan <- types.ConfigMessage{
|
||||
ProviderName: "file",
|
||||
Configuration: configuration,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Provider) loadFileConfig(filename string) *types.Configuration {
|
||||
func loadFileConfig(filename string) (*types.Configuration, error) {
|
||||
configuration := new(types.Configuration)
|
||||
if _, err := toml.DecodeFile(filename, configuration); err != nil {
|
||||
log.Error("Error reading file:", err)
|
||||
return nil
|
||||
return nil, fmt.Errorf("error reading configuration file: %s", err)
|
||||
}
|
||||
return configuration
|
||||
return configuration, nil
|
||||
}
|
||||
|
||||
func loadFileConfigFromDirectory(directory string) (*types.Configuration, error) {
|
||||
fileList, err := ioutil.ReadDir(directory)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to read directory %s: %v", directory, err)
|
||||
}
|
||||
|
||||
configuration := &types.Configuration{
|
||||
Frontends: make(map[string]*types.Frontend),
|
||||
Backends: make(map[string]*types.Backend),
|
||||
}
|
||||
|
||||
for _, file := range fileList {
|
||||
if !strings.HasSuffix(file.Name(), ".toml") {
|
||||
continue
|
||||
}
|
||||
|
||||
var c *types.Configuration
|
||||
c, err = loadFileConfig(path.Join(directory, file.Name()))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for backendName, backend := range c.Backends {
|
||||
if _, exists := configuration.Backends[backendName]; exists {
|
||||
log.Warnf("Backend %s already configured, skipping", backendName)
|
||||
} else {
|
||||
configuration.Backends[backendName] = backend
|
||||
}
|
||||
}
|
||||
|
||||
for frontendName, frontend := range c.Frontends {
|
||||
if _, exists := configuration.Frontends[frontendName]; exists {
|
||||
log.Warnf("Frontend %s already configured, skipping", frontendName)
|
||||
} else {
|
||||
configuration.Frontends[frontendName] = frontend
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return configuration, nil
|
||||
}
|
||||
|
||||
func (p *Provider) watcherCallback(configurationChan chan<- types.ConfigMessage, event fsnotify.Event) {
|
||||
configuration, err := p.loadConfig()
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("Error occurred during watcher callback: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
sendConfigToChannel(configurationChan, configuration)
|
||||
}
|
||||
|
||||
func (p *Provider) loadConfig() (*types.Configuration, error) {
|
||||
if p.Directory != "" {
|
||||
return loadFileConfigFromDirectory(p.Directory)
|
||||
}
|
||||
|
||||
return loadFileConfig(p.Filename)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue