Skip to content

Commit f5a9781

Browse files
troy0820k8s-infra-cherrypick-robot
authored andcommitted
update List in namespaced client
Signed-off-by: Troy Connor <troy0820@users.noreply.github.com>
1 parent 3e8b259 commit f5a9781

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
@@ -213,7 +213,12 @@ func (n *namespacedClient) Get(ctx context.Context, key ObjectKey, obj Object, o
213213

214214
// List implements client.Client.
215215
func (n *namespacedClient) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
216-
if n.namespace != "" {
216+
isNamespaceScoped, err := n.IsObjectNamespaced(obj)
217+
if err != nil {
218+
return fmt.Errorf("error finding the scope of the object: %w", err)
219+
}
220+
221+
if isNamespaceScoped && n.namespace != "" {
217222
opts = append(opts, InNamespace(n.namespace))
218223
}
219224
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
@@ -53,6 +53,8 @@ var _ = Describe("NamespacedClient", func() {
5353

5454
err := rbacv1.AddToScheme(sch)
5555
Expect(err).ToNot(HaveOccurred())
56+
err = corev1.AddToScheme(sch)
57+
Expect(err).ToNot(HaveOccurred())
5658
err = appsv1.AddToScheme(sch)
5759
Expect(err).ToNot(HaveOccurred())
5860

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

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

0 commit comments

Comments
 (0)