Skip to content

Commit

Permalink
Merge pull request #1467 from alvaroaleman/don-require-list
Browse files Browse the repository at this point in the history
🐛 Fakeclient: Do not require ListKind
  • Loading branch information
k8s-ci-robot committed Apr 5, 2021
2 parents 10ae090 + 3a037ef commit 11dfabf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
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

0 comments on commit 11dfabf

Please sign in to comment.