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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tenstad opened this issue May 24, 2023 · 1 comment · Fixed by #2346
Labels
kind/bug Categorizes issue or PR as related to a bug. kind/regression Categorizes issue or PR as related to a regression from a prior release.

Comments

@tenstad
Copy link
Contributor

tenstad commented May 24, 2023

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{}
	}
@erikgb
Copy link
Contributor

erikgb commented May 24, 2023

/kind bug
/kind regression

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. kind/regression Categorizes issue or PR as related to a regression from a prior release. labels May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. kind/regression Categorizes issue or PR as related to a regression from a prior release.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants