Skip to content

RESTMapper addKnownGroupAndReload fails when calling method's resource has unspecified Version #2345

Closed
@tenstad

Description

@tenstad

Problem

When calling KindFor, KindsFor, ResourceFor, or ResourcesFor without specifying Version in resource schema.GroupVersionResource, the list of versions forwarded to addKnownGroupAndReload is [""]. addKnownGroupAndReload does not handle empty resources well, and fails with:

Unexpected error:
    <*fmt.wrapError | 0xc0000b6300>:
    failed to get API group resources: unable to retrieve the complete list of server APIs: autoscaling/: converting (v1.APIGroup) to (v1.APIResourceList): unknown conversion
    {
        msg: "failed to get API group resources: unable to retrieve the complete list of server APIs: autoscaling/: converting (v1.APIGroup) to (v1.APIResourceList): unknown conversion",
        err: <*discovery.ErrGroupDiscoveryFailed | 0xc0005120f8>{
            Groups: {
                {Group: "autoscaling", Version: ""}: <*errors.errorString | 0xc00039a4d0>{
                    s: "converting (v1.APIGroup) to (v1.APIResourceList): unknown conversion",
                },
            },
        },
    }

Test case

t.Run("KindsFor succeeds without specified 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())

	kinds, err := lazyRestMapper.KindsFor(schema.GroupVersionResource{Group: "autoscaling", Resource: "horizontalpodautoscaler"})
	g.Expect(err).NotTo(gmg.HaveOccurred())
	g.Expect(len(kinds)).To(gmg.Equal(2))
	g.Expect(kinds[0].Kind).To(gmg.Equal("HorizontalPodAutoscaler"))
	g.Expect(kinds[1].Kind).To(gmg.Equal("HorizontalPodAutoscaler"))
})

Suggested Solution

Clear the version list in addKnownGroupAndReload if "" is the only value:

func (m *mapper) addKnownGroupAndReload(groupName string, versions ...string) error {
	// versions will here be [""] if the forwarded Version value of
	// GroupVersionResource (in calling method) was not specified.
	if len(versions) == 1 && versions[0] == "" {
		versions = []string{}
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.kind/regressionCategorizes issue or PR as related to a regression from a prior release.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions