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

Fix missing pod name for kubectl attach #77606

Merged
merged 1 commit into from
May 20, 2019
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
16 changes: 8 additions & 8 deletions pkg/kubectl/cmd/attach/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions pkg/kubectl/cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down