Skip to content

Commit a495593

Browse files
committed
cache: clone maps to prevent data race when concurrently creating caches using the same options
1 parent 2aa9459 commit a495593

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

pkg/cache/cache.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
469469
}
470470
}
471471

472+
opts.ByObject = maps.Clone(opts.ByObject)
473+
opts.DefaultNamespaces = maps.Clone(opts.DefaultNamespaces)
472474
for obj, byObject := range opts.ByObject {
473475
isNamespaced, err := apiutil.IsObjectNamespaced(obj, opts.Scheme, opts.Mapper)
474476
if err != nil {
@@ -480,14 +482,15 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
480482

481483
if isNamespaced && byObject.Namespaces == nil {
482484
byObject.Namespaces = maps.Clone(opts.DefaultNamespaces)
485+
} else {
486+
byObject.Namespaces = maps.Clone(byObject.Namespaces)
483487
}
484488

485489
// Default the namespace-level configs first, because they need to use the undefaulted type-level config
486490
// to be able to potentially fall through to settings from DefaultNamespaces.
487491
for namespace, config := range byObject.Namespaces {
488492
// 1. Default from the undefaulted type-level config
489493
config = defaultConfig(config, byObjectToConfig(byObject))
490-
491494
// 2. Default from the namespace-level config. This was defaulted from the global default config earlier, but
492495
// might not have an entry for the current namespace.
493496
if defaultNamespaceSettings, hasDefaultNamespace := opts.DefaultNamespaces[namespace]; hasDefaultNamespace {

0 commit comments

Comments
 (0)