Skip to content

Commit b48c1bc

Browse files
Only add InNamespace option when not provided to clientutil List
This patch changes the implementation of the `List` method on the k8s client wrapper implemented in the `clientutil` package to only add the `InNamespace` option for restricting to a certain namespace, if that option is not already provided to the function, honoring the callers intend.
1 parent e392c0a commit b48c1bc

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

internal/clientutil/clientutil.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
22
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package clientutil provides a client wrapper for the controller-runtime client with convenience functions.
35
package clientutil
46

57
import (
68
"context"
79
"crypto/tls"
810
"errors"
911
"fmt"
12+
"slices"
1013

1114
corev1 "k8s.io/api/core/v1"
1215
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -50,7 +53,9 @@ func (c *Client) Get(ctx context.Context, key client.ObjectKey, obj client.Objec
5053
// returned from the Server. It will automatically restrict the request to the
5154
// namespace that is set in the Client.
5255
func (c *Client) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
53-
opts = append(opts, client.InNamespace(c.DefaultNamespace))
56+
if slices.ContainsFunc(opts, func(opt client.ListOption) bool { _, ok := opt.(client.InNamespace); return ok }) {
57+
opts = append(opts, client.InNamespace(c.DefaultNamespace))
58+
}
5459
return c.r.List(ctx, list, opts...)
5560
}
5661

0 commit comments

Comments
 (0)