Skip to content

Commit 530dde0

Browse files
k8s-infra-cherrypick-robottenstadalvaroaleman
authored
[release-0.15] 🐛 fix unspecified KindsFor version (#2347)
* Fix unspecified KindsFor version * Dont allocate empty list Co-authored-by: Alvaro Aleman <[email protected]> * Test all four relevant interface methods, and do not assert count * Attempt to format correctly in web editor * Remove request count asserts * Remove non-version item asserts --------- Co-authored-by: Amund Tenstad <[email protected]> Co-authored-by: Alvaro Aleman <[email protected]>
1 parent 1a82503 commit 530dde0

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

pkg/client/apiutil/restmapper.go

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ func (m *mapper) getMapper() meta.RESTMapper {
152152
// addKnownGroupAndReload reloads the mapper with updated information about missing API group.
153153
// versions can be specified for partial updates, for instance for v1beta1 version only.
154154
func (m *mapper) addKnownGroupAndReload(groupName string, versions ...string) error {
155+
// versions will here be [""] if the forwarded Version value of
156+
// GroupVersionResource (in calling method) was not specified.
157+
if len(versions) == 1 && versions[0] == "" {
158+
versions = nil
159+
}
160+
155161
// If no specific versions are set by user, we will scan all available ones for the API group.
156162
// This operation requires 2 requests: /api and /apis, but only once. For all subsequent calls
157163
// this data will be taken from cache.

pkg/client/apiutil/restmapper_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,34 @@ func TestLazyRestMapperProvider(t *testing.T) {
404404
g.Expect(crt.GetRequestCount()).To(gmg.Equal(6))
405405
})
406406

407+
t.Run("LazyRESTMapper should work correctly if the version isn't specified", func(t *testing.T) {
408+
g := gmg.NewWithT(t)
409+
410+
httpClient, err := rest.HTTPClientFor(restCfg)
411+
g.Expect(err).NotTo(gmg.HaveOccurred())
412+
413+
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient)
414+
g.Expect(err).NotTo(gmg.HaveOccurred())
415+
416+
kind, err := lazyRestMapper.KindFor(schema.GroupVersionResource{Group: "networking.k8s.io", Resource: "ingress"})
417+
g.Expect(err).NotTo(gmg.HaveOccurred())
418+
g.Expect(kind.Version).ToNot(gmg.BeEmpty())
419+
420+
kinds, err := lazyRestMapper.KindsFor(schema.GroupVersionResource{Group: "authentication.k8s.io", Resource: "tokenreviews"})
421+
g.Expect(err).NotTo(gmg.HaveOccurred())
422+
g.Expect(kinds).ToNot(gmg.BeEmpty())
423+
g.Expect(kinds[0].Version).ToNot(gmg.BeEmpty())
424+
425+
resorce, err := lazyRestMapper.ResourceFor(schema.GroupVersionResource{Group: "scheduling.k8s.io", Resource: "priorityclasses"})
426+
g.Expect(err).NotTo(gmg.HaveOccurred())
427+
g.Expect(resorce.Version).ToNot(gmg.BeEmpty())
428+
429+
resorces, err := lazyRestMapper.ResourcesFor(schema.GroupVersionResource{Group: "policy", Resource: "poddisruptionbudgets"})
430+
g.Expect(err).NotTo(gmg.HaveOccurred())
431+
g.Expect(kinds).ToNot(gmg.BeEmpty())
432+
g.Expect(resorces[0].Version).ToNot(gmg.BeEmpty())
433+
})
434+
407435
t.Run("LazyRESTMapper can fetch CRDs if they were created at runtime", func(t *testing.T) {
408436
g := gmg.NewWithT(t)
409437

0 commit comments

Comments
 (0)