From b9cb8fa591fa15cdef192dc85c4c04879fbf462e Mon Sep 17 00:00:00 2001 From: wyike Date: Fri, 2 Sep 2022 17:45:09 +0800 Subject: [PATCH] fix enable addon cannot update definition bug (#4684) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 楚岳 Signed-off-by: 楚岳 --- pkg/addon/addon_suite_test.go | 3 +++ pkg/addon/utils.go | 11 ++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/addon/addon_suite_test.go b/pkg/addon/addon_suite_test.go index 591f4629a..25c35bc40 100644 --- a/pkg/addon/addon_suite_test.go +++ b/pkg/addon/addon_suite_test.go @@ -423,9 +423,12 @@ var _ = Describe("test override defs of addon", func() { u := unstructured.Unstructured{Object: compUnstructured} u.SetAPIVersion(v1beta1.SchemeGroupVersion.String()) u.SetKind(v1beta1.ComponentDefinitionKind) + u.SetLabels(map[string]string{"testUpdateLabel": "test"}) c, err := checkConflictDefs(ctx, k8sClient, []*unstructured.Unstructured{&u}, app.GetName()) Expect(err).Should(BeNil()) Expect(len(c)).Should(BeEquivalentTo(1)) + // guarantee checkConflictDefs won't change source definition + Expect(u.GetLabels()["testUpdateLabel"]).Should(BeEquivalentTo("test")) u.SetName("rollout") c, err = checkConflictDefs(ctx, k8sClient, []*unstructured.Unstructured{&u}, app.GetName()) diff --git a/pkg/addon/utils.go b/pkg/addon/utils.go index f8344f38e..7cdd5d134 100644 --- a/pkg/addon/utils.go +++ b/pkg/addon/utils.go @@ -444,20 +444,21 @@ func generateAnnotation(meta *Meta) map[string]string { func checkConflictDefs(ctx context.Context, k8sClient client.Client, defs []*unstructured.Unstructured, appName string) (map[string]string, error) { res := map[string]string{} for _, def := range defs { - err := k8sClient.Get(ctx, client.ObjectKeyFromObject(def), def) + checkDef := def.DeepCopy() + err := k8sClient.Get(ctx, client.ObjectKeyFromObject(checkDef), checkDef) if err == nil { - owner := metav1.GetControllerOf(def) + owner := metav1.GetControllerOf(checkDef) if owner == nil || owner.Kind != v1beta1.ApplicationKind { - res[def.GetName()] = fmt.Sprintf("definition: %s already exist and not belong to any addon \n", def.GetName()) + res[checkDef.GetName()] = fmt.Sprintf("definition: %s already exist and not belong to any addon \n", checkDef.GetName()) continue } if owner.Name != appName { // if addon not belong to an addon or addon name is another one, we should put them in result - res[def.GetName()] = fmt.Sprintf("definition: %s in this addon already exist in %s \n", def.GetName(), addon.AppName2Addon(appName)) + res[checkDef.GetName()] = fmt.Sprintf("definition: %s in this addon already exist in %s \n", checkDef.GetName(), addon.AppName2Addon(appName)) } } if err != nil && !errors2.IsNotFound(err) { - return nil, errors.Wrapf(err, "check definition %s", def.GetName()) + return nil, errors.Wrapf(err, "check definition %s", checkDef.GetName()) } } return res, nil