Skip to content

Commit 9c36bda

Browse files
committed
Use indexer in lister
1 parent 2108c00 commit 9c36bda

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

pkg/cache/internal/cache_reader.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"reflect"
2323

2424
kcpcache "github.com/kcp-dev/apimachinery/pkg/cache"
25+
kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
26+
2527
apierrors "k8s.io/apimachinery/pkg/api/errors"
2628
apimeta "k8s.io/apimachinery/pkg/api/meta"
2729
"k8s.io/apimachinery/pkg/fields"
@@ -106,14 +108,24 @@ func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Ob
106108
}
107109

108110
// List lists items out of the indexer and writes them to out.
109-
func (c *CacheReader) List(_ context.Context, out client.ObjectList, opts ...client.ListOption) error {
111+
func (c *CacheReader) List(ctx context.Context, out client.ObjectList, opts ...client.ListOption) error {
110112
var objs []interface{}
111113
var err error
112114

113115
listOpts := client.ListOptions{}
114116
listOpts.ApplyOptions(opts)
115117

118+
// TODO(kcp), should we just require people to pass in the cluster list option, or maybe provide
119+
// a wrapper that adds it from the context automatically rather than doing this?
120+
// It may also make more sense to just use the context and not bother provided a ListOption for it
121+
if listOpts.Cluster.String() == "" {
122+
if cluster, ok := kcpclient.ClusterFromContext(ctx); ok {
123+
client.InCluster(cluster).ApplyToList(&listOpts)
124+
}
125+
}
126+
116127
switch {
128+
// TODO(kcp) add cluster to this case
117129
case listOpts.FieldSelector != nil:
118130
// TODO(directxman12): support more complicated field selectors by
119131
// combining multiple indices, GetIndexers, etc
@@ -125,10 +137,12 @@ func (c *CacheReader) List(_ context.Context, out client.ObjectList, opts ...cli
125137
// namespaced index key. Otherwise, ask for the non-namespaced variant by using the fake "all namespaces"
126138
// namespace.
127139
objs, err = c.indexer.ByIndex(FieldIndexName(field), KeyToNamespacedKey(listOpts.Namespace, val))
140+
case listOpts.Cluster.String() == "":
141+
objs = c.indexer.List()
128142
case listOpts.Namespace != "":
129-
objs, err = c.indexer.ByIndex(cache.NamespaceIndex, listOpts.Namespace)
143+
objs, err = c.indexer.ByIndex(kcpcache.ClusterAndNamespaceIndexName, kcpcache.ToClusterAwareKey(listOpts.Cluster.String(), listOpts.Namespace, ""))
130144
default:
131-
objs = c.indexer.List()
145+
objs, err = c.indexer.ByIndex(kcpcache.ClusterIndexName, kcpcache.ToClusterAwareKey(listOpts.Cluster.String(), "", ""))
132146
}
133147
if err != nil {
134148
return err

pkg/client/options.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package client
1818

1919
import (
20+
"github.com/kcp-dev/logicalcluster"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
"k8s.io/apimachinery/pkg/fields"
2223
"k8s.io/apimachinery/pkg/labels"
@@ -330,6 +331,9 @@ type ListOptions struct {
330331
// non-namespaced objects, or to list across all namespaces.
331332
Namespace string
332333

334+
// Cluster represents the cluster to list for, or empty to list across all clusters.
335+
Cluster logicalcluster.Name
336+
333337
// Limit specifies the maximum number of results to return from the server. The server may
334338
// not support this field on all resource types, but if it does and more results remain it
335339
// will set the continue field on the returned list object. This field is not supported if watch
@@ -488,6 +492,14 @@ func (m MatchingFieldsSelector) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
488492
m.ApplyToList(&opts.ListOptions)
489493
}
490494

495+
// InCluster restricts the list/delete operation to the given cluster.
496+
type InCluster logicalcluster.Name
497+
498+
// ApplyToList applies this configuration to the given list options.
499+
func (n InCluster) ApplyToList(opts *ListOptions) {
500+
opts.Cluster = logicalcluster.Name(n)
501+
}
502+
491503
// InNamespace restricts the list/delete operation to the given namespace.
492504
type InNamespace string
493505

0 commit comments

Comments
 (0)