From f73d3a09acfcf98cd353a8bbfd44fec471a35787 Mon Sep 17 00:00:00 2001 From: Bobby DeSimone Date: Wed, 2 Oct 2019 22:48:41 -0700 Subject: [PATCH] internal/config: fix on config change Signed-off-by: Bobby DeSimone --- cmd/pomerium/main.go | 6 +----- internal/config/options.go | 9 +++++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/pomerium/main.go b/cmd/pomerium/main.go index be353c35a..27b666414 100644 --- a/cmd/pomerium/main.go +++ b/cmd/pomerium/main.go @@ -9,7 +9,6 @@ import ( "github.com/fsnotify/fsnotify" "github.com/gorilla/mux" - "github.com/spf13/viper" "google.golang.org/grpc" "github.com/pomerium/pomerium/authenticate" @@ -77,9 +76,7 @@ func run() error { defer proxy.AuthorizeClient.Close() } - go viper.WatchConfig() - - viper.OnConfigChange(func(e fsnotify.Event) { + opt.OnConfigChange(func(e fsnotify.Event) { log.Info().Str("file", e.Name).Msg("cmd/pomerium: config file changed") opt = config.HandleConfigUpdate(*configFile, opt, []config.OptionsUpdater{authz, proxy}) }) @@ -112,7 +109,6 @@ func newAuthorizeService(opt config.Options, wg *sync.WaitGroup) (*authorize.Aut if !config.IsAuthorize(opt.Services) { return nil, nil } - log.Info().Interface("opts", opt).Msg("newAuthorizeService") service, err := authorize.New(opt) if err != nil { return nil, err diff --git a/internal/config/options.go b/internal/config/options.go index 9e7480940..99f2c9c98 100644 --- a/internal/config/options.go +++ b/internal/config/options.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/fsnotify/fsnotify" "github.com/pomerium/pomerium/internal/cryptutil" "github.com/pomerium/pomerium/internal/log" "github.com/pomerium/pomerium/internal/telemetry/metrics" @@ -220,6 +221,7 @@ func NewOptionsFromConfig(configFile string) (*Options, error) { log.Warn().Err(err).Msg("internal/config: could not parse config checksum into decimal") } metrics.SetConfigChecksum(o.Services, checksumDec) + return o, nil } @@ -281,6 +283,13 @@ func (o *Options) parsePolicy() error { return nil } +// OnConfigChange starts a go routine and watches for any changes. If any are +// detected, via an fsnotify event the provided function is run. +func (o *Options) OnConfigChange(run func(in fsnotify.Event)) { + go o.viper.WatchConfig() + o.viper.OnConfigChange(run) +} + func (o *Options) viperUnmarshalKey(key string, rawVal interface{}) error { return o.viper.UnmarshalKey(key, &rawVal) }