Skip to content

Commit 1ae2a5d

Browse files
authored
Merge pull request kubernetes#77606 from tnqn/attach
Fix missing pod name for kubectl attach
2 parents 6ba13bf + efcca8f commit 1ae2a5d

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

pkg/kubectl/cmd/attach/attach.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ type AttachOptions struct {
6868
// whether to disable use of standard error when streaming output from tty
6969
DisableStderr bool
7070

71-
CommandName string
72-
SuggestedCmdUsage string
71+
CommandName string
72+
ParentCommandName string
73+
EnableSuggestedCmdUsage bool
7374

7475
Pod *corev1.Pod
7576

@@ -183,13 +184,12 @@ func (o *AttachOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []s
183184
o.Resources = args
184185
o.restClientGetter = f
185186

186-
fullCmdName := ""
187187
cmdParent := cmd.Parent()
188188
if cmdParent != nil {
189-
fullCmdName = cmdParent.CommandPath()
189+
o.ParentCommandName = cmdParent.CommandPath()
190190
}
191-
if len(fullCmdName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") {
192-
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)
191+
if len(o.ParentCommandName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") {
192+
o.EnableSuggestedCmdUsage = true
193193
}
194194

195195
config, err := f.ToRESTConfig()
@@ -325,9 +325,9 @@ func (o *AttachOptions) containerToAttachTo(pod *corev1.Pod) (*corev1.Container,
325325
return nil, fmt.Errorf("container not found (%s)", o.ContainerName)
326326
}
327327

328-
if len(o.SuggestedCmdUsage) > 0 {
328+
if o.EnableSuggestedCmdUsage {
329329
fmt.Fprintf(o.ErrOut, "Defaulting container name to %s.\n", pod.Spec.Containers[0].Name)
330-
fmt.Fprintf(o.ErrOut, "%s\n", o.SuggestedCmdUsage)
330+
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)
331331
}
332332

333333
klog.V(4).Infof("defaulting container name to %s", pod.Spec.Containers[0].Name)

pkg/kubectl/cmd/exec/exec.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ type ExecOptions struct {
151151
ResourceName string
152152
Command []string
153153

154-
FullCmdName string
155-
SuggestedCmdUsage string
154+
ParentCommandName string
155+
EnableSuggestedCmdUsage bool
156156

157157
Builder func() *resource.Builder
158158
ExecutablePodFn polymorphichelpers.AttachablePodForObjectFunc
@@ -200,10 +200,10 @@ func (p *ExecOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, argsIn []s
200200

201201
cmdParent := cmd.Parent()
202202
if cmdParent != nil {
203-
p.FullCmdName = cmdParent.CommandPath()
203+
p.ParentCommandName = cmdParent.CommandPath()
204204
}
205-
if len(p.FullCmdName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") {
206-
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)
205+
if len(p.ParentCommandName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") {
206+
p.EnableSuggestedCmdUsage = true
207207
}
208208

209209
p.Config, err = f.ToRESTConfig()
@@ -322,11 +322,10 @@ func (p *ExecOptions) Run() error {
322322
containerName := p.ContainerName
323323
if len(containerName) == 0 {
324324
if len(pod.Spec.Containers) > 1 {
325-
usageString := fmt.Sprintf("Defaulting container name to %s.", pod.Spec.Containers[0].Name)
326-
if len(p.SuggestedCmdUsage) > 0 {
327-
usageString = fmt.Sprintf("%s\n%s", usageString, p.SuggestedCmdUsage)
325+
fmt.Fprintf(p.ErrOut, "Defaulting container name to %s.\n", pod.Spec.Containers[0].Name)
326+
if p.EnableSuggestedCmdUsage {
327+
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)
328328
}
329-
fmt.Fprintf(p.ErrOut, "%s\n", usageString)
330329
}
331330
containerName = pod.Spec.Containers[0].Name
332331
}

0 commit comments

Comments
 (0)