Skip to content

LazyRESTMapper KindsFor does not return all versions when single version is previously queried #2349

@tenstad

Description

@tenstad

Problem

addKnownGroupAndReload is only triggered when no matches are found. If priming the mapper with a single version and then requesting all versions (not specifying version), a match is found and only a single version returned (not multiple versions).

func (m *mapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
	res, err := m.getMapper().KindsFor(resource)
	if meta.IsNoMatchError(err) {
		if err := m.addKnownGroupAndReload(resource.Group, resource.Version); err != nil {

Test Case

The test passes if lazyRestMapper.KindsFor(schema.GroupVersionResource{Group: "autoscaling", Version: "v1", Resource: "horizontalpodautoscaler"}) is removed, thus making the other KindsFor trigger a addKnownGroupAndReload.

t.Run("LazyRESTMapper should be able to fetch all versions after fetching single version", 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.KindsFor(schema.GroupVersionResource{Group: "autoscaling", Version: "v1", Resource: "horizontalpodautoscaler"})
	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))
})

Suggestion

Trigger addKnownGroupAndReload when schema.GroupVersionResource input is partial, and some fields are unspecified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    lifecycle/frozenIndicates that an issue or PR should not be auto-closed due to staleness.needs-triageIndicates an issue or PR lacks a `triage/foo` label and requires one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions