kubernetes-sigs / kubernetes-sigs/controller-runtime

LazyRESTMapper KindsFor does not return all versions when single version is previously queried

Open
#2,349 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

lifecycle/frozen needs-triage
Dominant language
Go
Stars
3k
Forks
1.3k
Avg merge
2d 5h
Merged PRs (30d)
14

Description

Problem

addKnownGroupAndReload is only triggered when no matches are found. If priming the mapper with a single version and then requesting all versions (not specifying version), a match is found and only a single version returned (not multiple versions).

func (m *mapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
	res, err := m.getMapper().KindsFor(resource)
	if meta.IsNoMatchError(err) {
		if err := m.addKnownGroupAndReload(resource.Group, resource.Version); err != nil {

Test Case

The test passes if lazyRestMapper.KindsFor(schema.GroupVersionResource{Group: "autoscaling", Version: "v1", Resource: "horizontalpodautoscaler"}) is removed, thus making the other KindsFor trigger a addKnownGroupAndReload.

t.Run("LazyRESTMapper should be able to fetch all versions after fetching single 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())

	_, err = lazyRestMapper.KindsFor(schema.GroupVersionResource{Group: "autoscaling", Version: "v1", Resource: "horizontalpodautoscaler"})
	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.BeNumerically(">", 1))
})

Suggestion

Trigger addKnownGroupAndReload when schema.GroupVersionResource input is partial, and some fields are unspecified.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lazyRestMapper.KindsFor and the addKnownGroupAndReload path shown in the issue, then run the supplied regression test for autoscaling horizontalpodautoscaler. Verify that querying v1 first and then querying without a version returns more than one kind version.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.