Merge pull request #77606 from tnqn/attach

Fix missing pod name for kubectl attach
This commit is contained in:
Kubernetes Prow Robot 2019-05-20 08:05:20 -07:00 committed by GitHub
commit 1ae2a5d0f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 17 deletions

View File

@ -68,8 +68,9 @@ type AttachOptions struct {
// whether to disable use of standard error when streaming output from tty // whether to disable use of standard error when streaming output from tty
DisableStderr bool DisableStderr bool
CommandName string CommandName string
SuggestedCmdUsage string ParentCommandName string
EnableSuggestedCmdUsage bool
Pod *corev1.Pod Pod *corev1.Pod
@ -183,13 +184,12 @@ func (o *AttachOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []s
o.Resources = args o.Resources = args
o.restClientGetter = f o.restClientGetter = f
fullCmdName := ""
cmdParent := cmd.Parent() cmdParent := cmd.Parent()
if cmdParent != nil { if cmdParent != nil {
fullCmdName = cmdParent.CommandPath() o.ParentCommandName = cmdParent.CommandPath()
} }
if len(fullCmdName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") { if len(o.ParentCommandName) > 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) o.EnableSuggestedCmdUsage = true
} }
config, err := f.ToRESTConfig() 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) 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, "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) klog.V(4).Infof("defaulting container name to %s", pod.Spec.Containers[0].Name)

View File

@ -151,8 +151,8 @@ type ExecOptions struct {
ResourceName string ResourceName string
Command []string Command []string
FullCmdName string ParentCommandName string
SuggestedCmdUsage string EnableSuggestedCmdUsage bool
Builder func() *resource.Builder Builder func() *resource.Builder
ExecutablePodFn polymorphichelpers.AttachablePodForObjectFunc ExecutablePodFn polymorphichelpers.AttachablePodForObjectFunc
@ -200,10 +200,10 @@ func (p *ExecOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, argsIn []s
cmdParent := cmd.Parent() cmdParent := cmd.Parent()
if cmdParent != nil { if cmdParent != nil {
p.FullCmdName = cmdParent.CommandPath() p.ParentCommandName = cmdParent.CommandPath()
} }
if len(p.FullCmdName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") { if len(p.ParentCommandName) > 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) p.EnableSuggestedCmdUsage = true
} }
p.Config, err = f.ToRESTConfig() p.Config, err = f.ToRESTConfig()
@ -322,11 +322,10 @@ func (p *ExecOptions) Run() error {
containerName := p.ContainerName containerName := p.ContainerName
if len(containerName) == 0 { if len(containerName) == 0 {
if len(pod.Spec.Containers) > 1 { if len(pod.Spec.Containers) > 1 {
usageString := fmt.Sprintf("Defaulting container name to %s.", pod.Spec.Containers[0].Name) fmt.Fprintf(p.ErrOut, "Defaulting container name to %s.\n", pod.Spec.Containers[0].Name)
if len(p.SuggestedCmdUsage) > 0 { if p.EnableSuggestedCmdUsage {
usageString = fmt.Sprintf("%s\n%s", usageString, p.SuggestedCmdUsage) 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 containerName = pod.Spec.Containers[0].Name
} }