From d1ec1b363daedb12c5fd90945235c1138660f81e Mon Sep 17 00:00:00 2001 From: Solly Ross Date: Fri, 9 Dec 2016 16:23:58 -0500 Subject: [PATCH] Priority REST Mapper: Actually honor user choice 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. --- pkg/api/meta/priority.go | 6 +++--- pkg/api/meta/priority_test.go | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/pkg/api/meta/priority.go b/pkg/api/meta/priority.go index f99273b6499..b9dca1a5eeb 100644 --- a/pkg/api/meta/priority.go +++ b/pkg/api/meta/priority.go @@ -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)) } diff --git a/pkg/api/meta/priority_test.go b/pkg/api/meta/priority_test.go index 70b93042489..45229eee214 100644 --- a/pkg/api/meta/priority_test.go +++ b/pkg/api/meta/priority_test.go @@ -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) + } +}