From 0a28c28a8c5e7277384077bd593926422e7c97de Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 28 Jan 2019 13:31:35 -0800 Subject: [PATCH] Avoid code which looks at local files when etcd is configured (#7144) This situation happens only in gateway nas which supports etcd based `config.json` to support all FS mode features. The issue was we would try to migrate something which doesn't exist when etcd is configured which leads to inconsistent server configs in memory. This PR fixes this situation by properly loading config after initialization, avoiding backend disk config migration to be done only if etcd is not configured. --- cmd/config-common.go | 18 ++++++++---------- cmd/config-current.go | 20 ++++++++------------ cmd/config.go | 18 ++++++++++-------- cmd/gateway-main.go | 6 +++--- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/cmd/config-common.go b/cmd/config-common.go index 397d9b116..cf597cd3d 100644 --- a/cmd/config-common.go +++ b/cmd/config-common.go @@ -103,16 +103,14 @@ func readConfigEtcd(ctx context.Context, client *etcd.Client, configFile string) return nil, errConfigNotFound } -// watchConfig - watches for changes on `configFile` on etcd and loads them. -func watchConfig(objAPI ObjectLayer, configFile string, loadCfgFn func(ObjectLayer) error) { - if globalEtcdClient != nil { - ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout) - defer cancel() - for watchResp := range globalEtcdClient.Watch(ctx, configFile) { - for _, event := range watchResp.Events { - if event.IsModify() || event.IsCreate() { - loadCfgFn(objAPI) - } +// watchConfigEtcd - watches for changes on `configFile` on etcd and loads them. +func watchConfigEtcd(objAPI ObjectLayer, configFile string, loadCfgFn func(ObjectLayer) error) { + ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout) + defer cancel() + for watchResp := range globalEtcdClient.Watch(ctx, configFile) { + for _, event := range watchResp.Events { + if event.IsModify() || event.IsCreate() { + loadCfgFn(objAPI) } } } diff --git a/cmd/config-current.go b/cmd/config-current.go index 8da7a28b5..89172b0e8 100644 --- a/cmd/config-current.go +++ b/cmd/config-current.go @@ -545,19 +545,15 @@ func (s *serverConfig) loadToCachedConfigs() { globalIsCompressionEnabled = compressionConf.Enabled } - if globalIAMValidators == nil { - globalIAMValidators = getAuthValidators(s) - } + globalIAMValidators = getAuthValidators(s) - if globalPolicyOPA == nil { - if s.Policy.OPA.URL != nil && s.Policy.OPA.URL.String() != "" { - globalPolicyOPA = iampolicy.NewOpa(iampolicy.OpaArgs{ - URL: s.Policy.OPA.URL, - AuthToken: s.Policy.OPA.AuthToken, - Transport: NewCustomHTTPTransport(), - CloseRespFn: CloseResponse, - }) - } + if s.Policy.OPA.URL != nil && s.Policy.OPA.URL.String() != "" { + globalPolicyOPA = iampolicy.NewOpa(iampolicy.OpaArgs{ + URL: s.Policy.OPA.URL, + AuthToken: s.Policy.OPA.AuthToken, + Transport: NewCustomHTTPTransport(), + CloseRespFn: CloseResponse, + }) } } diff --git a/cmd/config.go b/cmd/config.go index a6d43c63a..bfc92c0ca 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -157,6 +157,8 @@ func initConfig(objAPI ObjectLayer) error { return errServerNotInitialized } + configFile := path.Join(minioConfigPrefix, minioConfigFile) + if globalEtcdClient != nil { if err := checkConfigEtcd(context.Background(), globalEtcdClient, getConfigFile()); err != nil { if err == errConfigNotFound { @@ -172,6 +174,10 @@ func initConfig(objAPI ObjectLayer) error { return err } } + + // Watch config for changes and reloads them. + go watchConfigEtcd(objAPI, configFile, loadConfig) + } else { if isFile(getConfigFile()) { if err := migrateConfig(); err != nil { @@ -184,15 +190,11 @@ func initConfig(objAPI ObjectLayer) error { if err := migrateConfigToMinioSys(objAPI); err != nil { return err } - } - configFile := path.Join(minioConfigPrefix, minioConfigFile) - - // Watch config for changes and reloads them in-memory. - go watchConfig(objAPI, configFile, loadConfig) - - if err := migrateMinioSysConfig(objAPI); err != nil { - return err + // Migrates backend '/.minio.sys/config/config.json' to latest version. + if err := migrateMinioSysConfig(objAPI); err != nil { + return err + } } return loadConfig(objAPI) diff --git a/cmd/gateway-main.go b/cmd/gateway-main.go index 43cb15f0a..860e96920 100644 --- a/cmd/gateway-main.go +++ b/cmd/gateway-main.go @@ -223,7 +223,7 @@ func StartGateway(ctx *cli.Context, gw Gateway) { globalConfigSys = NewConfigSys() // Load globalServerConfig from etcd - _ = globalConfigSys.Init(newObject) + logger.LogIf(context.Background(), globalConfigSys.Init(newObject)) } // Load logger subsystem @@ -247,7 +247,7 @@ func StartGateway(ctx *cli.Context, gw Gateway) { globalIAMSys = NewIAMSys() if enableIAMOps { // Initialize IAM sys. - _ = globalIAMSys.Init(newObject) + logger.LogIf(context.Background(), globalIAMSys.Init(newObject)) } // Create new policy system. @@ -259,7 +259,7 @@ func StartGateway(ctx *cli.Context, gw Gateway) { // Create new notification system. globalNotificationSys = NewNotificationSys(globalServerConfig, globalEndpoints) if globalEtcdClient != nil && newObject.IsNotificationSupported() { - _ = globalNotificationSys.Init(newObject) + logger.LogIf(context.Background(), globalNotificationSys.Init(newObject)) } // Encryption support checks in gateway mode.