From f8239be21e41c9eb00de033dbcbcfa4416b872a7 Mon Sep 17 00:00:00 2001 From: Somefive Date: Fri, 13 Jan 2023 13:27:32 +0800 Subject: [PATCH] Fix: conflict while using gc policy and shared-resource policy concurrently Signed-off-by: Somefive --- pkg/resourcekeeper/gc.go | 1 + .../multicluster_test.go | 21 +++++++++++++++ .../testdata/app/app-gc-shared.yaml | 26 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 test/e2e-multicluster-test/testdata/app/app-gc-shared.yaml diff --git a/pkg/resourcekeeper/gc.go b/pkg/resourcekeeper/gc.go index 5b8808f85..947dabe67 100644 --- a/pkg/resourcekeeper/gc.go +++ b/pkg/resourcekeeper/gc.go @@ -371,6 +371,7 @@ func DeleteManagedResourceInApplication(ctx context.Context, cli client.Client, } return nil } + util.RemoveAnnotations(obj, []string{oam.AnnotationAppSharedBy}) } if mr.SkipGC || hasOrphanFinalizer(app) { if labels := obj.GetLabels(); labels != nil { diff --git a/test/e2e-multicluster-test/multicluster_test.go b/test/e2e-multicluster-test/multicluster_test.go index 4cefaaa08..c9d83f76a 100644 --- a/test/e2e-multicluster-test/multicluster_test.go +++ b/test/e2e-multicluster-test/multicluster_test.go @@ -912,5 +912,26 @@ var _ = Describe("Test multicluster scenario", func() { g.Expect(cnt).Should(Equal(1)) }).WithTimeout(30 * time.Second).WithPolling(2 * time.Second).Should(Succeed()) }) + + It("Test application with gc policy and shared-resource policy", func() { + app := &v1beta1.Application{} + bs, err := os.ReadFile("./testdata/app/app-gc-shared.yaml") + Expect(err).Should(Succeed()) + Expect(yaml.Unmarshal(bs, app)).Should(Succeed()) + app.SetNamespace(namespace) + Expect(k8sClient.Create(hubCtx, app)).Should(Succeed()) + appKey := client.ObjectKeyFromObject(app) + Eventually(func(g Gomega) { + g.Expect(k8sClient.Get(hubCtx, appKey, app)).Should(Succeed()) + g.Expect(app.Status.Phase).Should(Equal(common.ApplicationRunning)) + g.Expect(k8sClient.Get(hubCtx, appKey, &corev1.ConfigMap{})).Should(Succeed()) + }).WithTimeout(10 * time.Second).Should(Succeed()) + Expect(k8sClient.Get(hubCtx, appKey, app)).Should(Succeed()) + Expect(k8sClient.Delete(hubCtx, app)).Should(Succeed()) + Eventually(func(g Gomega) { + g.Expect(kerrors.IsNotFound(k8sClient.Get(hubCtx, appKey, app))).Should(BeTrue()) + g.Expect(k8sClient.Get(hubCtx, appKey, &corev1.ConfigMap{})).Should(Succeed()) + }).WithTimeout(10 * time.Second).Should(Succeed()) + }) }) }) diff --git a/test/e2e-multicluster-test/testdata/app/app-gc-shared.yaml b/test/e2e-multicluster-test/testdata/app/app-gc-shared.yaml new file mode 100644 index 000000000..700b73c76 --- /dev/null +++ b/test/e2e-multicluster-test/testdata/app/app-gc-shared.yaml @@ -0,0 +1,26 @@ +apiVersion: core.oam.dev/v1beta1 +kind: Application +metadata: + name: app-gc-shared +spec: + components: + - type: k8s-objects + name: app-gc-shared + properties: + objects: + - apiVersion: v1 + kind: ConfigMap + policies: + - name: gc-policy + type: garbage-collect + properties: + rules: + - selector: + resourceTypes: ["ConfigMap"] + strategy: never + - name: shared-policy + type: shared-resource + properties: + rules: + - selector: + resourceTypes: ["ConfigMap"] \ No newline at end of file