Skip to content

Commit 3a037ef

Browse files
committed
🐛 Fakeclient: Do not require ListKind
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.
1 parent 10ae090 commit 3a037ef

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

pkg/client/fake/client.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,11 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
290290
return err
291291
}
292292

293-
OriginalKind := gvk.Kind
293+
originalKind := gvk.Kind
294294

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

301299
listOpts := client.ListOptions{}
302300
listOpts.ApplyOptions(opts)
@@ -311,7 +309,7 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
311309
if err != nil {
312310
return err
313311
}
314-
ta.SetKind(OriginalKind)
312+
ta.SetKind(originalKind)
315313
ta.SetAPIVersion(gvk.GroupVersion().String())
316314

317315
j, err := json.Marshal(o)

pkg/client/fake/client_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ var _ = Describe("Fake client", func() {
129129
Expect(list.Items).To(HaveLen(2))
130130
})
131131

132+
It("should be able to List using unstructured list when setting a non-list kind", func() {
133+
By("Listing all deployments in a namespace")
134+
list := &unstructured.UnstructuredList{}
135+
list.SetAPIVersion("apps/v1")
136+
list.SetKind("Deployment")
137+
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
138+
Expect(err).To(BeNil())
139+
Expect(list.Items).To(HaveLen(2))
140+
})
141+
132142
It("should support filtering by labels and their values", func() {
133143
By("Listing deployments with a particular label and value")
134144
list := &appsv1.DeploymentList{}

0 commit comments

Comments
 (0)