Skip to content

Commit 25743c7

Browse files
committed
Populate GVK for listed objects
1 parent 5734523 commit 25743c7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pkg/cache/cache_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"k8s.io/apimachinery/pkg/runtime/schema"
3131
kscheme "k8s.io/client-go/kubernetes/scheme"
3232
kcache "k8s.io/client-go/tools/cache"
33-
3433
"sigs.k8s.io/controller-runtime/pkg/cache"
3534
"sigs.k8s.io/controller-runtime/pkg/client"
3635
)
@@ -198,6 +197,19 @@ var _ = Describe("Informer Cache", func() {
198197
Expect(actual.Namespace).To(Equal(testNamespaceOne))
199198
})
200199

200+
It("should be able to list objects with GVK populated", func() {
201+
By("listing pods")
202+
listObj := &kcorev1.PodList{}
203+
Expect(informerCache.List(context.Background(), listObj)).To(Succeed())
204+
205+
By("verifying that the returned pods have GVK populated")
206+
Expect(listObj.Items).NotTo(BeEmpty())
207+
Expect(listObj.Items).Should(HaveLen(3))
208+
for _, p := range listObj.Items {
209+
Expect(p.GroupVersionKind().Empty()).To(BeFalse())
210+
}
211+
})
212+
201213
It("should be able to restrict cache to a namespace", func() {
202214
By("creating a namespaced cache")
203215
namespacedCache, err := cache.New(cfg, cache.Options{Namespace: testNamespaceOne})

pkg/cache/internal/cache_reader.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ func (c *CacheReader) getListItems(objs []interface{}, labelSel labels.Selector)
142142
continue
143143
}
144144
}
145-
outItems = append(outItems, obj.DeepCopyObject())
145+
outObj := obj.DeepCopyObject()
146+
outObj.GetObjectKind().SetGroupVersionKind(c.groupVersionKind)
147+
outItems = append(outItems, outObj)
146148
}
147149
return outItems, nil
148150
}

0 commit comments

Comments
 (0)