From 4be02a4066b32c53263adccc6fc537dd8d1ebc1b Mon Sep 17 00:00:00 2001 From: shaoyue Date: Tue, 21 Nov 2023 15:12:16 +0800 Subject: [PATCH] Fix config tool merge panic (#49) Signed-off-by: shaoyue.chen --- Makefile | 2 +- pkg/util/util.go | 4 ++++ pkg/util/util_test.go | 16 ++++++++++++++++ tool/merge/main.go | 3 +++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9752aec6..6c7ae9c8 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ IMG ?= milvusdb/milvus-operator:dev-latest TOOL_IMG ?= milvus-config-tool:dev-latest SIT_IMG ?= milvus-operator:sit VERSION ?= 0.8.5 -TOOL_VERSION ?= 0.1.1 +TOOL_VERSION ?= 0.2.0 MILVUS_HELM_VERSION ?= milvus-4.1.8 RELEASE_IMG ?= milvusdb/milvus-operator:v$(VERSION) TOOL_RELEASE_IMG ?= milvusdb/milvus-config-tool:v$(TOOL_VERSION) diff --git a/pkg/util/util.go b/pkg/util/util.go index 0951a433..ccb01f53 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -52,7 +52,11 @@ func SetStringSlice(values map[string]interface{}, v []string, fields ...string) unstructured.SetNestedStringSlice(values, v, fields...) } +// MergeValues merges patch into origin, you have to make sure origin is not nil, otherwise it won't work func MergeValues(origin, patch map[string]interface{}) { + if origin == nil || patch == nil { + return + } for patchK, patchV := range patch { if _, exist := origin[patchK]; !exist { origin[patchK] = patchV diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 43136430..b67b6447 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -123,6 +123,22 @@ var mergeTests = []struct { }, }, }, + { + def: nil, + overrides: map[string]interface{}{ + "foo": "bar", + }, + expected: nil, + }, + { + def: map[string]interface{}{ + "foo": "bar", + }, + overrides: nil, + expected: map[string]interface{}{ + "foo": "bar", + }, + }, } func TestMergeValues(t *testing.T) { diff --git a/tool/merge/main.go b/tool/merge/main.go index 0b275a2a..b234a7a0 100644 --- a/tool/merge/main.go +++ b/tool/merge/main.go @@ -51,6 +51,9 @@ func main() { if err != nil { log.Fatal("read destination yaml failed: ", err) } + if dst == nil { + dst = map[string]interface{}{} + } util.MergeValues(dst, src) // backward compatibility