mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 12:07:47 +00:00
Merge pull request #31675 from brendandburns/msgs
Automatic merge from submit-queue improve some error messages. Fixes https://github.com/kubernetes/kubernetes/issues/9944 @pwittrock @deads2k @kubernetes/kubectl
This commit is contained in:
commit
5e22e51f34
@ -457,7 +457,7 @@ func TestResourceErrors(t *testing.T) {
|
||||
}{
|
||||
"no args": {
|
||||
args: []string{},
|
||||
errFn: func(err error) bool { return strings.Contains(err.Error(), "you must provide one or more resources") },
|
||||
errFn: func(err error) bool { return strings.Contains(err.Error(), "You must provide one or more resources") },
|
||||
},
|
||||
"resources but no selectors": {
|
||||
args: []string{"pods"},
|
||||
|
@ -48,6 +48,10 @@ var (
|
||||
kubectl exec 123456-7890 -c ruby-container -i -t -- bash -il`)
|
||||
)
|
||||
|
||||
const (
|
||||
execUsageStr = "expected 'exec POD_NAME COMMAND [ARG1] [ARG2] ... [ARGN]'.\nPOD_NAME and COMMAND are required arguments for the exec command"
|
||||
)
|
||||
|
||||
func NewCmdExec(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.Command {
|
||||
options := &ExecOptions{
|
||||
StreamOptions: StreamOptions{
|
||||
@ -135,19 +139,19 @@ type ExecOptions struct {
|
||||
func (p *ExecOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, argsIn []string, argsLenAtDash int) error {
|
||||
// Let kubectl exec follow rules for `--`, see #13004 issue
|
||||
if len(p.PodName) == 0 && (len(argsIn) == 0 || argsLenAtDash == 0) {
|
||||
return cmdutil.UsageError(cmd, "POD is required for exec")
|
||||
return cmdutil.UsageError(cmd, execUsageStr)
|
||||
}
|
||||
if len(p.PodName) != 0 {
|
||||
printDeprecationWarning("exec POD", "-p POD")
|
||||
printDeprecationWarning("exec POD_NAME", "-p POD_NAME")
|
||||
if len(argsIn) < 1 {
|
||||
return cmdutil.UsageError(cmd, "COMMAND is required for exec")
|
||||
return cmdutil.UsageError(cmd, execUsageStr)
|
||||
}
|
||||
p.Command = argsIn
|
||||
} else {
|
||||
p.PodName = argsIn[0]
|
||||
p.Command = argsIn[1:]
|
||||
if len(p.Command) < 1 {
|
||||
return cmdutil.UsageError(cmd, "COMMAND is required for exec")
|
||||
return cmdutil.UsageError(cmd, execUsageStr)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,6 +53,10 @@ var (
|
||||
kubectl logs --since=1h nginx`)
|
||||
)
|
||||
|
||||
const (
|
||||
logsUsageStr = "expected 'logs POD_NAME [CONTAINER_NAME]'.\nPOD_NAME is a required argument for the logs command"
|
||||
)
|
||||
|
||||
type LogsOptions struct {
|
||||
Namespace string
|
||||
ResourceArg string
|
||||
@ -111,17 +115,17 @@ func (o *LogsOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Com
|
||||
containerName := cmdutil.GetFlagString(cmd, "container")
|
||||
switch len(args) {
|
||||
case 0:
|
||||
return cmdutil.UsageError(cmd, "POD is required for logs")
|
||||
return cmdutil.UsageError(cmd, logsUsageStr)
|
||||
case 1:
|
||||
o.ResourceArg = args[0]
|
||||
case 2:
|
||||
if cmd.Flag("container").Changed {
|
||||
return cmdutil.UsageError(cmd, "only one of -c, [CONTAINER] arg is allowed")
|
||||
return cmdutil.UsageError(cmd, "only one of -c or an inline [CONTAINER] arg is allowed")
|
||||
}
|
||||
o.ResourceArg = args[0]
|
||||
containerName = args[1]
|
||||
default:
|
||||
return cmdutil.UsageError(cmd, "logs POD [-c CONTAINER]")
|
||||
return cmdutil.UsageError(cmd, logsUsageStr)
|
||||
}
|
||||
var err error
|
||||
o.Namespace, _, err = f.DefaultNamespace()
|
||||
|
@ -106,11 +106,6 @@ func RunScale(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
|
||||
printDeprecationWarning("scale", "resize")
|
||||
}
|
||||
|
||||
count := cmdutil.GetFlagInt(cmd, "replicas")
|
||||
if count < 0 {
|
||||
return cmdutil.UsageError(cmd, "--replicas=COUNT is required, and COUNT must be greater than or equal to 0")
|
||||
}
|
||||
|
||||
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -125,10 +120,18 @@ func RunScale(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
|
||||
Flatten().
|
||||
Do()
|
||||
err = r.Err()
|
||||
if resource.IsUsageError(err) {
|
||||
return cmdutil.UsageError(cmd, err.Error())
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
count := cmdutil.GetFlagInt(cmd, "replicas")
|
||||
if count < 0 {
|
||||
return cmdutil.UsageError(cmd, "The --replicas=COUNT flag is required, and COUNT must be greater than or equal to 0")
|
||||
}
|
||||
|
||||
infos := []*resource.Info{}
|
||||
err = r.Visit(func(info *resource.Info, err error) error {
|
||||
if err == nil {
|
||||
|
@ -78,6 +78,21 @@ type Builder struct {
|
||||
schema validation.Schema
|
||||
}
|
||||
|
||||
var missingResourceError = fmt.Errorf(`You must provide one or more resources by argument or filename.
|
||||
Example resource specifications include:
|
||||
'-f rsrc.yaml'
|
||||
'--filename=rsrc.json'
|
||||
'pods my-pod'
|
||||
'services'`)
|
||||
|
||||
// TODO: expand this to include other errors.
|
||||
func IsUsageError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
return err == missingResourceError
|
||||
}
|
||||
|
||||
type resourceTuple struct {
|
||||
Resource string
|
||||
Name string
|
||||
@ -700,7 +715,7 @@ func (b *Builder) visitorResult() *Result {
|
||||
if len(b.resources) != 0 {
|
||||
return &Result{err: fmt.Errorf("resource(s) were provided, but no name, label selector, or --all flag specified")}
|
||||
}
|
||||
return &Result{err: fmt.Errorf("you must provide one or more resources by argument or filename (%s)", strings.Join(InputExtensions, "|"))}
|
||||
return &Result{err: missingResourceError}
|
||||
}
|
||||
|
||||
// Do returns a Result object with a Visitor for the resources identified by the Builder.
|
||||
|
Loading…
Reference in New Issue
Block a user