Skip to content

Commit

Permalink
Merge branch 'release-2.6' into chore/backport-release-2.6-11923
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Beaumont <[email protected]>
  • Loading branch information
michaelbeaumont committed Oct 31, 2024
2 parents 743cc6c + 62176b0 commit 743965a
Show file tree
Hide file tree
Showing 6 changed files with 57 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
7 changes: 6 additions & 1 deletion pkg/plugins/resources/k8s/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ 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 @@ -179,7 +179,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 @@ -153,7 +153,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 @@ -105,7 +105,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 @@ -174,6 +174,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"))
})

// todo(jakubdyszkiewicz) write tests for optimistic locking
})

Expand Down

0 comments on commit 743965a

Please sign in to comment.