diff --git a/pkg/kubectl/cmd/attach/attach.go b/pkg/kubectl/cmd/attach/attach.go index 266fff2b355..2fa5786f95e 100644 --- a/pkg/kubectl/cmd/attach/attach.go +++ b/pkg/kubectl/cmd/attach/attach.go @@ -68,8 +68,9 @@ type AttachOptions struct { // whether to disable use of standard error when streaming output from tty DisableStderr bool - CommandName string - SuggestedCmdUsage string + CommandName string + ParentCommandName string + EnableSuggestedCmdUsage bool Pod *corev1.Pod @@ -183,13 +184,12 @@ func (o *AttachOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []s o.Resources = args o.restClientGetter = f - fullCmdName := "" cmdParent := cmd.Parent() if cmdParent != nil { - fullCmdName = cmdParent.CommandPath() + o.ParentCommandName = cmdParent.CommandPath() } - if len(fullCmdName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") { - o.SuggestedCmdUsage = fmt.Sprintf("Use '%s describe pod/%s -n %s' to see all of the containers in this pod.", fullCmdName, o.PodName, o.Namespace) + if len(o.ParentCommandName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") { + o.EnableSuggestedCmdUsage = true } config, err := f.ToRESTConfig() @@ -325,9 +325,9 @@ func (o *AttachOptions) containerToAttachTo(pod *corev1.Pod) (*corev1.Container, return nil, fmt.Errorf("container not found (%s)", o.ContainerName) } - if len(o.SuggestedCmdUsage) > 0 { + if o.EnableSuggestedCmdUsage { fmt.Fprintf(o.ErrOut, "Defaulting container name to %s.\n", pod.Spec.Containers[0].Name) - fmt.Fprintf(o.ErrOut, "%s\n", o.SuggestedCmdUsage) + fmt.Fprintf(o.ErrOut, "Use '%s describe pod/%s -n %s' to see all of the containers in this pod.\n", o.ParentCommandName, o.PodName, o.Namespace) } klog.V(4).Infof("defaulting container name to %s", pod.Spec.Containers[0].Name) diff --git a/pkg/kubectl/cmd/exec/exec.go b/pkg/kubectl/cmd/exec/exec.go index ee1b8da9d1b..e8f13259aa8 100644 --- a/pkg/kubectl/cmd/exec/exec.go +++ b/pkg/kubectl/cmd/exec/exec.go @@ -151,8 +151,8 @@ type ExecOptions struct { ResourceName string Command []string - FullCmdName string - SuggestedCmdUsage string + ParentCommandName string + EnableSuggestedCmdUsage bool Builder func() *resource.Builder ExecutablePodFn polymorphichelpers.AttachablePodForObjectFunc @@ -200,10 +200,10 @@ func (p *ExecOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, argsIn []s cmdParent := cmd.Parent() if cmdParent != nil { - p.FullCmdName = cmdParent.CommandPath() + p.ParentCommandName = cmdParent.CommandPath() } - if len(p.FullCmdName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") { - p.SuggestedCmdUsage = fmt.Sprintf("Use '%s describe %s -n %s' to see all of the containers in this pod.", p.FullCmdName, p.ResourceName, p.Namespace) + if len(p.ParentCommandName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") { + p.EnableSuggestedCmdUsage = true } p.Config, err = f.ToRESTConfig() @@ -322,11 +322,10 @@ func (p *ExecOptions) Run() error { containerName := p.ContainerName if len(containerName) == 0 { if len(pod.Spec.Containers) > 1 { - usageString := fmt.Sprintf("Defaulting container name to %s.", pod.Spec.Containers[0].Name) - if len(p.SuggestedCmdUsage) > 0 { - usageString = fmt.Sprintf("%s\n%s", usageString, p.SuggestedCmdUsage) + fmt.Fprintf(p.ErrOut, "Defaulting container name to %s.\n", pod.Spec.Containers[0].Name) + if p.EnableSuggestedCmdUsage { + fmt.Fprintf(p.ErrOut, "Use '%s describe pod/%s -n %s' to see all of the containers in this pod.\n", p.ParentCommandName, pod.Name, p.Namespace) } - fmt.Fprintf(p.ErrOut, "%s\n", usageString) } containerName = pod.Spec.Containers[0].Name }