New initializes and returns a new Cache.
(cfg *rest.Config, opts Options)
| 425 | |
| 426 | // New initializes and returns a new Cache. |
| 427 | func New(cfg *rest.Config, opts Options) (Cache, error) { |
| 428 | opts, err := defaultOpts(cfg, opts) |
| 429 | if err != nil { |
| 430 | return nil, err |
| 431 | } |
| 432 | |
| 433 | newCacheFunc := newCache(cfg, opts) |
| 434 | |
| 435 | var defaultCache Cache |
| 436 | if len(opts.DefaultNamespaces) > 0 { |
| 437 | defaultConfig := optionDefaultsToConfig(&opts) |
| 438 | defaultCache = newMultiNamespaceCache(newCacheFunc, opts.Scheme, opts.Mapper, opts.DefaultNamespaces, &defaultConfig) |
| 439 | } else { |
| 440 | defaultCache = newCacheFunc(optionDefaultsToConfig(&opts), corev1.NamespaceAll) |
| 441 | } |
| 442 | |
| 443 | if len(opts.ByObject) == 0 { |
| 444 | return defaultCache, nil |
| 445 | } |
| 446 | |
| 447 | delegating := &delegatingByGVKCache{ |
| 448 | scheme: opts.Scheme, |
| 449 | caches: make(map[schema.GroupVersionKind]Cache, len(opts.ByObject)), |
| 450 | defaultCache: defaultCache, |
| 451 | } |
| 452 | |
| 453 | for obj, config := range opts.ByObject { |
| 454 | gvk, err := apiutil.GVKForObject(obj, opts.Scheme) |
| 455 | if err != nil { |
| 456 | return nil, fmt.Errorf("failed to get GVK for type %T: %w", obj, err) |
| 457 | } |
| 458 | var cache Cache |
| 459 | if len(config.Namespaces) > 0 { |
| 460 | cache = newMultiNamespaceCache(newCacheFunc, opts.Scheme, opts.Mapper, config.Namespaces, nil) |
| 461 | } else { |
| 462 | cache = newCacheFunc(byObjectToConfig(config), corev1.NamespaceAll) |
| 463 | } |
| 464 | delegating.caches[gvk] = cache |
| 465 | } |
| 466 | |
| 467 | return delegating, nil |
| 468 | } |
| 469 | |
| 470 | // TransformStripManagedFields strips the managed fields of an object before it is committed to the cache. |
| 471 | // If you are not explicitly accessing managedFields from your code, setting this as `DefaultTransform` |