@@ -20,7 +20,6 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"net/http"
23
- "reflect"
24
23
"time"
25
24
26
25
"k8s.io/apimachinery/pkg/api/meta"
@@ -233,183 +232,6 @@ func New(config *rest.Config, opts Options) (Cache, error) {
233
232
}, nil
234
233
}
235
234
236
- // BuilderWithOptions returns a Cache constructor that will build a cache
237
- // honoring the options argument, this is useful to specify options like
238
- // ByObjects, DefaultSelector, DefaultTransform, etc.
239
- // WARNING: If ByObject selectors are specified, filtered out resources are not
240
- // returned.
241
- // WARNING: If ByObject UnsafeDisableDeepCopy is enabled, you must DeepCopy any object
242
- // returned from cache get/list before mutating it.
243
- func BuilderWithOptions (options Options ) NewCacheFunc {
244
- return func (config * rest.Config , inherited Options ) (Cache , error ) {
245
- var err error
246
- inherited , err = defaultOpts (config , inherited )
247
- if err != nil {
248
- return nil , err
249
- }
250
- options , err = defaultOpts (config , options )
251
- if err != nil {
252
- return nil , err
253
- }
254
- combined , err := options .inheritFrom (inherited )
255
- if err != nil {
256
- return nil , err
257
- }
258
- return New (config , * combined )
259
- }
260
- }
261
-
262
- func (options Options ) inheritFrom (inherited Options ) (* Options , error ) {
263
- var (
264
- combined Options
265
- err error
266
- )
267
- combined .Scheme = combineScheme (inherited .Scheme , options .Scheme )
268
- combined .Mapper = selectMapper (inherited .Mapper , options .Mapper )
269
- combined .SyncPeriod = selectResync (inherited .SyncPeriod , options .SyncPeriod )
270
- combined .Namespaces = selectNamespaces (inherited .Namespaces , options .Namespaces )
271
- combined .DefaultLabelSelector = combineSelector (
272
- internal.Selector {Label : inherited .DefaultLabelSelector },
273
- internal.Selector {Label : options .DefaultLabelSelector },
274
- ).Label
275
- combined .DefaultFieldSelector = combineSelector (
276
- internal.Selector {Field : inherited .DefaultFieldSelector },
277
- internal.Selector {Field : options .DefaultFieldSelector },
278
- ).Field
279
- combined .DefaultTransform = combineTransform (inherited .DefaultTransform , options .DefaultTransform )
280
- combined .ByObject , err = combineByObject (inherited , options , combined .Scheme )
281
- if options .UnsafeDisableDeepCopy != nil {
282
- combined .UnsafeDisableDeepCopy = options .UnsafeDisableDeepCopy
283
- } else {
284
- combined .UnsafeDisableDeepCopy = inherited .UnsafeDisableDeepCopy
285
- }
286
- if err != nil {
287
- return nil , err
288
- }
289
- return & combined , nil
290
- }
291
-
292
- func combineScheme (schemes ... * runtime.Scheme ) * runtime.Scheme {
293
- var out * runtime.Scheme
294
- for _ , sch := range schemes {
295
- if sch == nil {
296
- continue
297
- }
298
- for gvk , t := range sch .AllKnownTypes () {
299
- if out == nil {
300
- out = runtime .NewScheme ()
301
- }
302
- out .AddKnownTypeWithName (gvk , reflect .New (t ).Interface ().(runtime.Object ))
303
- }
304
- }
305
- return out
306
- }
307
-
308
- func selectMapper (def , override meta.RESTMapper ) meta.RESTMapper {
309
- if override != nil {
310
- return override
311
- }
312
- return def
313
- }
314
-
315
- func selectResync (def , override * time.Duration ) * time.Duration {
316
- if override != nil {
317
- return override
318
- }
319
- return def
320
- }
321
-
322
- func selectNamespaces (def , override []string ) []string {
323
- if len (override ) > 0 {
324
- return override
325
- }
326
- return def
327
- }
328
-
329
- func combineByObject (inherited , options Options , scheme * runtime.Scheme ) (map [client.Object ]ByObject , error ) {
330
- optionsByGVK , err := convertToInformerOptsByGVK (options .ByObject , scheme )
331
- if err != nil {
332
- return nil , err
333
- }
334
- inheritedByGVK , err := convertToInformerOptsByGVK (inherited .ByObject , scheme )
335
- if err != nil {
336
- return nil , err
337
- }
338
- for gvk , inheritedByGVK := range inheritedByGVK {
339
- unsafeDisableDeepCopy := options .UnsafeDisableDeepCopy
340
- if current , ok := optionsByGVK [gvk ]; ok {
341
- unsafeDisableDeepCopy = current .UnsafeDisableDeepCopy
342
- }
343
- optionsByGVK [gvk ] = internal.InformersOptsByGVK {
344
- Selector : combineSelector (inheritedByGVK .Selector , optionsByGVK [gvk ].Selector ),
345
- Transform : combineTransform (inheritedByGVK .Transform , optionsByGVK [gvk ].Transform ),
346
- UnsafeDisableDeepCopy : unsafeDisableDeepCopy ,
347
- }
348
- }
349
- return convertToByObject (optionsByGVK , scheme )
350
- }
351
-
352
- func combineSelector (selectors ... internal.Selector ) internal.Selector {
353
- ls := make ([]labels.Selector , 0 , len (selectors ))
354
- fs := make ([]fields.Selector , 0 , len (selectors ))
355
- for _ , s := range selectors {
356
- ls = append (ls , s .Label )
357
- fs = append (fs , s .Field )
358
- }
359
- return internal.Selector {
360
- Label : combineLabelSelectors (ls ... ),
361
- Field : combineFieldSelectors (fs ... ),
362
- }
363
- }
364
-
365
- func combineLabelSelectors (ls ... labels.Selector ) labels.Selector {
366
- var combined labels.Selector
367
- for _ , l := range ls {
368
- if l == nil {
369
- continue
370
- }
371
- if combined == nil {
372
- combined = labels .NewSelector ()
373
- }
374
- reqs , _ := l .Requirements ()
375
- combined = combined .Add (reqs ... )
376
- }
377
- return combined
378
- }
379
-
380
- func combineFieldSelectors (fs ... fields.Selector ) fields.Selector {
381
- nonNil := fs [:0 ]
382
- for _ , f := range fs {
383
- if f == nil {
384
- continue
385
- }
386
- nonNil = append (nonNil , f )
387
- }
388
- if len (nonNil ) == 0 {
389
- return nil
390
- }
391
- if len (nonNil ) == 1 {
392
- return nonNil [0 ]
393
- }
394
- return fields .AndSelectors (nonNil ... )
395
- }
396
-
397
- func combineTransform (inherited , current toolscache.TransformFunc ) toolscache.TransformFunc {
398
- if inherited == nil {
399
- return current
400
- }
401
- if current == nil {
402
- return inherited
403
- }
404
- return func (in interface {}) (interface {}, error ) {
405
- mid , err := inherited (in )
406
- if err != nil {
407
- return nil , err
408
- }
409
- return current (mid )
410
- }
411
- }
412
-
413
235
func defaultOpts (config * rest.Config , opts Options ) (Options , error ) {
414
236
logger := log .WithName ("setup" )
415
237
@@ -466,28 +288,3 @@ func convertToInformerOptsByGVK(in map[client.Object]ByObject, scheme *runtime.S
466
288
}
467
289
return out , nil
468
290
}
469
-
470
- func convertToByObject (in map [schema.GroupVersionKind ]internal.InformersOptsByGVK , scheme * runtime.Scheme ) (map [client.Object ]ByObject , error ) {
471
- out := map [client.Object ]ByObject {}
472
- for gvk , opts := range in {
473
- if gvk == (schema.GroupVersionKind {}) {
474
- continue
475
- }
476
- obj , err := scheme .New (gvk )
477
- if err != nil {
478
- return nil , err
479
- }
480
- cObj , ok := obj .(client.Object )
481
- if ! ok {
482
- return nil , fmt .Errorf ("object %T for GVK %q does not implement client.Object" , obj , gvk )
483
- }
484
- cObj .GetObjectKind ().SetGroupVersionKind (gvk )
485
- out [cObj ] = ByObject {
486
- Field : opts .Selector .Field ,
487
- Label : opts .Selector .Label ,
488
- Transform : opts .Transform ,
489
- UnsafeDisableDeepCopy : opts .UnsafeDisableDeepCopy ,
490
- }
491
- }
492
- return out , nil
493
- }
0 commit comments