Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/client/namespaced_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,12 @@ func (n *namespacedClient) Get(ctx context.Context, key ObjectKey, obj Object, o

// List implements client.Client.
func (n *namespacedClient) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
if n.namespace != "" {
isNamespaceScoped, err := n.IsObjectNamespaced(obj)
if err != nil {
return fmt.Errorf("error finding the scope of the object: %w", err)
}

if isNamespaceScoped && n.namespace != "" {
opts = append(opts, InNamespace(n.namespace))
}
return n.client.List(ctx, obj, opts...)
Expand Down
9 changes: 9 additions & 0 deletions pkg/client/namespaced_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var _ = Describe("NamespacedClient", func() {

err := rbacv1.AddToScheme(sch)
Expect(err).ToNot(HaveOccurred())
err = corev1.AddToScheme(sch)
Expect(err).ToNot(HaveOccurred())
err = appsv1.AddToScheme(sch)
Expect(err).ToNot(HaveOccurred())

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

It("should successfully List objects when object is not namespaced scoped", func(ctx SpecContext) {
result := &corev1.NodeList{}
opts := &client.ListOptions{}
Expect(getClient().List(ctx, result, opts)).NotTo(HaveOccurred())
Expect(result.Items).NotTo(BeEmpty())
})

It("should List objects from the namespace specified in the client", func(ctx SpecContext) {
result := &appsv1.DeploymentList{}
opts := client.InNamespace("non-default")
Expand Down