mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
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:
parent
46a80259f6
commit
efcca8f357
@ -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)
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user