internal/config: fix on config change

Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
Bobby DeSimone 2019-10-02 22:48:41 -07:00
parent 83a30d80a5
commit f73d3a09ac
No known key found for this signature in database
GPG key ID: AEE4CF12FE86D07E
2 changed files with 10 additions and 5 deletions

View file

@ -9,7 +9,6 @@ import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/spf13/viper"
"google.golang.org/grpc" "google.golang.org/grpc"
"github.com/pomerium/pomerium/authenticate" "github.com/pomerium/pomerium/authenticate"
@ -77,9 +76,7 @@ func run() error {
defer proxy.AuthorizeClient.Close() defer proxy.AuthorizeClient.Close()
} }
go viper.WatchConfig() opt.OnConfigChange(func(e fsnotify.Event) {
viper.OnConfigChange(func(e fsnotify.Event) {
log.Info().Str("file", e.Name).Msg("cmd/pomerium: config file changed") log.Info().Str("file", e.Name).Msg("cmd/pomerium: config file changed")
opt = config.HandleConfigUpdate(*configFile, opt, []config.OptionsUpdater{authz, proxy}) 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) { if !config.IsAuthorize(opt.Services) {
return nil, nil return nil, nil
} }
log.Info().Interface("opts", opt).Msg("newAuthorizeService")
service, err := authorize.New(opt) service, err := authorize.New(opt)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -11,6 +11,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/fsnotify/fsnotify"
"github.com/pomerium/pomerium/internal/cryptutil" "github.com/pomerium/pomerium/internal/cryptutil"
"github.com/pomerium/pomerium/internal/log" "github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/internal/telemetry/metrics" "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") log.Warn().Err(err).Msg("internal/config: could not parse config checksum into decimal")
} }
metrics.SetConfigChecksum(o.Services, checksumDec) metrics.SetConfigChecksum(o.Services, checksumDec)
return o, nil return o, nil
} }
@ -281,6 +283,13 @@ func (o *Options) parsePolicy() error {
return nil 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 { func (o *Options) viperUnmarshalKey(key string, rawVal interface{}) error {
return o.viper.UnmarshalKey(key, &rawVal) return o.viper.UnmarshalKey(key, &rawVal)
} }