Skip to content

Commit

Permalink
feat(store): update does not wipe out labels (backport of #10335) (#1…
Browse files Browse the repository at this point in the history
…1954)

Needed for #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 <[email protected]>
Signed-off-by: Mike Beaumont <[email protected]>
Co-authored-by: Jakub Dyszkiewicz <[email protected]>
Co-authored-by: Mike Beaumont <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent 151c68c commit d91e1ef
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pkg/core/resources/store/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/plugins/resources/k8s/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 3 additions & 1 deletion pkg/plugins/resources/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion pkg/plugins/resources/postgres/pgx_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/plugins/resources/postgres/pq_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/test/store/store_test_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit d91e1ef

Please sign in to comment.