@@ -29,6 +29,7 @@ import (
29
29
"k8s.io/client-go/rest"
30
30
toolscache "k8s.io/client-go/tools/cache"
31
31
"sigs.k8s.io/controller-runtime/pkg/client"
32
+ "sigs.k8s.io/controller-runtime/pkg/internal/objectutil"
32
33
)
33
34
34
35
// NewCacheFunc - Function for creating a new cache from the options and a rest config
@@ -134,13 +135,16 @@ func (c *multiNamespaceCache) IndexField(ctx context.Context, obj client.Object,
134
135
}
135
136
136
137
func (c * multiNamespaceCache ) Get (ctx context.Context , key client.ObjectKey , obj client.Object ) error {
137
- // gvk := obj.GetObjectKind().GroupVersionKind()
138
- // mapping, _ := c.RESTMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
139
- // if mapping.Scope.Name() == meta.RESTScopeNameRoot {
140
- // // Look into the global cache to fetch the object
141
- // cache := c.namespaceToCache[globalCache]
142
- // return cache.Get(ctx, key, obj)
143
- // }
138
+ isNamespaced , err := objectutil .IsNamespacedObject (obj , c .Scheme , c .RESTMapper )
139
+ if err != nil {
140
+ return err
141
+ }
142
+
143
+ if ! isNamespaced {
144
+ // Look into the global cache to fetch the object
145
+ cache := c .namespaceToCache [globalCache ]
146
+ return cache .Get (ctx , key , obj )
147
+ }
144
148
145
149
cache , ok := c .namespaceToCache [key .Namespace ]
146
150
if ! ok {
@@ -154,32 +158,23 @@ func (c *multiNamespaceCache) List(ctx context.Context, list client.ObjectList,
154
158
listOpts := client.ListOptions {}
155
159
listOpts .ApplyOptions (opts )
156
160
157
- // handle cluster scoped objects by looking into global cache
158
- // gvk := list.GetObjectKind().GroupVersionKind()
159
- // mapping, _ := c.RESTMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
160
- // if mapping.Scope.Name() == meta.RESTScopeNameRoot {
161
- // // Look at the global cache to get the objects with the specified GVK
162
- // cache := c.namespaceToCache[globalCache]
163
- // err := cache.List(ctx, list, opts...)
164
- // if err != nil {
165
- // return err
166
- // }
167
- // }
161
+ isNamespaced , err := objectutil . IsNamespacedObject ( list , c . Scheme , c . RESTMapper )
162
+ if err != nil {
163
+ return err
164
+ }
165
+
166
+ if ! isNamespaced {
167
+ // Look at the global cache to get the objects with the specified GVK
168
+ cache := c . namespaceToCache [ globalCache ]
169
+ return cache . List ( ctx , list , opts ... )
170
+ }
171
+
168
172
if listOpts .Namespace != corev1 .NamespaceAll {
169
173
cache , ok := c .namespaceToCache [listOpts .Namespace ]
170
174
if ! ok {
171
175
return fmt .Errorf ("unable to get: %v because of unknown namespace for the cache" , listOpts .Namespace )
172
176
}
173
- err := cache .List (ctx , list , opts ... )
174
- if err != nil {
175
- return err
176
- }
177
- items , err := apimeta .ExtractList (list )
178
- if err != nil {
179
- return err
180
- }
181
- uniqueItems := removeDuplicates (items )
182
- return apimeta .SetList (list , uniqueItems )
177
+ return cache .List (ctx , list , opts ... )
183
178
}
184
179
185
180
listAccessor , err := meta .ListAccessor (list )
@@ -210,28 +205,9 @@ func (c *multiNamespaceCache) List(ctx context.Context, list client.ObjectList,
210
205
// The last list call should have the most correct resource version.
211
206
resourceVersion = accessor .GetResourceVersion ()
212
207
}
213
-
214
- uniqueItems := removeDuplicates (allItems )
215
208
listAccessor .SetResourceVersion (resourceVersion )
216
209
217
- return apimeta .SetList (list , uniqueItems )
218
- }
219
-
220
- // removeDuplicates removes the duplicate objects obtained from all namespaces so that
221
- // the resulting list has objects with unique name and namespace.
222
- func removeDuplicates (items []runtime.Object ) []runtime.Object {
223
- objects := make (map [string ]bool )
224
- unique := []runtime.Object {}
225
-
226
- for _ , obj := range items {
227
- metaObj , _ := meta .Accessor (obj )
228
- key := metaObj .GetName () + " " + metaObj .GetNamespace ()
229
- if _ , value := objects [key ]; ! value {
230
- objects [key ] = true
231
- unique = append (unique , obj )
232
- }
233
- }
234
- return unique
210
+ return apimeta .SetList (list , allItems )
235
211
}
236
212
237
213
// multiNamespaceInformer knows how to handle interacting with the underlying informer across multiple namespaces
0 commit comments