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 @@ -221,7 +221,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 @@ -54,6 +54,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 @@ -147,6 +149,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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work? We are not creating a node in the beforeEach or did I miss it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not creating a node but a node exist? I am making the assumption but when tested it showed that the node exists when querying the result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change it from len(result.Items) to EXPECT(result.Items) to not be Empty when that gave me 9. The test passes showing that the nodelist is not empty.

Copy link
Member

@sbueringer sbueringer Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. This seems to fail on my Machine somehow (at least when run with Intellij)? Which Nodes do you get?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible the node object(s) get created by other tests running in parallel using the same client? That would both explain the flaking and the inability to use len.

Sorry, I missed this before approving

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably. We could also simply make this case self-contained by creating a Node with some namespaced prefix

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flakiness of this is unfortunate because I made the assumption that because the node is there, it is always there. We can contain it by creating a node in the Before Spec if that helps with the testing suite.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3353 <-- Follow up for updating the test.

})

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