@@ -22,6 +22,8 @@ import (
22
22
"reflect"
23
23
24
24
kcpcache "github.com/kcp-dev/apimachinery/pkg/cache"
25
+ kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
26
+
25
27
apierrors "k8s.io/apimachinery/pkg/api/errors"
26
28
apimeta "k8s.io/apimachinery/pkg/api/meta"
27
29
"k8s.io/apimachinery/pkg/fields"
@@ -106,14 +108,24 @@ func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Ob
106
108
}
107
109
108
110
// 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 {
110
112
var objs []interface {}
111
113
var err error
112
114
113
115
listOpts := client.ListOptions {}
114
116
listOpts .ApplyOptions (opts )
115
117
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
+
116
127
switch {
128
+ // TODO(kcp) add cluster to this case
117
129
case listOpts .FieldSelector != nil :
118
130
// TODO(directxman12): support more complicated field selectors by
119
131
// combining multiple indices, GetIndexers, etc
@@ -125,10 +137,12 @@ func (c *CacheReader) List(_ context.Context, out client.ObjectList, opts ...cli
125
137
// namespaced index key. Otherwise, ask for the non-namespaced variant by using the fake "all namespaces"
126
138
// namespace.
127
139
objs , err = c .indexer .ByIndex (FieldIndexName (field ), KeyToNamespacedKey (listOpts .Namespace , val ))
140
+ case listOpts .Cluster .String () == "" :
141
+ objs = c .indexer .List ()
128
142
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 , "" ) )
130
144
default :
131
- objs = c .indexer .List ( )
145
+ objs , err = c .indexer .ByIndex ( kcpcache . ClusterIndexName , kcpcache . ToClusterAwareKey ( listOpts . Cluster . String (), "" , "" ) )
132
146
}
133
147
if err != nil {
134
148
return err
0 commit comments