From d91e1eff5f118fd59fd7cc7ddb74ed4724d2611e Mon Sep 17 00:00:00 2001 From: "kumahq[bot]" <110050114+kumahq[bot]@users.noreply.github.com> Date: Fri, 8 Nov 2024 13:37:44 +0000 Subject: [PATCH] feat(store): update does not wipe out labels (backport of #10335) (#11954) Needed for https://github.com/kumahq/kuma/pull/11941 Automatic cherry-pick of #10335 for branch release-2.7 Generated by [action](https://github.com/kumahq/kuma/actions/runs/11608845773) Signed-off-by: Jakub Dyszkiewicz Signed-off-by: Mike Beaumont Co-authored-by: Jakub Dyszkiewicz Co-authored-by: Mike Beaumont --- pkg/core/resources/store/options.go | 2 ++ pkg/plugins/resources/k8s/store.go | 6 +++- pkg/plugins/resources/memory/store.go | 4 ++- pkg/plugins/resources/postgres/pgx_store.go | 6 +++- pkg/plugins/resources/postgres/pq_store.go | 6 +++- pkg/test/store/store_test_template.go | 36 +++++++++++++++++++++ 6 files changed, 56 insertions(+), 4 deletions(-) diff --git a/pkg/core/resources/store/options.go b/pkg/core/resources/store/options.go index b54e5f005374..c56aeb96321b 100644 --- a/pkg/core/resources/store/options.go +++ b/pkg/core/resources/store/options.go @@ -57,6 +57,7 @@ func CreateWithLabels(labels map[string]string) CreateOptionsFunc { type UpdateOptions struct { ModificationTime time.Time Labels map[string]string + ModifyLabels bool } func ModifiedAt(modificationTime time.Time) UpdateOptionsFunc { @@ -68,6 +69,7 @@ func ModifiedAt(modificationTime time.Time) UpdateOptionsFunc { func UpdateWithLabels(labels map[string]string) UpdateOptionsFunc { return func(opts *UpdateOptions) { opts.Labels = labels + opts.ModifyLabels = true } } diff --git a/pkg/plugins/resources/k8s/store.go b/pkg/plugins/resources/k8s/store.go index af1d6496957d..9086f04677bb 100644 --- a/pkg/plugins/resources/k8s/store.go +++ b/pkg/plugins/resources/k8s/store.go @@ -99,7 +99,11 @@ func (s *KubernetesStore) Update(ctx context.Context, r core_model.Resource, fs return errors.Wrapf(err, "failed to convert core model of type %s into k8s counterpart", r.Descriptor().Name) } - labels, annotations := splitLabelsAndAnnotations(opts.Labels, obj.GetAnnotations()) + updateLabels := r.GetMeta().GetLabels() + if opts.ModifyLabels { + updateLabels = opts.Labels + } + labels, annotations := splitLabelsAndAnnotations(updateLabels, obj.GetAnnotations()) obj.GetObjectMeta().SetLabels(labels) obj.GetObjectMeta().SetAnnotations(annotations) obj.SetMesh(r.GetMeta().GetMesh()) diff --git a/pkg/plugins/resources/memory/store.go b/pkg/plugins/resources/memory/store.go index 8503dc273f05..92dcb42cac4b 100644 --- a/pkg/plugins/resources/memory/store.go +++ b/pkg/plugins/resources/memory/store.go @@ -184,7 +184,9 @@ func (c *memoryStore) Update(_ context.Context, r core_model.Resource, fs ...sto } meta.Version = meta.Version.Next() meta.ModificationTime = opts.ModificationTime - meta.Labels = opts.Labels + if opts.ModifyLabels { + meta.Labels = opts.Labels + } r.SetMeta(meta) record.Version = meta.Version diff --git a/pkg/plugins/resources/postgres/pgx_store.go b/pkg/plugins/resources/postgres/pgx_store.go index 97ec525eea63..3aaecaaac50b 100644 --- a/pkg/plugins/resources/postgres/pgx_store.go +++ b/pkg/plugins/resources/postgres/pgx_store.go @@ -159,7 +159,11 @@ func (r *pgxResourceStore) Update(ctx context.Context, resource core_model.Resou return errors.Wrap(err, "failed to convert meta version to int") } - labels, err := prepareLabels(opts.Labels) + updateLabels := resource.GetMeta().GetLabels() + if opts.ModifyLabels { + updateLabels = opts.Labels + } + labels, err := prepareLabels(updateLabels) if err != nil { return err } diff --git a/pkg/plugins/resources/postgres/pq_store.go b/pkg/plugins/resources/postgres/pq_store.go index 23159a985b75..ba7d701c69b5 100644 --- a/pkg/plugins/resources/postgres/pq_store.go +++ b/pkg/plugins/resources/postgres/pq_store.go @@ -110,7 +110,11 @@ func (r *postgresResourceStore) Update(_ context.Context, resource core_model.Re return errors.Wrap(err, "failed to convert meta version to int") } - labels, err := prepareLabels(opts.Labels) + updateLabels := resource.GetMeta().GetLabels() + if opts.ModifyLabels { + updateLabels = opts.Labels + } + labels, err := prepareLabels(updateLabels) if err != nil { return err } diff --git a/pkg/test/store/store_test_template.go b/pkg/test/store/store_test_template.go index b9780792e6e8..7b6336cdb5ed 100644 --- a/pkg/test/store/store_test_template.go +++ b/pkg/test/store/store_test_template.go @@ -223,6 +223,42 @@ func ExecuteStoreTests( } }) + It("should preserve labels", func() { + // given + name := "to-be-updated.demo" + resource := createResource(name, "foo", "bar") + + // when + resource.Spec.Conf.Destination["path"] = "new-path" + err := s.Update(context.Background(), resource) + + // then + Expect(err).ToNot(HaveOccurred()) + + res := core_mesh.NewTrafficRouteResource() + err = s.Get(context.Background(), res, store.GetByKey(name, mesh)) + Expect(err).ToNot(HaveOccurred()) + Expect(res.Meta.GetLabels()).To(HaveKeyWithValue("foo", "bar")) + }) + + It("should delete labels", func() { + // given a resources in storage + name := "to-be-updated.demo" + resource := createResource(name, "foo", "bar") + + // when + resource.Spec.Conf.Destination["path"] = "new-path" + err := s.Update(context.Background(), resource, store.UpdateWithLabels(map[string]string{})) + + // then + Expect(err).ToNot(HaveOccurred()) + + res := core_mesh.NewTrafficRouteResource() + err = s.Get(context.Background(), res, store.GetByKey(name, mesh)) + Expect(err).ToNot(HaveOccurred()) + Expect(res.Meta.GetLabels()).ToNot(HaveKeyWithValue("foo", "bar")) + }) + It("should update resource with status", func() { // given updated := meshservice_api.MeshServiceResource{