Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fake client: preserve TypeMeta during List call with UnstructuredList #3074

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,15 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
return err
}

if _, isUnstructured := obj.(runtime.Unstructured); isUnstructured {
ta, err := meta.TypeAccessor(obj)
if err != nil {
return err
}
ta.SetKind(originalKind)
ta.SetAPIVersion(gvk.GroupVersion().String())
}

objs, err := meta.ExtractList(objCopy)
if err != nil {
return err
Expand Down
24 changes: 24 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ var _ = Describe("Fake client", func() {
list.SetKind("DeploymentList")
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).ToNot(HaveOccurred())
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("apps/v1"))
Expect(list.GetKind()).To(Equal("DeploymentList"))
Expect(list.Items).To(HaveLen(2))
})

Expand All @@ -167,6 +169,8 @@ var _ = Describe("Fake client", func() {
list.SetKind("Deployment")
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).ToNot(HaveOccurred())
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("apps/v1"))
Expect(list.GetKind()).To(Equal("Deployment"))
Expect(list.Items).To(HaveLen(2))
})

Expand All @@ -178,6 +182,8 @@ var _ = Describe("Fake client", func() {
list.SetKind("EndpointsList")
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).ToNot(HaveOccurred())
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("v1"))
Expect(list.GetKind()).To(Equal("EndpointsList"))
Expect(list.Items).To(HaveLen(1))
}

Expand Down Expand Up @@ -247,6 +253,8 @@ var _ = Describe("Fake client", func() {
list.SetAPIVersion("custom/v3")
list.SetKind("ImageList")
err := cl.List(context.Background(), list)
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("custom/v3"))
Expect(list.GetKind()).To(Equal("ImageList"))
Expect(err).ToNot(HaveOccurred())
})

Expand All @@ -255,6 +263,8 @@ var _ = Describe("Fake client", func() {
list.SetAPIVersion("custom/v4")
list.SetKind("Image")
err := cl.List(context.Background(), list)
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("custom/v4"))
Expect(list.GetKind()).To(Equal("Image"))
Expect(err).ToNot(HaveOccurred())
})

Expand Down Expand Up @@ -1325,6 +1335,20 @@ var _ = Describe("Fake client", func() {
Expect(list.Items).To(BeEmpty())
})

It("errors when there's no Index for the GroupVersionResource with UnstructuredList", func() {
listOpts := &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector("key", "val"),
}
list := &unstructured.UnstructuredList{}
list.SetAPIVersion("v1")
list.SetKind("ConfigMapList")
err := cl.List(context.Background(), list, listOpts)
Expect(err).To(HaveOccurred())
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("v1"))
Expect(list.GetKind()).To(Equal("ConfigMapList"))
Expect(list.Items).To(BeEmpty())
})

It("errors when there's no Index matching the field name", func() {
listOpts := &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector("spec.paused", "false"),
Expand Down
Loading