Skip to content

Commit 3153d31

Browse files
committed
update List in namespaced client
Signed-off-by: Troy Connor <[email protected]>
1 parent 1ad089b commit 3153d31

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pkg/client/namespaced_client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,12 @@ func (n *namespacedClient) Get(ctx context.Context, key ObjectKey, obj Object, o
221221

222222
// List implements client.Client.
223223
func (n *namespacedClient) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
224-
if n.namespace != "" {
224+
isNamespaceScoped, err := n.IsObjectNamespaced(obj)
225+
if err != nil {
226+
return fmt.Errorf("error finding the scope of the object: %w", err)
227+
}
228+
229+
if isNamespaceScoped && n.namespace != "" {
225230
opts = append(opts, InNamespace(n.namespace))
226231
}
227232
return n.client.List(ctx, obj, opts...)

pkg/client/namespaced_client_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ var _ = Describe("NamespacedClient", func() {
5454

5555
err := rbacv1.AddToScheme(sch)
5656
Expect(err).ToNot(HaveOccurred())
57+
err = corev1.AddToScheme(sch)
58+
Expect(err).ToNot(HaveOccurred())
5759
err = appsv1.AddToScheme(sch)
5860
Expect(err).ToNot(HaveOccurred())
5961

@@ -147,6 +149,13 @@ var _ = Describe("NamespacedClient", func() {
147149
Expect(result.Items[0]).To(BeEquivalentTo(*dep))
148150
})
149151

152+
It("should successfully List objects when object is not namespaced scoped", func(ctx SpecContext) {
153+
result := &corev1.NodeList{}
154+
opts := &client.ListOptions{}
155+
Expect(getClient().List(ctx, result, opts)).NotTo(HaveOccurred())
156+
Expect(result.Items).NotTo(BeEmpty())
157+
})
158+
150159
It("should List objects from the namespace specified in the client", func(ctx SpecContext) {
151160
result := &appsv1.DeploymentList{}
152161
opts := client.InNamespace("non-default")

0 commit comments

Comments
 (0)