Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-0.14] 馃悰 inherited defaults not respected in cache BuilderWithOptions #2491

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,32 @@ func New(config *rest.Config, opts Options) (Cache, error) {
// returned from cache get/list before mutating it.
func BuilderWithOptions(options Options) NewCacheFunc {
return func(config *rest.Config, inherited Options) (Cache, error) {
var err error
inherited, err = defaultOpts(config, inherited)
if err != nil {
return nil, err
}
options, err = defaultOpts(config, options)
if err != nil {
return nil, err
}
combined, err := options.inheritFrom(inherited)
combined, err := options.combinedOpts(config, inherited)
if err != nil {
return nil, err
}
return New(config, *combined)
}
}

func (options Options) combinedOpts(config *rest.Config, inherited Options) (*Options, error) {
var err error
inherited, err = defaultOpts(config, inherited)
if err != nil {
return nil, err
}
options = defaultToInheritedOpts(options, inherited)
options, err = defaultOpts(config, options)
if err != nil {
return nil, err
}
combined, err := options.inheritFrom(inherited)
if err != nil {
return nil, err
}
return combined, nil
}

func (options Options) inheritFrom(inherited Options) (*Options, error) {
var (
combined Options
Expand Down Expand Up @@ -424,6 +433,21 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
return opts, nil
}

func defaultToInheritedOpts(opts, inherited Options) Options {
if opts.Scheme == nil {
opts.Scheme = inherited.Scheme
}

if opts.Mapper == nil {
opts.Mapper = inherited.Mapper
}

if opts.Resync == nil {
opts.Resync = inherited.Resync
}
return opts
}

func convertToByGVK[T any](byObject map[client.Object]T, def T, scheme *runtime.Scheme) (map[schema.GroupVersionKind]T, error) {
byGVK := map[schema.GroupVersionKind]T{}
for object, value := range byObject {
Expand Down
15 changes: 15 additions & 0 deletions pkg/cache/cache_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ var _ = Describe("cache.inheritFrom", func() {
Expect(checkError(specified.inheritFrom(inherited)).Scheme.AllKnownTypes()).To(HaveLen(2))
})
})
Context("Post defaulting of specified", func() {
It("inherited not lost", func() {
inherited.Scheme = runtime.NewScheme()
inherited.Scheme.AddKnownTypes(gv, &unstructured.Unstructured{})
Expect(inherited.Scheme.KnownTypes(gv)).To(HaveLen(1))
inherited.Mapper = meta.NewDefaultRESTMapper([]schema.GroupVersion{gv})
inherited.Resync = pointer.Duration(time.Minute)

combined := checkError(specified.combinedOpts(nil, inherited))
Expect(combined.Mapper).To(Equal(inherited.Mapper))
Expect(combined.Resync).To(Equal(inherited.Resync))
Expect(combined.Scheme).NotTo(BeNil())
Expect(combined.Scheme.KnownTypes(gv)).To(HaveLen(1))
})
})
Context("Mapper", func() {
It("is nil when specified and inherited are unset", func() {
Expect(checkError(specified.inheritFrom(inherited)).Mapper).To(BeNil())
Expand Down