@@ -24,6 +24,7 @@ import (
24
24
"time"
25
25
26
26
"k8s.io/apimachinery/pkg/api/meta"
27
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
28
"k8s.io/apimachinery/pkg/fields"
28
29
"k8s.io/apimachinery/pkg/labels"
29
30
"k8s.io/apimachinery/pkg/runtime"
@@ -118,44 +119,59 @@ type Options struct {
118
119
// Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
119
120
Mapper meta.RESTMapper
120
121
121
- // Resync is the base frequency the informers are resynced.
122
+ // ResyncEvery is the base frequency the informers are resynced.
122
123
// Defaults to defaultResyncTime.
123
- // A 10 percent jitter will be added to the Resync period between informers
124
+ // A 10 percent jitter will be added to the ResyncEvery period between informers
124
125
// So that all informers will not send list requests simultaneously.
125
- Resync * time.Duration
126
+ ResyncEvery * time.Duration
126
127
127
- // Namespace restricts the cache's ListWatch to the desired namespace
128
- // Default watches all namespaces
129
- Namespace string
128
+ // View restricts the cache's ListWatch to the desired fields per GVK
129
+ // Default watches all fields and all namespaces.
130
+ View ViewOptions
131
+ }
130
132
131
- // SelectorsByObject restricts the cache's ListWatch to the desired
132
- // fields per GVK at the specified object, the map's value must implement
133
- // Selector [1] using for example a Set [2]
134
- // [1] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Selector
135
- // [2] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Set
136
- SelectorsByObject SelectorsByObject
133
+ // ViewOptions are the optional arguments for creating a cache view.
134
+ // A cache view restricts the Get(), List(), and internal ListWatch operations
135
+ // to the desired parameter.
136
+ type ViewOptions struct {
137
+ // Namespaces restricts the cache's ListWatch to the desired namespaces
138
+ // Default watches all namespaces
139
+ Namespaces []string
137
140
138
141
// DefaultSelector will be used as selectors for all object types
139
- // that do not have a selector in SelectorsByObject defined .
142
+ // unless they have a more specific selector set in ByObject .
140
143
DefaultSelector ObjectSelector
141
144
142
- // UnsafeDisableDeepCopyByObject indicates not to deep copy objects during get or
143
- // list objects per GVK at the specified object.
144
- // Be very careful with this, when enabled you must DeepCopy any object before mutating it,
145
- // otherwise you will mutate the object in the cache.
146
- UnsafeDisableDeepCopyByObject DisableDeepCopyByObject
145
+ // DefaultTransform will be used as transform for all object types
146
+ // unless they have a more specific transform set in ByObject.
147
+ DefaultTransform toolscache.TransformFunc
148
+
149
+ // ByObject restricts the cache's ListWatch to the desired fields per GVK at the specified object.
150
+ ByObject ViewByObject
151
+ }
147
152
148
- // TransformByObject is a map from GVKs to transformer functions which
153
+ // ViewByObject offers more fine-grained control over the cache's ListWatch by object.
154
+ type ViewByObject struct {
155
+ // Selectors restricts the cache's ListWatch to the desired
156
+ // fields per GVK at the specified object, the map's value must implement
157
+ // Selectors [1] using for example a Set [2]
158
+ // [1] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Selectors
159
+ // [2] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Set
160
+ Selectors SelectorsByObject
161
+
162
+ // Transform is a map from objects to transformer functions which
149
163
// get applied when objects of the transformation are about to be committed
150
164
// to cache.
151
165
//
152
166
// This function is called both for new objects to enter the cache,
153
- // and for updated objects.
154
- TransformByObject TransformByObject
167
+ // and for updated objects.
168
+ Transform TransformByObject
155
169
156
- // DefaultTransform is the transform used for all GVKs which do
157
- // not have an explicit transform func set in TransformByObject
158
- DefaultTransform toolscache.TransformFunc
170
+ // UnsafeDisableDeepCopy indicates not to deep copy objects during get or
171
+ // list objects per GVK at the specified object.
172
+ // Be very careful with this, when enabled you must DeepCopy any object before mutating it,
173
+ // otherwise you will mutate the object in the cache.
174
+ UnsafeDisableDeepCopy DisableDeepCopyByObject
159
175
}
160
176
161
177
var defaultResyncTime = 10 * time .Hour
@@ -166,15 +182,15 @@ func New(config *rest.Config, opts Options) (Cache, error) {
166
182
if err != nil {
167
183
return nil , err
168
184
}
169
- selectorsByGVK , err := convertToByGVK (opts .SelectorsByObject , opts .DefaultSelector , opts .Scheme )
185
+ selectorsByGVK , err := convertToByGVK (opts .View . ByObject . Selectors , opts . View .DefaultSelector , opts .Scheme )
170
186
if err != nil {
171
187
return nil , err
172
188
}
173
- disableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (opts .UnsafeDisableDeepCopyByObject , opts .Scheme )
189
+ disableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (opts .View . ByObject . UnsafeDisableDeepCopy , opts .Scheme )
174
190
if err != nil {
175
191
return nil , err
176
192
}
177
- transformers , err := convertToByGVK (opts .TransformByObject , opts .DefaultTransform , opts .Scheme )
193
+ transformers , err := convertToByGVK (opts .View . ByObject . Transform , opts . View .DefaultTransform , opts .Scheme )
178
194
if err != nil {
179
195
return nil , err
180
196
}
@@ -185,14 +201,22 @@ func New(config *rest.Config, opts Options) (Cache, error) {
185
201
internalSelectorsByGVK [gvk ] = internal .Selector (selector )
186
202
}
187
203
204
+ if len (opts .View .Namespaces ) == 0 {
205
+ opts .View .Namespaces = []string {metav1 .NamespaceAll }
206
+ }
207
+
208
+ if len (opts .View .Namespaces ) > 1 {
209
+ return newMultiNamespaceCache (config , opts )
210
+ }
211
+
188
212
return & informerCache {
189
213
scheme : opts .Scheme ,
190
214
Informers : internal .NewInformers (config , & internal.InformersOpts {
191
215
HTTPClient : opts .HTTPClient ,
192
216
Scheme : opts .Scheme ,
193
217
Mapper : opts .Mapper ,
194
- ResyncPeriod : * opts .Resync ,
195
- Namespace : opts .Namespace ,
218
+ ResyncPeriod : * opts .ResyncEvery ,
219
+ Namespace : opts .View . Namespaces [ 0 ] ,
196
220
ByGVK : internal.InformersOptsByGVK {
197
221
Selectors : internalSelectorsByGVK ,
198
222
DisableDeepCopy : disableDeepCopyByGVK ,
@@ -235,17 +259,17 @@ func (options Options) inheritFrom(inherited Options) (*Options, error) {
235
259
)
236
260
combined .Scheme = combineScheme (inherited .Scheme , options .Scheme )
237
261
combined .Mapper = selectMapper (inherited .Mapper , options .Mapper )
238
- combined .Resync = selectResync (inherited .Resync , options .Resync )
239
- combined .Namespace = selectNamespace (inherited .Namespace , options .Namespace )
240
- combined .SelectorsByObject , combined .DefaultSelector , err = combineSelectors (inherited , options , combined .Scheme )
262
+ combined .ResyncEvery = selectResync (inherited .ResyncEvery , options .ResyncEvery )
263
+ combined .View . Namespaces = selectNamespaces (inherited .View . Namespaces , options .View . Namespaces )
264
+ combined .View . ByObject . Selectors , combined . View .DefaultSelector , err = combineSelectors (inherited , options , combined .Scheme )
241
265
if err != nil {
242
266
return nil , err
243
267
}
244
- combined .UnsafeDisableDeepCopyByObject , err = combineUnsafeDeepCopy (inherited , options , combined .Scheme )
268
+ combined .View . ByObject . UnsafeDisableDeepCopy , err = combineUnsafeDeepCopy (inherited , options , combined .Scheme )
245
269
if err != nil {
246
270
return nil , err
247
271
}
248
- combined .TransformByObject , combined .DefaultTransform , err = combineTransforms (inherited , options , combined .Scheme )
272
+ combined .View . ByObject . Transform , combined . View .DefaultTransform , err = combineTransforms (inherited , options , combined .Scheme )
249
273
if err != nil {
250
274
return nil , err
251
275
}
@@ -282,8 +306,8 @@ func selectResync(def, override *time.Duration) *time.Duration {
282
306
return def
283
307
}
284
308
285
- func selectNamespace (def , override string ) string {
286
- if override != "" {
309
+ func selectNamespaces (def , override [] string ) [] string {
310
+ if len ( override ) > 0 {
287
311
return override
288
312
}
289
313
return def
@@ -297,11 +321,11 @@ func combineSelectors(inherited, options Options, scheme *runtime.Scheme) (Selec
297
321
//
298
322
// There is a bunch of complexity here because we need to convert to SelectorsByGVK
299
323
// to be able to match keys between options and inherited and then convert back to SelectorsByObject
300
- optionsSelectorsByGVK , err := convertToByGVK (options .SelectorsByObject , options .DefaultSelector , scheme )
324
+ optionsSelectorsByGVK , err := convertToByGVK (options .View . ByObject . Selectors , options . View .DefaultSelector , scheme )
301
325
if err != nil {
302
326
return nil , ObjectSelector {}, err
303
327
}
304
- inheritedSelectorsByGVK , err := convertToByGVK (inherited .SelectorsByObject , inherited .DefaultSelector , inherited .Scheme )
328
+ inheritedSelectorsByGVK , err := convertToByGVK (inherited .View . ByObject . Selectors , inherited . View .DefaultSelector , inherited .Scheme )
305
329
if err != nil {
306
330
return nil , ObjectSelector {}, err
307
331
}
@@ -360,11 +384,11 @@ func combineFieldSelectors(fs ...fields.Selector) fields.Selector {
360
384
func combineUnsafeDeepCopy (inherited , options Options , scheme * runtime.Scheme ) (DisableDeepCopyByObject , error ) {
361
385
// UnsafeDisableDeepCopyByObject is combined via precedence. Only if a value for a particular GVK is unset
362
386
// in options will a value from inherited be used.
363
- optionsDisableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (options .UnsafeDisableDeepCopyByObject , options .Scheme )
387
+ optionsDisableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (options .View . ByObject . UnsafeDisableDeepCopy , options .Scheme )
364
388
if err != nil {
365
389
return nil , err
366
390
}
367
- inheritedDisableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (inherited .UnsafeDisableDeepCopyByObject , inherited .Scheme )
391
+ inheritedDisableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (inherited .View . ByObject . UnsafeDisableDeepCopy , inherited .Scheme )
368
392
if err != nil {
369
393
return nil , err
370
394
}
@@ -384,11 +408,11 @@ func combineTransforms(inherited, options Options, scheme *runtime.Scheme) (Tran
384
408
// Transform functions are combined via chaining. If both inherited and options define a transform
385
409
// function, the transform function from inherited will be called first, and the transform function from
386
410
// options will be called second.
387
- optionsTransformByGVK , err := convertToByGVK (options .TransformByObject , options .DefaultTransform , options .Scheme )
411
+ optionsTransformByGVK , err := convertToByGVK (options .View . ByObject . Transform , options . View .DefaultTransform , options .Scheme )
388
412
if err != nil {
389
413
return nil , nil , err
390
414
}
391
- inheritedTransformByGVK , err := convertToByGVK (inherited .TransformByObject , inherited .DefaultTransform , inherited .Scheme )
415
+ inheritedTransformByGVK , err := convertToByGVK (inherited .View . ByObject . Transform , inherited . View .DefaultTransform , inherited .Scheme )
392
416
if err != nil {
393
417
return nil , nil , err
394
418
}
@@ -447,8 +471,8 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
447
471
}
448
472
449
473
// Default the resync period to 10 hours if unset
450
- if opts .Resync == nil {
451
- opts .Resync = & defaultResyncTime
474
+ if opts .ResyncEvery == nil {
475
+ opts .ResyncEvery = & defaultResyncTime
452
476
}
453
477
return opts , nil
454
478
}
0 commit comments