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

馃悰 Fix fetching all LazyRESTMapper versions after fetching single version #2350

Closed
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
18 changes: 18 additions & 0 deletions pkg/client/apiutil/restmapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func (m *mapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVers

// KindsFor implements Mapper.KindsFor.
func (m *mapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
if resource.Version == "" {
if err := m.addKnownGroupAndReload(resource.Group, resource.Version); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a bit over eager and removing the "lazy" aspect if we always reload as soon as we get a request without version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, can be a bit over eager, but is it possible to fetch less and at the same time return the complete list?

Or is this by design, and #2349 not actually a bug?

return nil, err
}
}

res, err := m.getMapper().KindsFor(resource)
if meta.IsNoMatchError(err) {
if err := m.addKnownGroupAndReload(resource.Group, resource.Version); err != nil {
Expand All @@ -101,6 +107,12 @@ func (m *mapper) ResourceFor(input schema.GroupVersionResource) (schema.GroupVer

// ResourcesFor implements Mapper.ResourcesFor.
func (m *mapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
if input.Version == "" {
if err := m.addKnownGroupAndReload(input.Group, input.Version); err != nil {
return nil, err
}
}

res, err := m.getMapper().ResourcesFor(input)
if meta.IsNoMatchError(err) {
if err := m.addKnownGroupAndReload(input.Group, input.Version); err != nil {
Expand All @@ -127,6 +139,12 @@ func (m *mapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RES

// RESTMappings implements Mapper.RESTMappings.
func (m *mapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
if len(versions) == 0 {
if err := m.addKnownGroupAndReload(gk.Group, versions...); err != nil {
return nil, err
}
}

res, err := m.getMapper().RESTMappings(gk, versions...)
if meta.IsNoMatchError(err) {
if err := m.addKnownGroupAndReload(gk.Group, versions...); err != nil {
Expand Down
31 changes: 31 additions & 0 deletions pkg/client/apiutil/restmapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,37 @@ func TestLazyRestMapperProvider(t *testing.T) {
g.Expect(crt.GetRequestCount()).To(gmg.Equal(4))
})

t.Run("LazyRESTMapper should trigger addKnownGroupAndReload when fetching multiple items and input is partial", func(t *testing.T) {
g := gmg.NewWithT(t)

httpClient, err := rest.HTTPClientFor(restCfg)
g.Expect(err).NotTo(gmg.HaveOccurred())

lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient)
g.Expect(err).NotTo(gmg.HaveOccurred())

_, err = lazyRestMapper.RESTMapping(schema.GroupKind{Group: "crew.example.com", Kind: "driver"}, "v1")
g.Expect(err).NotTo(gmg.HaveOccurred())

mappings, err := lazyRestMapper.RESTMappings(schema.GroupKind{Group: "crew.example.com", Kind: "driver"})
g.Expect(err).NotTo(gmg.HaveOccurred())
g.Expect(len(mappings)).To(gmg.BeNumerically(">", 1))

_, err = lazyRestMapper.RESTMapping(schema.GroupKind{Group: "autoscaling", Kind: "horizontalpodautoscaler"}, "v1")
g.Expect(err).NotTo(gmg.HaveOccurred())

kinds, err := lazyRestMapper.KindsFor(schema.GroupVersionResource{Group: "autoscaling", Resource: "horizontalpodautoscaler"})
g.Expect(err).NotTo(gmg.HaveOccurred())
g.Expect(len(kinds)).To(gmg.BeNumerically(">", 1))

_, err = lazyRestMapper.RESTMapping(schema.GroupKind{Group: "flowcontrol.apiserver.k8s.io", Kind: "prioritylevelconfiguration"}, "v1beta2")
g.Expect(err).NotTo(gmg.HaveOccurred())

resources, err := lazyRestMapper.ResourcesFor(schema.GroupVersionResource{Group: "flowcontrol.apiserver.k8s.io", Resource: "prioritylevelconfiguration"})
g.Expect(err).NotTo(gmg.HaveOccurred())
g.Expect(len(resources)).To(gmg.BeNumerically(">", 1))
})

t.Run("LazyRESTMapper should work correctly with multiple API group versions", func(t *testing.T) {
g := gmg.NewWithT(t)

Expand Down