Skip to content

Commit

Permalink
cache: clone maps to prevent data race when concurrently creating cac…
Browse files Browse the repository at this point in the history
…hes using the same options
  • Loading branch information
chrischdi committed Jan 20, 2025
1 parent 2aa9459 commit a495593
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
}
}

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

if isNamespaced && byObject.Namespaces == nil {
byObject.Namespaces = maps.Clone(opts.DefaultNamespaces)
} else {
byObject.Namespaces = maps.Clone(byObject.Namespaces)
}

// Default the namespace-level configs first, because they need to use the undefaulted type-level config
// to be able to potentially fall through to settings from DefaultNamespaces.
for namespace, config := range byObject.Namespaces {
// 1. Default from the undefaulted type-level config
config = defaultConfig(config, byObjectToConfig(byObject))

// 2. Default from the namespace-level config. This was defaulted from the global default config earlier, but
// might not have an entry for the current namespace.
if defaultNamespaceSettings, hasDefaultNamespace := opts.DefaultNamespaces[namespace]; hasDefaultNamespace {
Expand Down

0 comments on commit a495593

Please sign in to comment.