Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#2249 from joelanford/0-14-preserve…
Browse files Browse the repository at this point in the history
…-unstructured-gvk-cache-options

🐛 release-0.14: Preserve unstructured object GVKs when using *ByObject cache options
  • Loading branch information
k8s-ci-robot committed Mar 27, 2023
2 parents ffb74e5 + 1dcfec4 commit cd65cb2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ func convertToByObject[T any](byGVK map[schema.GroupVersionKind]T, scheme *runti
if !ok {
return nil, def, fmt.Errorf("object %T for GVK %q does not implement client.Object", obj, gvk)
}
cObj.GetObjectKind().SetGroupVersionKind(gvk)
if byObject == nil {
byObject = map[client.Object]T{}
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/cache/cache_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,27 @@ var _ = Describe("cache.inheritFrom", func() {
))
})
})

Context("convertToByObject", func() {
It("embeds the GVK in the returned objects", func() {
gvk := gv.WithKind("Unstructured")
obj := &unstructured.Unstructured{}

sch := runtime.NewScheme()
sch.AddKnownTypeWithName(gvk, obj)

byObj, def, err := convertToByObject(map[schema.GroupVersionKind]struct{}{
gvk: {},
}, sch)

Expect(err).NotTo(HaveOccurred())
Expect(def).To(Equal(struct{}{}))
Expect(byObj).To(HaveLen(1))
for obj := range byObj {
Expect(obj.GetObjectKind().GroupVersionKind()).To(Equal(gvk))
}
})
})
})

func checkError[T any](v T, err error) T {
Expand Down

0 comments on commit cd65cb2

Please sign in to comment.