attempt to use discovery RESTMapper and fallback if we can't

This commit is contained in:
deads2k 2016-10-14 16:06:31 -04:00
parent 928b8cbdb8
commit abf2c43ad7

View File

@ -32,7 +32,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/blang/semver"
"github.com/emicklei/go-restful/swagger" "github.com/emicklei/go-restful/swagger"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -334,10 +333,7 @@ func (f *factory) Object() (meta.RESTMapper, runtime.ObjectTyper) {
mapper := registered.RESTMapper() mapper := registered.RESTMapper()
discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg) discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
// if we can find the server version and it's current enough to have discovery information, use it. Otherwise,
// fallback to our hardcoded list
if err == nil { if err == nil {
if serverVersion, err := discoveryClient.ServerVersion(); err == nil && useDiscoveryRESTMapper(serverVersion.GitVersion) {
// register third party resources with the api machinery groups. This probably should be done, but // register third party resources with the api machinery groups. This probably should be done, but
// its consistent with old code, so we'll start with it. // its consistent with old code, so we'll start with it.
if err := registerThirdPartyResources(discoveryClient); err != nil { if err := registerThirdPartyResources(discoveryClient); err != nil {
@ -351,11 +347,11 @@ func (f *factory) Object() (meta.RESTMapper, runtime.ObjectTyper) {
mapper = meta.FirstHitRESTMapper{ mapper = meta.FirstHitRESTMapper{
MultiRESTMapper: meta.MultiRESTMapper{ MultiRESTMapper: meta.MultiRESTMapper{
discovery.NewDeferredDiscoveryRESTMapper(discoveryClient, registered.InterfacesFor), discovery.NewDeferredDiscoveryRESTMapper(discoveryClient, registered.InterfacesFor),
thirdPartyResourceDataMapper, thirdPartyResourceDataMapper, // needed for TPR printing
registered.RESTMapper(), // hardcoded fall back
}, },
} }
} }
}
// wrap with shortcuts // wrap with shortcuts
mapper = NewShortcutExpander(mapper, discoveryClient) mapper = NewShortcutExpander(mapper, discoveryClient)
@ -1332,20 +1328,6 @@ func (f *factory) NewBuilder() *resource.Builder {
return resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)) return resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true))
} }
// useDiscoveryRESTMapper checks the server version to see if its recent enough to have
// enough discovery information available to reliably build a RESTMapper. If not, use the
// hardcoded mapper in this client (legacy behavior)
func useDiscoveryRESTMapper(serverVersion string) bool {
if len(serverVersion) == 0 {
return false
}
serverSemVer, err := semver.Parse(serverVersion[1:])
if err != nil {
return false
}
return serverSemVer.GE(semver.MustParse("1.3.0"))
}
// registerThirdPartyResources inspects the discovery endpoint to find thirdpartyresources in the discovery doc // registerThirdPartyResources inspects the discovery endpoint to find thirdpartyresources in the discovery doc
// and then registers them with the apimachinery code. I think this is done so that scheme/codec stuff works, // and then registers them with the apimachinery code. I think this is done so that scheme/codec stuff works,
// but I really don't know. Feels like this code should go away once kubectl is completely generic for generic // but I really don't know. Feels like this code should go away once kubectl is completely generic for generic