diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 9827ea0297..bcb1141a50 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -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{} } diff --git a/pkg/cache/cache_unit_test.go b/pkg/cache/cache_unit_test.go index c5e2ab7763..059bce97cb 100644 --- a/pkg/cache/cache_unit_test.go +++ b/pkg/cache/cache_unit_test.go @@ -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 {