From 21ed968663ede2847682b66dade5fa7243d41a79 Mon Sep 17 00:00:00 2001 From: shaoyue Date: Tue, 7 Nov 2023 19:44:08 +0800 Subject: [PATCH] Fix volumeMounts merge (#36) Signed-off-by: shaoyue.chen --- pkg/controllers/components.go | 12 +++++++++++- pkg/controllers/components_test.go | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pkg/controllers/components.go b/pkg/controllers/components.go index 4087fb11..de69b697 100644 --- a/pkg/controllers/components.go +++ b/pkg/controllers/components.go @@ -582,7 +582,17 @@ func MergeComponentSpec(src, dst ComponentSpec) ComponentSpec { } if src.VolumeMounts != nil { - dst.VolumeMounts = src.VolumeMounts + if dst.VolumeMounts == nil { + dst.VolumeMounts = []corev1.VolumeMount{} + } + for _, srcVolumeMount := range src.VolumeMounts { + idx := GetVolumeMountIndex(dst.VolumeMounts, srcVolumeMount.SubPath) + if idx < 0 { + dst.VolumeMounts = append(dst.VolumeMounts, srcVolumeMount) + } else { + dst.VolumeMounts[idx] = srcVolumeMount + } + } } if src.Volumes != nil { diff --git a/pkg/controllers/components_test.go b/pkg/controllers/components_test.go index eea1fb73..0505ded7 100644 --- a/pkg/controllers/components_test.go +++ b/pkg/controllers/components_test.go @@ -222,6 +222,28 @@ func TestMergeComponentSpec(t *testing.T) { merged = MergeComponentSpec(src, dst).PriorityClassName assert.Equal(t, "b", merged) }) + + t.Run("merge volumeMounts", func(t *testing.T) { + dst.VolumeMounts = []corev1.VolumeMount{ + {SubPath: "a.yaml"}, + } + merged := MergeComponentSpec(src, dst).VolumeMounts + assert.Equal(t, 1, len(merged)) + assert.Equal(t, "a.yaml", merged[0].SubPath) + + src.VolumeMounts = []corev1.VolumeMount{ + {SubPath: "b.yaml"}, + } + merged = MergeComponentSpec(src, dst).VolumeMounts + assert.Equal(t, 2, len(merged)) + + src.VolumeMounts = []corev1.VolumeMount{ + {SubPath: "a.yaml", Name: "c"}, + } + merged = MergeComponentSpec(src, dst).VolumeMounts + assert.Equal(t, 2, len(merged)) + assert.Equal(t, "c", merged[1].Name) + }) } func TestMilvusComponent_GetReplicas(t *testing.T) {