From 177dcb998fe7a891f25fc8e2f117a845d6b6063a Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Thu, 8 Mar 2018 12:13:38 -0500 Subject: [PATCH] match KindFor first --- pkg/kubectl/resource/builder.go | 39 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/pkg/kubectl/resource/builder.go b/pkg/kubectl/resource/builder.go index b2385d7d271..5c48c44fc3d 100644 --- a/pkg/kubectl/resource/builder.go +++ b/pkg/kubectl/resource/builder.go @@ -599,9 +599,21 @@ func (b *Builder) SingleResourceType() *Builder { } // mappingFor returns the RESTMapping for the Kind given, or the Kind referenced by the resource. -// prefers a fully specified GroupVersionKind match. If we don't have one, match on a fully specified -// GroupVersionResource, or fallback to a match on GroupResource. +// Prefers a fully specified GroupVersionResource match. If one is not found, we match on a fully +// specified GroupVersionKind, or fallback to a match on GroupKind. func (b *Builder) mappingFor(resourceOrKindArg string) (*meta.RESTMapping, error) { + fullySpecifiedGVR, groupResource := schema.ParseResourceArg(resourceOrKindArg) + gvk := schema.GroupVersionKind{} + if fullySpecifiedGVR != nil { + gvk, _ = b.mapper.KindFor(*fullySpecifiedGVR) + } + if gvk.Empty() { + gvk, _ = b.mapper.KindFor(groupResource.WithVersion("")) + } + if !gvk.Empty() { + return b.mapper.RESTMapping(gvk.GroupKind(), gvk.Version) + } + fullySpecifiedGVK, groupKind := schema.ParseKindArg(resourceOrKindArg) if fullySpecifiedGVK == nil { gvk := groupKind.WithVersion("") @@ -611,27 +623,18 @@ func (b *Builder) mappingFor(resourceOrKindArg string) (*meta.RESTMapping, error if !fullySpecifiedGVK.Empty() { if mapping, err := b.mapper.RESTMapping(fullySpecifiedGVK.GroupKind(), fullySpecifiedGVK.Version); err == nil { return mapping, nil - } else { - if mapping, err := b.mapper.RESTMapping(groupKind, ""); err == nil { - return mapping, nil - } } } - fullySpecifiedGVR, groupResource := schema.ParseResourceArg(resourceOrKindArg) - gvk := schema.GroupVersionKind{} - if fullySpecifiedGVR != nil { - gvk, _ = b.mapper.KindFor(*fullySpecifiedGVR) - } - if gvk.Empty() { - var err error - gvk, err = b.mapper.KindFor(groupResource.WithVersion("")) - if err != nil { - return nil, err - } + mapping, err := b.mapper.RESTMapping(groupKind, gvk.Version) + if err != nil { + // if we error out here, it is because we could not match a resource or a kind + // for the given argument. To maintain consistency with previous behavior, + // announce that a resource type could not be found. + return nil, fmt.Errorf("the server doesn't have a resource type %q", groupResource.Resource) } - return b.mapper.RESTMapping(gvk.GroupKind(), gvk.Version) + return mapping, nil } func (b *Builder) resourceMappings() ([]*meta.RESTMapping, error) {