Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate between server error messages and client error messages in kubectl #5016

Merged
merged 1 commit into from
Mar 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/api/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ func IsServerTimeout(err error) bool {
return reasonForError(err) == api.StatusReasonServerTimeout
}

// IsStatusError determines if err is an API Status error received from the master.
func IsStatusError(err error) bool {
_, ok := err.(*StatusError)
return ok
}

// IsUnexpectedObjectError determines if err is due to an unexpected object from the master.
func IsUnexpectedObjectError(err error) bool {
_, ok := err.(*UnexpectedObjectError)
return ok
}

func reasonForError(err error) api.StatusReason {
switch t := err.(type) {
case *StatusError:
Expand Down
9 changes: 8 additions & 1 deletion pkg/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func (u *UnexpectedStatusError) Error() string {
return fmt.Sprintf("request [%+v] failed (%d) %s: %s", u.Request, u.Response.StatusCode, u.Response.Status, u.Body)
}

// IsUnexpectedStatusError determines if err is due to an unexpected status from the server.
func IsUnexpectedStatusError(err error) bool {
_, ok := err.(*UnexpectedStatusError)
return ok
}

// RequestConstructionError is returned when there's an error assembling a request.
type RequestConstructionError struct {
Err error
Expand Down Expand Up @@ -576,7 +582,8 @@ func (r *Request) transformResponse(body []byte, resp *http.Response, req *http.
// no-op, we've been upgraded
case resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusPartialContent:
if !isStatusResponse {
var err error = &UnexpectedStatusError{
var err error
err = &UnexpectedStatusError{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be changed? Just for my own education...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a style thing, I think. The pattern is to either declare a variable with var name type and assign to it later or let the compiler infer type by assigning with name :=. Doing both in the same statement is unnecessary.

Request: req,
Response: resp,
Body: string(body),
Expand Down
5 changes: 3 additions & 2 deletions pkg/kubectl/cmd/clusterinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"

"github.com/spf13/cobra"
Expand All @@ -41,12 +42,12 @@ func (f *Factory) NewCmdClusterInfo(out io.Writer) *cobra.Command {

func RunClusterInfo(factory *Factory, out io.Writer, cmd *cobra.Command) {
client, err := factory.ClientConfig(cmd)
checkErr(err)
util.CheckErr(err)
fmt.Fprintf(out, "Kubernetes master is running at %v\n", client.Host)

mapper, typer := factory.Object(cmd)
cmdNamespace, err := factory.DefaultNamespace(cmd)
checkErr(err)
util.CheckErr(err)

// TODO: use generalized labels once they are implemented (#341)
b := resource.NewBuilder(mapper, typer, factory.ClientMapperForCommand(cmd)).
Expand Down
10 changes: 2 additions & 8 deletions pkg/kubectl/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {

Object: func(cmd *cobra.Command) (meta.RESTMapper, runtime.ObjectTyper) {
cfg, err := clientConfig.ClientConfig()
checkErr(err)
cmdutil.CheckErr(err)
cmdApiVersion := cfg.Version

return kubectl.OutputVersionMapper{mapper, cmdApiVersion}, api.Scheme
Expand Down Expand Up @@ -253,7 +253,7 @@ func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMappin
}
if ok {
clientConfig, err := f.ClientConfig(cmd)
checkErr(err)
cmdutil.CheckErr(err)
defaultVersion := clientConfig.Version

version := cmdutil.OutputVersion(cmd, defaultVersion)
Expand Down Expand Up @@ -329,12 +329,6 @@ func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
return clientConfig
}

func checkErr(err error) {
if err != nil {
glog.FatalDepth(1, err.Error())
}
}

func usageError(cmd *cobra.Command, format string, args ...interface{}) {
glog.Errorf(format, args...)
glog.Errorf("See '%s -h' for help.", cmd.CommandPath())
Expand Down
11 changes: 6 additions & 5 deletions pkg/kubectl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/spf13/cobra"

cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
Expand All @@ -48,10 +49,10 @@ func (f *Factory) NewCmdCreate(out io.Writer) *cobra.Command {
Example: create_example,
Run: func(cmd *cobra.Command, args []string) {
schema, err := f.Validator(cmd)
checkErr(err)
cmdutil.CheckErr(err)

cmdNamespace, err := f.DefaultNamespace(cmd)
checkErr(err)
cmdutil.CheckErr(err)

mapper, typer := f.Object(cmd)
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand(cmd)).
Expand All @@ -60,7 +61,7 @@ func (f *Factory) NewCmdCreate(out io.Writer) *cobra.Command {
FilenameParam(flags.Filenames...).
Flatten().
Do()
checkErr(r.Err())
cmdutil.CheckErr(r.Err())

count := 0
err = r.Visit(func(info *resource.Info) error {
Expand All @@ -80,9 +81,9 @@ func (f *Factory) NewCmdCreate(out io.Writer) *cobra.Command {
fmt.Fprintf(out, "%s\n", info.Name)
return nil
})
checkErr(err)
cmdutil.CheckErr(err)
if count == 0 {
checkErr(fmt.Errorf("no objects passed to create"))
cmdutil.CheckErr(fmt.Errorf("no objects passed to create"))
}
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubectl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (f *Factory) NewCmdDelete(out io.Writer) *cobra.Command {
Example: delete_example,
Run: func(cmd *cobra.Command, args []string) {
cmdNamespace, err := f.DefaultNamespace(cmd)
checkErr(err)
cmdutil.CheckErr(err)
mapper, typer := f.Object(cmd)
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand(cmd)).
ContinueOnError().
Expand All @@ -77,7 +77,7 @@ func (f *Factory) NewCmdDelete(out io.Writer) *cobra.Command {
ResourceTypeOrNameArgs(false, args...).
Flatten().
Do()
checkErr(r.Err())
cmdutil.CheckErr(r.Err())

found := 0
err = r.IgnoreErrors(errors.IsNotFound).Visit(func(r *resource.Info) error {
Expand All @@ -88,7 +88,7 @@ func (f *Factory) NewCmdDelete(out io.Writer) *cobra.Command {
fmt.Fprintf(out, "%s\n", r.Name)
return nil
})
checkErr(err)
cmdutil.CheckErr(err)
if found == 0 {
fmt.Fprintf(cmd.Out(), "No resources found\n")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubectl/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ This command joins many API calls together to form a detailed description of a
given resource.`,
Run: func(cmd *cobra.Command, args []string) {
cmdNamespace, err := f.DefaultNamespace(cmd)
checkErr(err)
util.CheckErr(err)

mapper, _ := f.Object(cmd)
// TODO: use resource.Builder instead
mapping, namespace, name := util.ResourceFromArgs(cmd, args, mapper, cmdNamespace)

describer, err := f.Describer(cmd, mapping)
checkErr(err)
util.CheckErr(err)

s, err := describer.Describe(namespace, name)
checkErr(err)
util.CheckErr(err)
fmt.Fprintf(out, "%s\n", s)
},
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/kubectl/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func (f *Factory) NewCmdExec(cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.C
}

namespace, err := f.DefaultNamespace(cmd)
checkErr(err)
util.CheckErr(err)

client, err := f.Client(cmd)
checkErr(err)
util.CheckErr(err)

pod, err := client.Pods(namespace).Get(flags.pod)
checkErr(err)
util.CheckErr(err)

if pod.Status.Phase != api.PodRunning {
glog.Fatalf("Unable to execute command because pod is not running. Current status=%v", pod.Status.Phase)
Expand Down Expand Up @@ -113,7 +113,7 @@ func (f *Factory) NewCmdExec(cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.C
}

config, err := f.ClientConfig(cmd)
checkErr(err)
util.CheckErr(err)

req := client.RESTClient.Get().
Prefix("proxy").
Expand All @@ -123,7 +123,7 @@ func (f *Factory) NewCmdExec(cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.C

e := remotecommand.New(req, config, args, stdin, cmdOut, cmdErr, flags.tty)
err = e.Execute()
checkErr(err)
util.CheckErr(err)
},
}
cmd.Flags().StringVarP(&flags.pod, "pod", "p", "", "Pod name")
Expand Down
16 changes: 8 additions & 8 deletions pkg/kubectl/cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (f *Factory) NewCmdExposeService(out io.Writer) *cobra.Command {
}

namespace, err := f.DefaultNamespace(cmd)
checkErr(err)
util.CheckErr(err)
client, err := f.Client(cmd)
checkErr(err)
util.CheckErr(err)

generatorName := util.GetFlagString(cmd, "generator")

Expand All @@ -73,33 +73,33 @@ func (f *Factory) NewCmdExposeService(out io.Writer) *cobra.Command {
}
if _, found := params["selector"]; !found {
rc, err := client.ReplicationControllers(namespace).Get(args[0])
checkErr(err)
util.CheckErr(err)
params["selector"] = kubectl.MakeLabels(rc.Spec.Selector)
}
if util.GetFlagBool(cmd, "create-external-load-balancer") {
params["create-external-load-balancer"] = "true"
}

err = kubectl.ValidateParams(names, params)
checkErr(err)
util.CheckErr(err)

service, err := generator.Generate(params)
checkErr(err)
util.CheckErr(err)

inline := util.GetFlagString(cmd, "overrides")
if len(inline) > 0 {
service, err = util.Merge(service, inline, "Service")
checkErr(err)
util.CheckErr(err)
}

// TODO: extract this flag to a central location, when such a location exists.
if !util.GetFlagBool(cmd, "dry-run") {
service, err = client.Services(namespace).Create(service.(*api.Service))
checkErr(err)
util.CheckErr(err)
}

err = f.PrintObject(cmd, service, out)
checkErr(err)
util.CheckErr(err)
},
}
util.AddPrinterFlags(cmd)
Expand Down
26 changes: 13 additions & 13 deletions pkg/kubectl/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
mapper, typer := f.Object(cmd)

cmdNamespace, err := f.DefaultNamespace(cmd)
checkErr(err)
util.CheckErr(err)

// handle watch separately since we cannot watch multiple resource types
isWatch, isWatchOnly := util.GetFlagBool(cmd, "watch"), util.GetFlagBool(cmd, "watch-only")
Expand All @@ -92,30 +92,30 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
ResourceTypeOrNameArgs(true, args...).
SingleResourceType().
Do()
checkErr(r.Err())
util.CheckErr(r.Err())

mapping, err := r.ResourceMapping()
checkErr(err)
util.CheckErr(err)

printer, err := f.PrinterForMapping(cmd, mapping)
checkErr(err)
util.CheckErr(err)

obj, err := r.Object()
checkErr(err)
util.CheckErr(err)

rv, err := mapping.MetadataAccessor.ResourceVersion(obj)
checkErr(err)
util.CheckErr(err)

// print the current object
if !isWatchOnly {
if err := printer.PrintObj(obj, out); err != nil {
checkErr(fmt.Errorf("unable to output the provided object: %v", err))
util.CheckErr(fmt.Errorf("unable to output the provided object: %v", err))
}
}

// print watched changes
w, err := r.Watch(rv)
checkErr(err)
util.CheckErr(err)

kubectl.WatchLoop(w, func(e watch.Event) error {
return printer.PrintObj(e.Object, out)
Expand All @@ -129,19 +129,19 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
ResourceTypeOrNameArgs(true, args...).
Latest()
printer, generic, err := util.PrinterForCommand(cmd)
checkErr(err)
util.CheckErr(err)

if generic {
clientConfig, err := f.ClientConfig(cmd)
checkErr(err)
util.CheckErr(err)
defaultVersion := clientConfig.Version

// the outermost object will be converted to the output-version
version := util.OutputVersion(cmd, defaultVersion)

r := b.Flatten().Do()
obj, err := r.Object()
checkErr(err)
util.CheckErr(err)

// try conversion to all the possible versions
// TODO: simplify by adding a ResourceBuilder mode
Expand All @@ -158,7 +158,7 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
printer := kubectl.NewVersionedPrinter(printer, api.Scheme, versions...)

err = printer.PrintObj(obj, out)
checkErr(err)
util.CheckErr(err)
return
}

Expand All @@ -170,5 +170,5 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
}
return printer.PrintObj(r.Object, out)
})
checkErr(err)
util.CheckErr(err)
}
12 changes: 6 additions & 6 deletions pkg/kubectl/cmd/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,28 @@ func (f *Factory) NewCmdLabel(out io.Writer) *cobra.Command {
}
res := args[:2]
cmdNamespace, err := f.DefaultNamespace(cmd)
checkErr(err)
util.CheckErr(err)

mapper, _ := f.Object(cmd)
// TODO: use resource.Builder instead
mapping, namespace, name := util.ResourceFromArgs(cmd, res, mapper, cmdNamespace)
client, err := f.RESTClient(cmd, mapping)
checkErr(err)
util.CheckErr(err)

labels, remove, err := parseLabels(args[2:])
checkErr(err)
util.CheckErr(err)
overwrite := util.GetFlagBool(cmd, "overwrite")
resourceVersion := util.GetFlagString(cmd, "resource-version")

obj, err := updateObject(client, mapping, namespace, name, func(obj runtime.Object) runtime.Object {
outObj, err := labelFunc(obj, overwrite, resourceVersion, labels, remove)
checkErr(err)
util.CheckErr(err)
return outObj
})
checkErr(err)
util.CheckErr(err)

printer, err := f.PrinterForMapping(cmd, mapping)
checkErr(err)
util.CheckErr(err)

printer.PrintObj(obj, out)
},
Expand Down