Skip to content

Commit

Permalink
[release-0.15] 🐛 fix unspecified KindsFor version (#2347)
Browse files Browse the repository at this point in the history
* Fix unspecified KindsFor version

* Dont allocate empty list

Co-authored-by: Alvaro Aleman <alvaroaleman@users.noreply.github.com>

* 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 <github@amund.io>
Co-authored-by: Alvaro Aleman <alvaroaleman@users.noreply.github.com>
  • Loading branch information
3 people authored May 24, 2023
1 parent 1a82503 commit 530dde0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/client/apiutil/restmapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func (m *mapper) getMapper() meta.RESTMapper {
// addKnownGroupAndReload reloads the mapper with updated information about missing API group.
// versions can be specified for partial updates, for instance for v1beta1 version only.
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 = nil
}

// If no specific versions are set by user, we will scan all available ones for the API group.
// This operation requires 2 requests: /api and /apis, but only once. For all subsequent calls
// this data will be taken from cache.
Expand Down
28 changes: 28 additions & 0 deletions pkg/client/apiutil/restmapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,34 @@ func TestLazyRestMapperProvider(t *testing.T) {
g.Expect(crt.GetRequestCount()).To(gmg.Equal(6))
})

t.Run("LazyRESTMapper should work correctly if the version isn't specified", 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())

kind, err := lazyRestMapper.KindFor(schema.GroupVersionResource{Group: "networking.k8s.io", Resource: "ingress"})
g.Expect(err).NotTo(gmg.HaveOccurred())
g.Expect(kind.Version).ToNot(gmg.BeEmpty())

kinds, err := lazyRestMapper.KindsFor(schema.GroupVersionResource{Group: "authentication.k8s.io", Resource: "tokenreviews"})
g.Expect(err).NotTo(gmg.HaveOccurred())
g.Expect(kinds).ToNot(gmg.BeEmpty())
g.Expect(kinds[0].Version).ToNot(gmg.BeEmpty())

resorce, err := lazyRestMapper.ResourceFor(schema.GroupVersionResource{Group: "scheduling.k8s.io", Resource: "priorityclasses"})
g.Expect(err).NotTo(gmg.HaveOccurred())
g.Expect(resorce.Version).ToNot(gmg.BeEmpty())

resorces, err := lazyRestMapper.ResourcesFor(schema.GroupVersionResource{Group: "policy", Resource: "poddisruptionbudgets"})
g.Expect(err).NotTo(gmg.HaveOccurred())
g.Expect(kinds).ToNot(gmg.BeEmpty())
g.Expect(resorces[0].Version).ToNot(gmg.BeEmpty())
})

t.Run("LazyRESTMapper can fetch CRDs if they were created at runtime", func(t *testing.T) {
g := gmg.NewWithT(t)

Expand Down

0 comments on commit 530dde0

Please sign in to comment.