diff --git a/pkg/addon/addon_suite_test.go b/pkg/addon/addon_suite_test.go index b996ca051..60a293b9b 100644 --- a/pkg/addon/addon_suite_test.go +++ b/pkg/addon/addon_suite_test.go @@ -418,9 +418,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 a8a5ca330..9dea2dd89 100644 --- a/pkg/addon/utils.go +++ b/pkg/addon/utils.go @@ -448,20 +448,21 @@ func isErrorCueRenderPathNotFound(err error, path string) bool { 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