Closed
Description
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{}
}