MCPcopy
hub / github.com/kubernetes-sigs/controller-runtime / New

Function New

pkg/cache/cache.go:427–468  ·  view source on GitHub ↗

New initializes and returns a new Cache.

(cfg *rest.Config, opts Options)

Source from the content-addressed store, hash-verified

425
426// New initializes and returns a new Cache.
427func 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`

Callers 9

cache_test.goFile · 0.92
CacheTestFunction · 0.92
controller_test.goFile · 0.92
newNonTypedOnlyCacheFunction · 0.92

Calls 7

GVKForObjectFunction · 0.92
defaultOptsFunction · 0.85
newCacheFunction · 0.85
optionDefaultsToConfigFunction · 0.85
newMultiNamespaceCacheFunction · 0.85
newCacheFuncFuncType · 0.85
byObjectToConfigFunction · 0.85

Tested by 5

CacheTestFunction · 0.74
newNonTypedOnlyCacheFunction · 0.74