From 24b9e2c1a0a72a7ad302cdce7da3abbc4e6295cb Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Fri, 23 Aug 2019 15:49:02 -0700 Subject: [PATCH] Merge configs section by section Signed-off-by: Maksym Pavlenko --- services/server/config/config.go | 21 ++++++++++++++++++++- services/server/config/config_test.go | 25 ++++++++++++++----------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/services/server/config/config.go b/services/server/config/config.go index bc9520fea..98fef816e 100644 --- a/services/server/config/config.go +++ b/services/server/config/config.go @@ -297,8 +297,27 @@ func resolveImports(parent string, imports []string) ([]string, error) { // 0 1 1 // []{"1"} []{"2"} []{"1","2"} // []{"1"} []{} []{"1"} +// Maps merged by keys, but values are replaced entirely. func mergeConfig(to, from *Config) error { - return mergo.Merge(to, from, mergo.WithOverride, mergo.WithAppendSlice) + err := mergo.Merge(to, from, mergo.WithOverride, mergo.WithAppendSlice) + if err != nil { + return err + } + + // Replace entire sections instead of merging map's values. + for k, v := range from.Plugins { + to.Plugins[k] = v + } + + for k, v := range from.StreamProcessors { + to.StreamProcessors[k] = v + } + + for k, v := range from.ProxyPlugins { + to.ProxyPlugins[k] = v + } + + return nil } // V1DisabledFilter matches based on ID diff --git a/services/server/config/config_test.go b/services/server/config/config_test.go index 90cba46f2..8660105d4 100644 --- a/services/server/config/config_test.go +++ b/services/server/config/config_test.go @@ -29,20 +29,22 @@ import ( func TestMergeConfigs(t *testing.T) { a := &Config{ - Version: 2, - Root: "old_root", - RequiredPlugins: []string{"old_plugin"}, - DisabledPlugins: []string{"old_plugin"}, - State: "old_state", - OOMScore: 1, - Timeouts: map[string]string{"a": "1"}, + Version: 2, + Root: "old_root", + RequiredPlugins: []string{"old_plugin"}, + DisabledPlugins: []string{"old_plugin"}, + State: "old_state", + OOMScore: 1, + Timeouts: map[string]string{"a": "1"}, + StreamProcessors: map[string]StreamProcessor{"1": {Path: "2", Returns: "4"}, "2": {Path: "5"}}, } b := &Config{ - Root: "new_root", - RequiredPlugins: []string{"new_plugin1", "new_plugin2"}, - OOMScore: 2, - Timeouts: map[string]string{"b": "2"}, + Root: "new_root", + RequiredPlugins: []string{"new_plugin1", "new_plugin2"}, + OOMScore: 2, + Timeouts: map[string]string{"b": "2"}, + StreamProcessors: map[string]StreamProcessor{"1": {Path: "3"}}, } err := mergeConfig(a, b) @@ -55,6 +57,7 @@ func TestMergeConfigs(t *testing.T) { assert.DeepEqual(t, a.RequiredPlugins, []string{"old_plugin", "new_plugin1", "new_plugin2"}) assert.DeepEqual(t, a.DisabledPlugins, []string{"old_plugin"}) assert.DeepEqual(t, a.Timeouts, map[string]string{"a": "1", "b": "2"}) + assert.DeepEqual(t, a.StreamProcessors, map[string]StreamProcessor{"1": {Path: "3"}, "2": {Path: "5"}}) } func TestResolveImports(t *testing.T) {