Fix missing pod name for kubectl attach and exec

The pod name is missing in suggested cmd usage when executing kubectl attach
and exec:
"Defaulting container name to master.
 Use 'kubectl describe pod/ -n default' to see all of the containers in this pod."

The PodName is empty in func Complete as the attached Pod isn't populated yet,
causing suggested cmd usage imcomplete. This patch renders PodName after it is
populated.
This commit is contained in:
Quan Tian 2019-05-08 08:29:36 -07:00
parent 46a80259f6
commit efcca8f357
2 changed files with 16 additions and 17 deletions

View File

@ -69,7 +69,8 @@ type AttachOptions struct {
DisableStderr bool
CommandName string
SuggestedCmdUsage 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)

View File

@ -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
}