Commit e338afbc authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #38533 from DirectXMan12/bug/priority-restmapper-versions

Automatic merge from submit-queue (batch tested with PRs 38720, 38533) Priority REST Mapper: Actually honor user choice ```release-note Fixes bug in resolving client-requested API versions ``` RESTMapping takes a desired GroupKind, and a set of versions, and returns a rest mapper for the first matching version. It also has a list of built-in discovered prioritized versions, to which it appends the user versions. However, when it goes to parse the versions, it parses them as GroupVersions. Since only a version was passed, the group will be the empty group (""), which will only match rest mappings for the empty group, ergo, none of the user's versions will match if they are attempting a match for a non-emtpy-group GroupKind. This fixes that by taking the parsed GroupVersion, and overriding the Group with the Group from the passed-in GroupKind.
parents 11c1cd87 d1ec1b36
......@@ -163,9 +163,9 @@ func (m PriorityRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string)
if len(versions) > 0 {
priorities = make([]schema.GroupVersionKind, 0, len(m.KindPriority)+len(versions))
for _, version := range versions {
gv, err := schema.ParseGroupVersion(version)
if err != nil {
return nil, err
gv := schema.GroupVersion{
Version: version,
Group: gk.Group,
}
priorities = append(priorities, gv.WithKind(AnyKind))
}
......
......@@ -307,3 +307,40 @@ func TestPriorityRESTMapperRESTMapping(t *testing.T) {
}
}
}
func TestPriorityRESTMapperRESTMappingHonorsUserVersion(t *testing.T) {
mappingV2alpha1 := &RESTMapping{
GroupVersionKind: schema.GroupVersionKind{Group: "Bar", Kind: "Foo", Version: "v2alpha1"},
}
mappingV1 := &RESTMapping{
GroupVersionKind: schema.GroupVersionKind{Group: "Bar", Kind: "Foo", Version: "v1"},
}
allMappers := MultiRESTMapper{
fixedRESTMapper{mappings: []*RESTMapping{mappingV2alpha1}},
fixedRESTMapper{mappings: []*RESTMapping{mappingV1}},
}
mapper := PriorityRESTMapper{
Delegate: allMappers,
KindPriority: []schema.GroupVersionKind{{Group: "Bar", Version: "v2alpha1", Kind: AnyKind}, {Group: "Bar", Version: AnyVersion, Kind: AnyKind}},
}
outMapping1, err := mapper.RESTMapping(schema.GroupKind{Group: "Bar", Kind: "Foo"}, "v1")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if outMapping1 != mappingV1 {
t.Errorf("asked for version %v, expected mapping for %v, got mapping for %v", "v1", mappingV1.GroupVersionKind, outMapping1.GroupVersionKind)
}
outMapping2, err := mapper.RESTMapping(schema.GroupKind{Group: "Bar", Kind: "Foo"}, "v2alpha1")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if outMapping2 != mappingV2alpha1 {
t.Errorf("asked for version %v, expected mapping for %v, got mapping for %v", "v2alpha1", mappingV2alpha1.GroupVersionKind, outMapping2.GroupVersionKind)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment