Skip to content
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
10 changes: 4 additions & 6 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,11 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
return err
}

OriginalKind := gvk.Kind
originalKind := gvk.Kind

if !strings.HasSuffix(gvk.Kind, "List") {
return fmt.Errorf("non-list type %T (kind %q) passed as output", obj, gvk)
if strings.HasSuffix(gvk.Kind, "List") {
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
}
// we need the non-list GVK, so chop off the "List" from the end of the kind
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]

listOpts := client.ListOptions{}
listOpts.ApplyOptions(opts)
Expand All @@ -311,7 +309,7 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
if err != nil {
return err
}
ta.SetKind(OriginalKind)
ta.SetKind(originalKind)
ta.SetAPIVersion(gvk.GroupVersion().String())

j, err := json.Marshal(o)
Expand Down
10 changes: 10 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ var _ = Describe("Fake client", func() {
Expect(list.Items).To(HaveLen(2))
})

It("should be able to List using unstructured list when setting a non-list kind", func() {
By("Listing all deployments in a namespace")
list := &unstructured.UnstructuredList{}
list.SetAPIVersion("apps/v1")
list.SetKind("Deployment")
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(HaveLen(2))
})

It("should support filtering by labels and their values", func() {
By("Listing deployments with a particular label and value")
list := &appsv1.DeploymentList{}
Expand Down