Skip to content

Commit 8d52513

Browse files
authored
Merge pull request #1286 from vincepri/unstructured-list-bug
🐛 client.List should check on UnstructuredList, not Unstructured
2 parents 7d38e25 + 5d44082 commit 8d52513

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

pkg/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (c *client) Get(ctx context.Context, key ObjectKey, obj runtime.Object) err
198198
// List implements client.Client
199199
func (c *client) List(ctx context.Context, obj runtime.Object, opts ...ListOption) error {
200200
switch obj.(type) {
201-
case *unstructured.Unstructured:
201+
case *unstructured.UnstructuredList:
202202
return c.unstructuredClient.List(ctx, obj, opts...)
203203
case *metav1.PartialObjectMetadataList:
204204
return c.metadataClient.List(ctx, obj, opts...)

pkg/client/client_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,36 @@ var _ = Describe("Client", func() {
19861986
close(done)
19871987
}, serverSideTimeoutSeconds)
19881988

1989+
It("should fetch unstructured collection of objects, even if scheme is empty", func(done Done) {
1990+
By("create an initial object")
1991+
_, err := clientset.AppsV1().Deployments(ns).Create(dep)
1992+
Expect(err).NotTo(HaveOccurred())
1993+
1994+
cl, err := client.New(cfg, client.Options{Scheme: runtime.NewScheme()})
1995+
Expect(err).NotTo(HaveOccurred())
1996+
1997+
By("listing all objects of that type in the cluster")
1998+
deps := &unstructured.UnstructuredList{}
1999+
deps.SetGroupVersionKind(schema.GroupVersionKind{
2000+
Group: "apps",
2001+
Kind: "DeploymentList",
2002+
Version: "v1",
2003+
})
2004+
err = cl.List(context.Background(), deps)
2005+
Expect(err).NotTo(HaveOccurred())
2006+
2007+
Expect(deps.Items).NotTo(BeEmpty())
2008+
hasDep := false
2009+
for _, item := range deps.Items {
2010+
if item.GetName() == dep.Name && item.GetNamespace() == dep.Namespace {
2011+
hasDep = true
2012+
break
2013+
}
2014+
}
2015+
Expect(hasDep).To(BeTrue())
2016+
close(done)
2017+
}, serverSideTimeoutSeconds)
2018+
19892019
It("should return an empty list if there are no matching objects", func(done Done) {
19902020
cl, err := client.New(cfg, client.Options{})
19912021
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)