Skip to content

Commit ef9edab

Browse files
authored
Merge pull request kubernetes-sigs#1287 from vincepri/unstructed-list-bug-06
🐛 client.List should check on UnstructuredList, not Unstructured
2 parents 9357e78 + df4e60f commit ef9edab

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
@@ -1681,6 +1681,36 @@ var _ = Describe("Client", func() {
16811681
close(done)
16821682
}, serverSideTimeoutSeconds)
16831683

1684+
It("should fetch unstructured collection of objects, even if scheme is empty", func(done Done) {
1685+
By("create an initial object")
1686+
_, err := clientset.AppsV1().Deployments(ns).Create(ctx, dep, metav1.CreateOptions{})
1687+
Expect(err).NotTo(HaveOccurred())
1688+
1689+
cl, err := client.New(cfg, client.Options{Scheme: runtime.NewScheme()})
1690+
Expect(err).NotTo(HaveOccurred())
1691+
1692+
By("listing all objects of that type in the cluster")
1693+
deps := &unstructured.UnstructuredList{}
1694+
deps.SetGroupVersionKind(schema.GroupVersionKind{
1695+
Group: "apps",
1696+
Kind: "DeploymentList",
1697+
Version: "v1",
1698+
})
1699+
err = cl.List(context.Background(), deps)
1700+
Expect(err).NotTo(HaveOccurred())
1701+
1702+
Expect(deps.Items).NotTo(BeEmpty())
1703+
hasDep := false
1704+
for _, item := range deps.Items {
1705+
if item.GetName() == dep.Name && item.GetNamespace() == dep.Namespace {
1706+
hasDep = true
1707+
break
1708+
}
1709+
}
1710+
Expect(hasDep).To(BeTrue())
1711+
close(done)
1712+
}, serverSideTimeoutSeconds)
1713+
16841714
It("should return an empty list if there are no matching objects", func(done Done) {
16851715
cl, err := client.New(cfg, client.Options{})
16861716
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)