@@ -21,17 +21,14 @@ import (
21
21
"fmt"
22
22
"time"
23
23
24
- "github.com/prometheus/common/log"
25
24
apimeta "k8s.io/apimachinery/pkg/api/meta"
26
25
"k8s.io/apimachinery/pkg/runtime"
27
26
"k8s.io/apimachinery/pkg/runtime/schema"
28
27
"k8s.io/client-go/rest"
28
+ toolscache "k8s.io/client-go/tools/cache"
29
29
"sigs.k8s.io/controller-runtime/pkg/client"
30
30
)
31
31
32
- // NewCacheFunc - Function for creating a new cache from the options and a rest config.
33
- type NewCacheFunc func (config * rest.Config , opts Options ) (Cache , error )
34
-
35
32
// a new global namespaced cache to handle cluster scoped resources.
36
33
const globalClusterCache = "_cluster"
37
34
@@ -66,7 +63,7 @@ func MultiClusterCacheBuilder(clusterNames []string) NewCacheFunc {
66
63
}
67
64
caches [cs ] = c
68
65
}
69
- return & multiNamespaceCache { namespaceToCache : caches , Scheme : opts .Scheme , RESTMapper : opts .Mapper , clusterCache : gCache }, nil
66
+ return & multiClusterCache { clusterToCache : caches , Scheme : opts .Scheme , RESTMapper : opts .Mapper , gClusterCache : gCache }, nil
70
67
}
71
68
}
72
69
@@ -93,10 +90,10 @@ func (c *multiClusterCache) GetInformer(ctx context.Context, obj client.Object)
93
90
return nil , fmt .Errorf ("error getting clustername %q" , err )
94
91
}
95
92
96
- if len (clusterName ) == "*" {
93
+ if (clusterName ) == "*" {
97
94
globalInformer , err := c .gClusterCache .GetInformer (ctx , obj )
98
95
if err != nil {
99
- return err
96
+ return nil , err
100
97
}
101
98
informers [globalClusterCache ] = globalInformer
102
99
}
@@ -109,13 +106,13 @@ func (c *multiClusterCache) GetInformer(ctx context.Context, obj client.Object)
109
106
informers [cs ] = informer
110
107
}
111
108
112
- return & multiClusterCache { clusterToCache : informers }, nil
109
+ return & multiClusterInformer { clusterNameToInformer : informers }, nil
113
110
114
111
}
115
112
116
113
func getClusterName (obj client.Object ) (string , error ) {
117
114
if obj == nil {
118
- return nil , fmt .Errorf ("object cannot be empty %v" , obj )
115
+ return "" , fmt .Errorf ("object cannot be empty %v" , obj )
119
116
}
120
117
if obj .GetClusterName () != "" {
121
118
return "*" , nil
@@ -124,39 +121,14 @@ func getClusterName(obj client.Object) (string, error) {
124
121
return obj .GetClusterName (), nil
125
122
}
126
123
127
- func (c * multiNamespaceCache ) GetInformerForKind (ctx context.Context , gvk schema.GroupVersionKind , clusterName string ) (Informer , error ) {
128
- informers := map [string ]Informer {} // clusterName -> informer
129
-
130
- clusterName , err := getCLusterName (obj )
131
- if err != nil {
132
- return err
133
- }
134
- // globalCLusterCache
135
- if len (clusterName ) == "*" {
136
- clusterCacheInf , err := c .gClusterCache .GetInformerForKind (ctx , gvk )
137
- if err != nil {
138
- return err
139
- }
140
-
141
- informers [globalClusterCache ] = clusterCacheInf
142
- return multiClusterCache {clusterToCache : informers }, nil
143
- }
144
-
145
- for cs , cache := range c .clusterToCache {
146
- informer , err := cache .GetInformerForKind (ctx , gvk )
147
- if err != nil {
148
- return err
149
- }
150
- informer [cs ] = informer
151
- }
152
-
153
- return multiClusterCache {clusterToCache : informer }, nil
124
+ func (c * multiClusterCache ) GetInformerForKind (ctx context.Context , gvk schema.GroupVersionKind ) (Informer , error ) {
125
+ return nil , fmt .Errorf ("not supported in multiClustercache" )
154
126
}
155
127
156
128
func (c * multiClusterCache ) Start (ctx context.Context ) error {
157
129
// start global cache
158
130
go func () {
159
- err := c .glusterCache .Start (ctx )
131
+ err := c .gClusterCache .Start (ctx )
160
132
if err != nil {
161
133
log .Error (err , "cluster scoped cache failed to start" )
162
134
}
@@ -191,9 +163,9 @@ func (c *multiClusterCache) WaitForCacheSync(ctx context.Context) bool {
191
163
return synced
192
164
}
193
165
194
- func (c * multiNamespaceCache ) IndexField (ctx context.Context , obj client.Object , field string , extractValue client.IndexerFunc ) error {
166
+ func (c * multiClusterCache ) IndexField (ctx context.Context , obj client.Object , field string , extractValue client.IndexerFunc ) error {
195
167
196
- clusterName , err := getCLusterName (obj )
168
+ clusterName , err := getClusterName (obj )
197
169
if err != nil {
198
170
return err
199
171
}
@@ -233,16 +205,17 @@ func (c *multiClusterCache) Get(ctx context.Context, key client.ObjectKey, obj c
233
205
// ClusterName is passed => getCache
234
206
// ListAll clusters => clusterName is "*"
235
207
236
- func (c * multiClusterCache ) List (ctx context.Context , list client.ObjectList , opts ... client.ListOptions ) {
208
+ func (c * multiClusterCache ) List (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
237
209
listOpts := client.ListOptions {}
238
- listOpts .ApplyOptions (opts )
239
210
240
- clusterName := opts .ClusterName
211
+ clusterName := listOpts .ClusterName
241
212
if clusterName == "" {
242
213
// initial stab - error out
243
214
fmt .Errorf ("cluster Name is empty in listOpts %v" , listOpts )
244
215
}
245
216
217
+ listOpts .ApplyOptions (opts )
218
+
246
219
if clusterName == "*" {
247
220
// Look at gloabal cluster cache
248
221
return c .gClusterCache .List (ctx , list , opts ... )
@@ -261,7 +234,7 @@ type multiClusterInformer struct {
261
234
clusterNameToInformer map [string ]Informer
262
235
}
263
236
264
- var _Informer = & multiClusterInformer
237
+ var _Informer = & multiClusterInformer {}
265
238
266
239
// AddEventHandler adds the handler to each namespaced informer.
267
240
func (i * multiClusterInformer ) AddEventHandler (handler toolscache.ResourceEventHandler ) {
0 commit comments