From 30743b8195fbc753b668eba1d239b8492bff2235 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Tue, 13 Jan 2015 18:51:55 -0500 Subject: [PATCH] The get command should properly handle users providing invalid apiversions Add more guards against bad behavior. A follow up commit will ensure that when the flags for api-version are specified, they default to the correct values that client.SetKubernetesDefaults would use. --- pkg/kubectl/cmd/get.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index bc9f0d1579b..de43cd32f95 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -21,6 +21,7 @@ import ( "io" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource" @@ -126,7 +127,15 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) { if generic { // the outermost object will be converted to the output-version - printer = kubectl.NewVersionedPrinter(printer, api.Scheme, outputVersion(cmd)) + version := outputVersion(cmd) + if len(version) == 0 { + // TODO: add a new ResourceBuilder mode for Object() that attempts to ensure the objects + // are in the appropriate version if one exists (and if not, use the best effort). + // TODO: ensure api-version is set with the default preferred api version by the client + // builder on initialization + version = latest.Version + } + printer = kubectl.NewVersionedPrinter(printer, api.Scheme, version) obj, err := b.Flatten().Do().Object() checkErr(err) @@ -174,7 +183,14 @@ func printerForMapping(f *Factory, cmd *cobra.Command, mapping *meta.RESTMapping return nil, err } if ok { - printer = kubectl.NewVersionedPrinter(printer, mapping.ObjectConvertor, outputVersion(cmd)) + version := outputVersion(cmd) + if len(version) == 0 { + version = mapping.APIVersion + } + if len(version) == 0 { + return nil, fmt.Errorf("you must specify an output-version when using this output format") + } + printer = kubectl.NewVersionedPrinter(printer, mapping.ObjectConvertor, version) } else { printer, err = f.Printer(cmd, mapping, GetFlagBool(cmd, "no-headers")) if err != nil {