@@ -23,6 +23,7 @@ import (
23
23
"time"
24
24
25
25
"k8s.io/apimachinery/pkg/api/meta"
26
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
27
"k8s.io/apimachinery/pkg/fields"
27
28
"k8s.io/apimachinery/pkg/labels"
28
29
"k8s.io/apimachinery/pkg/runtime"
@@ -114,44 +115,59 @@ type Options struct {
114
115
// Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
115
116
Mapper meta.RESTMapper
116
117
117
- // Resync is the base frequency the informers are resynced.
118
+ // ResyncEvery is the base frequency the informers are resynced.
118
119
// Defaults to defaultResyncTime.
119
- // A 10 percent jitter will be added to the Resync period between informers
120
+ // A 10 percent jitter will be added to the ResyncEvery period between informers
120
121
// So that all informers will not send list requests simultaneously.
121
- Resync * time.Duration
122
+ ResyncEvery * time.Duration
122
123
123
- // Namespace restricts the cache's ListWatch to the desired namespace
124
- // Default watches all namespaces
125
- Namespace string
124
+ // View restricts the cache's ListWatch to the desired fields per GVK
125
+ // Default watches all fields and all namespaces.
126
+ View ViewOptions
127
+ }
126
128
127
- // SelectorsByObject restricts the cache's ListWatch to the desired
128
- // fields per GVK at the specified object, the map's value must implement
129
- // Selector [1] using for example a Set [2]
130
- // [1] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Selector
131
- // [2] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Set
132
- SelectorsByObject SelectorsByObject
129
+ // ViewOptions are the optional arguments for creating a cache view.
130
+ // A cache view restricts the Get(), List(), and internal ListWatch operations
131
+ // to the desired parameter.
132
+ type ViewOptions struct {
133
+ // Namespaces restricts the cache's ListWatch to the desired namespaces
134
+ // Default watches all namespaces
135
+ Namespaces []string
133
136
134
137
// DefaultSelector will be used as selectors for all object types
135
- // that do not have a selector in SelectorsByObject defined .
138
+ // unless they have a more specific selector set in ByObject .
136
139
DefaultSelector ObjectSelector
137
140
138
- // UnsafeDisableDeepCopyByObject indicates not to deep copy objects during get or
139
- // list objects per GVK at the specified object.
140
- // Be very careful with this, when enabled you must DeepCopy any object before mutating it,
141
- // otherwise you will mutate the object in the cache.
142
- UnsafeDisableDeepCopyByObject DisableDeepCopyByObject
141
+ // DefaultTransform will be used as transform for all object types
142
+ // unless they have a more specific transform set in ByObject.
143
+ DefaultTransform toolscache.TransformFunc
144
+
145
+ // ByObject restricts the cache's ListWatch to the desired fields per GVK at the specified object.
146
+ ByObject ViewByObject
147
+ }
143
148
144
- // TransformByObject is a map from GVKs to transformer functions which
149
+ // ViewByObject offers more fine-grained control over the cache's ListWatch by object.
150
+ type ViewByObject struct {
151
+ // Selectors restricts the cache's ListWatch to the desired
152
+ // fields per GVK at the specified object, the map's value must implement
153
+ // Selectors [1] using for example a Set [2]
154
+ // [1] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Selectors
155
+ // [2] https://pkg.go.dev/k8s.io/apimachinery/pkg/fields#Set
156
+ Selectors SelectorsByObject
157
+
158
+ // Transform is a map from objects to transformer functions which
145
159
// get applied when objects of the transformation are about to be committed
146
160
// to cache.
147
161
//
148
162
// This function is called both for new objects to enter the cache,
149
- // and for updated objects.
150
- TransformByObject TransformByObject
163
+ // and for updated objects.
164
+ Transform TransformByObject
151
165
152
- // DefaultTransform is the transform used for all GVKs which do
153
- // not have an explicit transform func set in TransformByObject
154
- DefaultTransform toolscache.TransformFunc
166
+ // UnsafeDisableDeepCopy indicates not to deep copy objects during get or
167
+ // list objects per GVK at the specified object.
168
+ // Be very careful with this, when enabled you must DeepCopy any object before mutating it,
169
+ // otherwise you will mutate the object in the cache.
170
+ UnsafeDisableDeepCopy DisableDeepCopyByObject
155
171
}
156
172
157
173
var defaultResyncTime = 10 * time .Hour
@@ -162,15 +178,15 @@ func New(config *rest.Config, opts Options) (Cache, error) {
162
178
if err != nil {
163
179
return nil , err
164
180
}
165
- selectorsByGVK , err := convertToByGVK (opts .SelectorsByObject , opts .DefaultSelector , opts .Scheme )
181
+ selectorsByGVK , err := convertToByGVK (opts .View . ByObject . Selectors , opts . View .DefaultSelector , opts .Scheme )
166
182
if err != nil {
167
183
return nil , err
168
184
}
169
- disableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (opts .UnsafeDisableDeepCopyByObject , opts .Scheme )
185
+ disableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (opts .View . ByObject . UnsafeDisableDeepCopy , opts .Scheme )
170
186
if err != nil {
171
187
return nil , err
172
188
}
173
- transformers , err := convertToByGVK (opts .TransformByObject , opts .DefaultTransform , opts .Scheme )
189
+ transformers , err := convertToByGVK (opts .View . ByObject . Transform , opts . View .DefaultTransform , opts .Scheme )
174
190
if err != nil {
175
191
return nil , err
176
192
}
@@ -181,13 +197,21 @@ func New(config *rest.Config, opts Options) (Cache, error) {
181
197
internalSelectorsByGVK [gvk ] = internal .Selector (selector )
182
198
}
183
199
200
+ if len (opts .View .Namespaces ) == 0 {
201
+ opts .View .Namespaces = []string {metav1 .NamespaceAll }
202
+ }
203
+
204
+ if len (opts .View .Namespaces ) > 1 {
205
+ return newMultiNamespaceCache (config , opts )
206
+ }
207
+
184
208
return & informerCache {
185
209
scheme : opts .Scheme ,
186
210
Informers : internal .NewInformers (config , & internal.InformersOpts {
187
211
Scheme : opts .Scheme ,
188
212
Mapper : opts .Mapper ,
189
- ResyncPeriod : * opts .Resync ,
190
- Namespace : opts .Namespace ,
213
+ ResyncPeriod : * opts .ResyncEvery ,
214
+ Namespace : opts .View . Namespaces [ 0 ] ,
191
215
ByGVK : internal.InformersOptsByGVK {
192
216
Selectors : internalSelectorsByGVK ,
193
217
DisableDeepCopy : disableDeepCopyByGVK ,
@@ -230,17 +254,17 @@ func (options Options) inheritFrom(inherited Options) (*Options, error) {
230
254
)
231
255
combined .Scheme = combineScheme (inherited .Scheme , options .Scheme )
232
256
combined .Mapper = selectMapper (inherited .Mapper , options .Mapper )
233
- combined .Resync = selectResync (inherited .Resync , options .Resync )
234
- combined .Namespace = selectNamespace (inherited .Namespace , options .Namespace )
235
- combined .SelectorsByObject , combined .DefaultSelector , err = combineSelectors (inherited , options , combined .Scheme )
257
+ combined .ResyncEvery = selectResync (inherited .ResyncEvery , options .ResyncEvery )
258
+ combined .View . Namespaces = selectNamespaces (inherited .View . Namespaces , options .View . Namespaces )
259
+ combined .View . ByObject . Selectors , combined . View .DefaultSelector , err = combineSelectors (inherited , options , combined .Scheme )
236
260
if err != nil {
237
261
return nil , err
238
262
}
239
- combined .UnsafeDisableDeepCopyByObject , err = combineUnsafeDeepCopy (inherited , options , combined .Scheme )
263
+ combined .View . ByObject . UnsafeDisableDeepCopy , err = combineUnsafeDeepCopy (inherited , options , combined .Scheme )
240
264
if err != nil {
241
265
return nil , err
242
266
}
243
- combined .TransformByObject , combined .DefaultTransform , err = combineTransforms (inherited , options , combined .Scheme )
267
+ combined .View . ByObject . Transform , combined . View .DefaultTransform , err = combineTransforms (inherited , options , combined .Scheme )
244
268
if err != nil {
245
269
return nil , err
246
270
}
@@ -277,8 +301,8 @@ func selectResync(def, override *time.Duration) *time.Duration {
277
301
return def
278
302
}
279
303
280
- func selectNamespace (def , override string ) string {
281
- if override != "" {
304
+ func selectNamespaces (def , override [] string ) [] string {
305
+ if len ( override ) > 0 {
282
306
return override
283
307
}
284
308
return def
@@ -292,11 +316,11 @@ func combineSelectors(inherited, options Options, scheme *runtime.Scheme) (Selec
292
316
//
293
317
// There is a bunch of complexity here because we need to convert to SelectorsByGVK
294
318
// to be able to match keys between options and inherited and then convert back to SelectorsByObject
295
- optionsSelectorsByGVK , err := convertToByGVK (options .SelectorsByObject , options .DefaultSelector , scheme )
319
+ optionsSelectorsByGVK , err := convertToByGVK (options .View . ByObject . Selectors , options . View .DefaultSelector , scheme )
296
320
if err != nil {
297
321
return nil , ObjectSelector {}, err
298
322
}
299
- inheritedSelectorsByGVK , err := convertToByGVK (inherited .SelectorsByObject , inherited .DefaultSelector , inherited .Scheme )
323
+ inheritedSelectorsByGVK , err := convertToByGVK (inherited .View . ByObject . Selectors , inherited . View .DefaultSelector , inherited .Scheme )
300
324
if err != nil {
301
325
return nil , ObjectSelector {}, err
302
326
}
@@ -355,11 +379,11 @@ func combineFieldSelectors(fs ...fields.Selector) fields.Selector {
355
379
func combineUnsafeDeepCopy (inherited , options Options , scheme * runtime.Scheme ) (DisableDeepCopyByObject , error ) {
356
380
// UnsafeDisableDeepCopyByObject is combined via precedence. Only if a value for a particular GVK is unset
357
381
// in options will a value from inherited be used.
358
- optionsDisableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (options .UnsafeDisableDeepCopyByObject , options .Scheme )
382
+ optionsDisableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (options .View . ByObject . UnsafeDisableDeepCopy , options .Scheme )
359
383
if err != nil {
360
384
return nil , err
361
385
}
362
- inheritedDisableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (inherited .UnsafeDisableDeepCopyByObject , inherited .Scheme )
386
+ inheritedDisableDeepCopyByGVK , err := convertToDisableDeepCopyByGVK (inherited .View . ByObject . UnsafeDisableDeepCopy , inherited .Scheme )
363
387
if err != nil {
364
388
return nil , err
365
389
}
@@ -379,11 +403,11 @@ func combineTransforms(inherited, options Options, scheme *runtime.Scheme) (Tran
379
403
// Transform functions are combined via chaining. If both inherited and options define a transform
380
404
// function, the transform function from inherited will be called first, and the transform function from
381
405
// options will be called second.
382
- optionsTransformByGVK , err := convertToByGVK (options .TransformByObject , options .DefaultTransform , options .Scheme )
406
+ optionsTransformByGVK , err := convertToByGVK (options .View . ByObject . Transform , options . View .DefaultTransform , options .Scheme )
383
407
if err != nil {
384
408
return nil , nil , err
385
409
}
386
- inheritedTransformByGVK , err := convertToByGVK (inherited .TransformByObject , inherited .DefaultTransform , inherited .Scheme )
410
+ inheritedTransformByGVK , err := convertToByGVK (inherited .View . ByObject . Transform , inherited . View .DefaultTransform , inherited .Scheme )
387
411
if err != nil {
388
412
return nil , nil , err
389
413
}
@@ -430,8 +454,8 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
430
454
}
431
455
432
456
// Default the resync period to 10 hours if unset
433
- if opts .Resync == nil {
434
- opts .Resync = & defaultResyncTime
457
+ if opts .ResyncEvery == nil {
458
+ opts .ResyncEvery = & defaultResyncTime
435
459
}
436
460
return opts , nil
437
461
}
0 commit comments