From 707905d877c71a87e21fea137c7c1778ebaafcd2 Mon Sep 17 00:00:00 2001 From: Jianbo Sun Date: Thu, 14 Apr 2022 19:32:53 +0800 Subject: [PATCH] Fix: add label from inner system in CR can prevent sync (#3655) Signed-off-by: Jianbo Sun --- pkg/apiserver/sync/cache.go | 11 +++++++++-- pkg/apiserver/sync/cache_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg/apiserver/sync/cache.go b/pkg/apiserver/sync/cache.go index 3d1e874d5..c6bd7e4f5 100644 --- a/pkg/apiserver/sync/cache.go +++ b/pkg/apiserver/sync/cache.go @@ -66,6 +66,11 @@ func (c *CR2UX) initCache(ctx context.Context) error { } func (c *CR2UX) shouldSync(ctx context.Context, targetApp *v1beta1.Application, del bool) bool { + + if targetApp != nil && targetApp.Labels != nil && targetApp.Labels[model.LabelSourceOfTruth] == model.FromInner { + return false + } + key := formatAppComposedName(targetApp.Name, targetApp.Namespace) cachedData, ok := c.cache.Load(key) if ok { @@ -85,9 +90,11 @@ func (c *CR2UX) shouldSync(ctx context.Context, targetApp *v1beta1.Application, // This is a double check to make sure the app not be converted and un-deployed sot := c.CheckSoTFromAppMeta(ctx, targetApp.Name, targetApp.Namespace, CheckSoTFromCR(targetApp)) - switch sot { - case model.FromUX, model.FromInner: + case model.FromUX: + // we don't sync if the application is not created from CR + return false + case model.FromInner: // we don't sync if the application is not created from CR return false case model.FromCR: diff --git a/pkg/apiserver/sync/cache_test.go b/pkg/apiserver/sync/cache_test.go index 90476dc12..9e1148b07 100644 --- a/pkg/apiserver/sync/cache_test.go +++ b/pkg/apiserver/sync/cache_test.go @@ -25,6 +25,7 @@ import ( corev1 "k8s.io/api/core/v1" + "github.com/oam-dev/kubevela/apis/core.oam.dev/common" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" "github.com/oam-dev/kubevela/pkg/apiserver/datastore" "github.com/oam-dev/kubevela/pkg/apiserver/model" @@ -95,5 +96,28 @@ var _ = Describe("Test Cache", func() { Expect(cr2ux.shouldSync(ctx, app1, false)).Should(BeEquivalentTo(true)) }) + It("Test don't cache with from inner system label", func() { + dbNamespace := "cache-db-ns2-test" + ds, err := NewDatastore(datastore.Config{Type: "kubeapi", Database: dbNamespace}) + Expect(ds).ToNot(BeNil()) + Expect(err).Should(BeNil()) + var ns = corev1.Namespace{} + ns.Name = dbNamespace + err = k8sClient.Create(context.TODO(), &ns) + Expect(err).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) + + cr2ux := CR2UX{ds: ds, cli: k8sClient, cache: sync.Map{}} + ctx := context.Background() + + app1 := &v1beta1.Application{} + app1.Name = "app1" + app1.Namespace = dbNamespace + app1.Generation = 1 + app1.Spec.Components = []common.ApplicationComponent{} + app1.Labels = make(map[string]string) + app1.Labels[model.LabelSourceOfTruth] = model.FromInner + Expect(k8sClient.Create(ctx, app1)).Should(BeNil()) + Expect(cr2ux.shouldSync(ctx, app1, false)).Should(BeEquivalentTo(false)) + }) })