From a0a69a35830e60ef9f87ddf3f13391c4c3e39077 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 19 Dec 2017 15:37:21 +0800 Subject: [PATCH 1/2] Add more validate conditions when run kubectl get with --raw --- pkg/kubectl/cmd/resource/get.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/kubectl/cmd/resource/get.go b/pkg/kubectl/cmd/resource/get.go index 4013cb42874..4e830342af4 100644 --- a/pkg/kubectl/cmd/resource/get.go +++ b/pkg/kubectl/cmd/resource/get.go @@ -25,6 +25,8 @@ import ( "github.com/golang/glog" "github.com/spf13/cobra" + "net/url" + kapierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -213,6 +215,12 @@ func (options *GetOptions) Validate(cmd *cobra.Command) error { if len(options.Raw) > 0 && (options.Watch || options.WatchOnly || len(options.LabelSelector) > 0 || options.Export) { return fmt.Errorf("--raw may not be specified with other flags that filter the server request or alter the output") } + if len(cmdutil.GetFlagString(cmd, "output")) > 0 { + return cmdutil.UsageErrorf(cmd, "--raw and --output are mutually exclusive") + } + if _, err := url.ParseRequestURI(options.Raw); err != nil { + return cmdutil.UsageErrorf(cmd, "--raw must be a valid URL path: %v", err) + } if cmdutil.GetFlagBool(cmd, "show-labels") { outputOption := cmd.Flags().Lookup("output").Value.String() if outputOption != "" && outputOption != "wide" { From 8a7f8bc0462afb34a53542ce1deb4256e644bb0b Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 19 Dec 2017 17:51:30 +0800 Subject: [PATCH 2/2] Move output and url checks under raw flag condition --- pkg/kubectl/cmd/resource/get.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/kubectl/cmd/resource/get.go b/pkg/kubectl/cmd/resource/get.go index 4e830342af4..6e928a65445 100644 --- a/pkg/kubectl/cmd/resource/get.go +++ b/pkg/kubectl/cmd/resource/get.go @@ -212,14 +212,16 @@ func (options *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args // Validate checks the set of flags provided by the user. func (options *GetOptions) Validate(cmd *cobra.Command) error { - if len(options.Raw) > 0 && (options.Watch || options.WatchOnly || len(options.LabelSelector) > 0 || options.Export) { - return fmt.Errorf("--raw may not be specified with other flags that filter the server request or alter the output") - } - if len(cmdutil.GetFlagString(cmd, "output")) > 0 { - return cmdutil.UsageErrorf(cmd, "--raw and --output are mutually exclusive") - } - if _, err := url.ParseRequestURI(options.Raw); err != nil { - return cmdutil.UsageErrorf(cmd, "--raw must be a valid URL path: %v", err) + if len(options.Raw) > 0 { + if options.Watch || options.WatchOnly || len(options.LabelSelector) > 0 || options.Export { + return fmt.Errorf("--raw may not be specified with other flags that filter the server request or alter the output") + } + if len(cmdutil.GetFlagString(cmd, "output")) > 0 { + return cmdutil.UsageErrorf(cmd, "--raw and --output are mutually exclusive") + } + if _, err := url.ParseRequestURI(options.Raw); err != nil { + return cmdutil.UsageErrorf(cmd, "--raw must be a valid URL path: %v", err) + } } if cmdutil.GetFlagBool(cmd, "show-labels") { outputOption := cmd.Flags().Lookup("output").Value.String()