2020-04-13 17:22:00 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-11-18 16:18:08 +00:00
|
|
|
"io"
|
2020-04-13 17:22:00 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/watcher"
|
2020-11-25 06:55:13 +00:00
|
|
|
|
2020-11-10 18:27:52 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-04-13 17:22:00 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Notifier sends out config updates
|
|
|
|
type Notifier interface {
|
|
|
|
ConfigDidUpdate(Root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manager is the base functions of the config manager
|
|
|
|
type Manager interface {
|
|
|
|
Start(Notifier) error
|
|
|
|
Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileManager watches the yaml config for changes
|
|
|
|
// sends updates to the service to reconfigure to match the updated config
|
|
|
|
type FileManager struct {
|
|
|
|
watcher watcher.Notifier
|
|
|
|
notifier Notifier
|
|
|
|
configPath string
|
2020-11-25 06:55:13 +00:00
|
|
|
log *zerolog.Logger
|
|
|
|
ReadConfig func(string, *zerolog.Logger) (Root, error)
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileManager creates a config manager
|
2020-11-25 06:55:13 +00:00
|
|
|
func NewFileManager(watcher watcher.Notifier, configPath string, log *zerolog.Logger) (*FileManager, error) {
|
2020-04-13 17:22:00 +00:00
|
|
|
m := &FileManager{
|
|
|
|
watcher: watcher,
|
|
|
|
configPath: configPath,
|
2020-11-25 06:55:13 +00:00
|
|
|
log: log,
|
2020-05-01 15:30:50 +00:00
|
|
|
ReadConfig: readConfigFromPath,
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
err := watcher.Add(configPath)
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the runloop to watch for config changes
|
|
|
|
func (m *FileManager) Start(notifier Notifier) error {
|
|
|
|
m.notifier = notifier
|
|
|
|
|
|
|
|
// update the notifier with a fresh config on start
|
|
|
|
config, err := m.GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
notifier.ConfigDidUpdate(config)
|
|
|
|
|
|
|
|
m.watcher.Start(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig reads the yaml file from the disk
|
|
|
|
func (m *FileManager) GetConfig() (Root, error) {
|
2020-11-25 06:55:13 +00:00
|
|
|
return m.ReadConfig(m.configPath, m.log)
|
2020-05-01 15:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the watcher
|
|
|
|
func (m *FileManager) Shutdown() {
|
|
|
|
m.watcher.Shutdown()
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
func readConfigFromPath(configPath string, log *zerolog.Logger) (Root, error) {
|
2020-05-01 15:30:50 +00:00
|
|
|
if configPath == "" {
|
2020-04-13 17:22:00 +00:00
|
|
|
return Root{}, errors.New("unable to find config file")
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:30:50 +00:00
|
|
|
file, err := os.Open(configPath)
|
2020-04-13 17:22:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return Root{}, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var config Root
|
|
|
|
if err := yaml.NewDecoder(file).Decode(&config); err != nil {
|
2020-11-18 16:18:08 +00:00
|
|
|
if err == io.EOF {
|
2020-11-25 06:55:13 +00:00
|
|
|
log.Error().Msgf("Configuration file %s was empty", configPath)
|
2020-11-18 16:18:08 +00:00
|
|
|
return Root{}, nil
|
|
|
|
}
|
2020-11-10 18:27:52 +00:00
|
|
|
return Root{}, errors.Wrap(err, "error parsing YAML in config file at "+configPath)
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// File change notifications from the watcher
|
|
|
|
|
|
|
|
// WatcherItemDidChange triggers when the yaml config is updated
|
|
|
|
// sends the updated config to the service to reload its state
|
|
|
|
func (m *FileManager) WatcherItemDidChange(filepath string) {
|
|
|
|
config, err := m.GetConfig()
|
|
|
|
if err != nil {
|
2020-11-25 06:55:13 +00:00
|
|
|
m.log.Error().Msgf("Failed to read new config: %s", err)
|
2020-04-13 17:22:00 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
m.log.Info().Msg("Config file has been updated")
|
2020-04-13 17:22:00 +00:00
|
|
|
m.notifier.ConfigDidUpdate(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatcherDidError notifies of errors with the file watcher
|
|
|
|
func (m *FileManager) WatcherDidError(err error) {
|
2020-11-25 06:55:13 +00:00
|
|
|
m.log.Error().Msgf("Config watcher encountered an error: %s", err)
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|