[MM-30436] Add support for configuration custom defaults (#16265)

* [MM-30436] Add support for configuration custom defaults

* Addressing review comments

* Addressing PR comments

* Soft fail if the configuration defaults cannot be read

* Directly print error in log message

* Fix linter

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Miguel de la Cruz
2020-11-16 15:25:48 +01:00
коммит произвёл GitHub
родитель c82c37a36e
Коммит 6c857a4525
18 изменённых файлов: 334 добавлений и 131 удалений

Просмотреть файл

@@ -48,19 +48,19 @@ type BackingStore interface {
}
// NewStore creates a database or file store given a data source name by which to connect.
func NewStore(dsn string, watch bool) (*Store, error) {
func NewStore(dsn string, watch bool, customDefaults *model.Config) (*Store, error) {
backingStore, err := getBackingStore(dsn, watch)
if err != nil {
return nil, err
}
return NewStoreFromBacking(backingStore)
return NewStoreFromBacking(backingStore, customDefaults)
}
func NewStoreFromBacking(backingStore BackingStore) (*Store, error) {
func NewStoreFromBacking(backingStore BackingStore, customDefaults *model.Config) (*Store, error) {
store := &Store{
backingStore: backingStore,
backingStore: backingStore,
configCustomDefaults: customDefaults,
}
if err := store.Load(); err != nil {
@@ -90,7 +90,7 @@ func NewTestMemoryStore() *Store {
panic("failed to initialize memory store: " + err.Error())
}
configStore, err := NewStoreFromBacking(memoryStore)
configStore, err := NewStoreFromBacking(memoryStore, nil)
if err != nil {
panic("failed to initialize config store: " + err.Error())
}
@@ -102,9 +102,10 @@ type Store struct {
emitter
backingStore BackingStore
configLock sync.RWMutex
config *model.Config
configNoEnv *model.Config
configLock sync.RWMutex
config *model.Config
configNoEnv *model.Config
configCustomDefaults *model.Config
persistFeatureFlags bool
}
@@ -195,6 +196,18 @@ func (s *Store) loadLockedWithOld(oldCfg *model.Config, unlockOnce *sync.Once) e
}
}
// If we have custom defaults set, the initial config is merged on
// top of them and we delete them not to be used again in the
// configuration reloads
if s.configCustomDefaults != nil {
var mErr error
loadedConfig, mErr = Merge(s.configCustomDefaults, loadedConfig, nil)
if mErr != nil {
return errors.Wrap(mErr, "failed to merge custom config defaults")
}
s.configCustomDefaults = nil
}
loadedConfig.SetDefaults()
s.configNoEnv = loadedConfig.Clone()