Skip to content

Commit

Permalink
🐛 Fakeclient: Do not require ListKind
Browse files Browse the repository at this point in the history
The fake client will currently error out of given a Kind in an
UnstructuredList that doesn't end with list. This is different from what
we do in the actual client, where we tolerate that:
https://github.com/kubernetes-sigs/controller-runtime/blob/10ae090c1d3ac0c560dfa1a29b2517eb8d74442b/pkg/client/unstructured_client.go#L204

This change brings the fake client in line with the real client.
  • Loading branch information
alvaroaleman committed Apr 3, 2021
1 parent 10ae090 commit 3a037ef
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 3a037ef

Please sign in to comment.