From cc332547d6a0e9295df8779f7e075e1c9773936f Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Thu, 18 Jan 2018 18:35:39 -0500 Subject: [PATCH] add suggestion to describe pod for container names --- pkg/oc/cli/cmd/debug.go | 10 ++++++ pkg/oc/cli/cmd/rsh.go | 9 ++++++ pkg/oc/cli/cmd/rsync/execremote.go | 31 ++++++++++--------- pkg/oc/cli/cmd/rsync/rsync.go | 29 +++++++++++------ .../kubernetes/pkg/kubectl/cmd/attach.go | 16 +++++++++- 5 files changed, 71 insertions(+), 24 deletions(-) diff --git a/pkg/oc/cli/cmd/debug.go b/pkg/oc/cli/cmd/debug.go index de25e03ada56..e03990097065 100644 --- a/pkg/oc/cli/cmd/debug.go +++ b/pkg/oc/cli/cmd/debug.go @@ -263,6 +263,16 @@ func (o *DebugOptions) Complete(cmd *cobra.Command, f *clientcmd.Factory, args [ o.AsNonRoot = !o.AsRoot && cmd.Flag("as-root").Changed if len(o.Attach.ContainerName) == 0 && len(pod.Spec.Containers) > 0 { + fullCmdName := "" + cmdParent := cmd.Parent() + if cmdParent != nil { + fullCmdName = cmdParent.CommandPath() + } + + if len(fullCmdName) > 0 && kcmdutil.IsSiblingCommandExists(cmd, "describe") { + fmt.Fprintf(o.Attach.Err, "Use '%s describe pod/%s -n %s' to see all of the containers in this pod.\n", fullCmdName, pod.Name, pod.Namespace) + } + glog.V(4).Infof("Defaulting container name to %s", pod.Spec.Containers[0].Name) o.Attach.ContainerName = pod.Spec.Containers[0].Name } diff --git a/pkg/oc/cli/cmd/rsh.go b/pkg/oc/cli/cmd/rsh.go index 0d622d748c34..066dde579357 100644 --- a/pkg/oc/cli/cmd/rsh.go +++ b/pkg/oc/cli/cmd/rsh.go @@ -147,6 +147,15 @@ func (o *RshOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []s o.PodClient = client.Core() o.PodName, err = f.PodForResource(resource, time.Duration(o.Timeout)*time.Second) + + fullCmdName := "" + cmdParent := cmd.Parent() + if cmdParent != nil { + fullCmdName = cmdParent.CommandPath() + } + if len(fullCmdName) > 0 && kcmdutil.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) + } return err } diff --git a/pkg/oc/cli/cmd/rsync/execremote.go b/pkg/oc/cli/cmd/rsync/execremote.go index 93fbbcdabc5a..595f88afe983 100644 --- a/pkg/oc/cli/cmd/rsync/execremote.go +++ b/pkg/oc/cli/cmd/rsync/execremote.go @@ -14,11 +14,12 @@ import ( // remoteExecutor will execute commands on a given pod/container by using the kube Exec command type remoteExecutor struct { - Namespace string - PodName string - ContainerName string - Client kclientset.Interface - Config *restclient.Config + Namespace string + PodName string + ContainerName string + SuggestedCmdUsage string + Client kclientset.Interface + Config *restclient.Config } // Ensure it implements the executor interface @@ -37,10 +38,11 @@ func (e *remoteExecutor) Execute(command []string, in io.Reader, out, errOut io. Err: errOut, Stdin: in != nil, }, - Executor: &kubecmd.DefaultRemoteExecutor{}, - PodClient: e.Client.Core(), - Config: e.Config, - Command: command, + SuggestedCmdUsage: e.SuggestedCmdUsage, + Executor: &kubecmd.DefaultRemoteExecutor{}, + PodClient: e.Client.Core(), + Config: e.Config, + Command: command, } err := execOptions.Validate() if err != nil { @@ -66,10 +68,11 @@ func newRemoteExecutor(f *clientcmd.Factory, o *RsyncOptions) (executor, error) } return &remoteExecutor{ - Namespace: o.Namespace, - PodName: o.PodName(), - ContainerName: o.ContainerName, - Config: config, - Client: client, + Namespace: o.Namespace, + PodName: o.PodName(), + ContainerName: o.ContainerName, + SuggestedCmdUsage: o.SuggestedCmdUsage, + Config: config, + Client: client, }, nil } diff --git a/pkg/oc/cli/cmd/rsync/rsync.go b/pkg/oc/cli/cmd/rsync/rsync.go index d6368b82d1dc..7476a7e0237b 100644 --- a/pkg/oc/cli/cmd/rsync/rsync.go +++ b/pkg/oc/cli/cmd/rsync/rsync.go @@ -70,15 +70,16 @@ type podChecker interface { // RsyncOptions holds the options to execute the sync command type RsyncOptions struct { - Namespace string - ContainerName string - Source *pathSpec - Destination *pathSpec - Strategy copyStrategy - StrategyName string - Quiet bool - Delete bool - Watch bool + Namespace string + ContainerName string + Source *pathSpec + Destination *pathSpec + Strategy copyStrategy + StrategyName string + Quiet bool + Delete bool + Watch bool + SuggestedCmdUsage string RsyncInclude []string RsyncExclude []string @@ -214,6 +215,16 @@ func (o *RsyncOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args [ return err } + fullCmdName := "" + cmdParent := cmd.Parent() + if cmdParent != nil { + fullCmdName = cmdParent.CommandPath() + } + + if len(fullCmdName) > 0 && kcmdutil.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.Strategy, err = o.determineStrategy(f, cmd, o.StrategyName) if err != nil { return err diff --git a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/attach.go b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/attach.go index 6d1b23323d18..a6523379fe1d 100644 --- a/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/attach.go +++ b/vendor/k8s.io/kubernetes/pkg/kubectl/cmd/attach.go @@ -114,7 +114,8 @@ func (*DefaultRemoteAttach) Attach(method string, url *url.URL, config *restclie type AttachOptions struct { StreamOptions - CommandName string + CommandName string + SuggestedCmdUsage string Pod *api.Pod @@ -167,6 +168,15 @@ func (p *AttachOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, argsIn [ p.PodName = attachablePod.Name p.Namespace = namespace + fullCmdName := "" + cmdParent := cmd.Parent() + if cmdParent != nil { + fullCmdName = cmdParent.CommandPath() + } + if len(fullCmdName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "describe") { + p.SuggestedCmdUsage = fmt.Sprintf("Use '%s describe pod/%s -n %s' to see all of the containers in this pod.", fullCmdName, p.PodName, p.Namespace) + } + config, err := f.ClientConfig() if err != nil { return err @@ -312,6 +322,10 @@ func (p *AttachOptions) containerToAttachTo(pod *api.Pod) (*api.Container, error return nil, fmt.Errorf("container not found (%s)", p.ContainerName) } + if len(p.SuggestedCmdUsage) > 0 { + fmt.Fprintf(p.Err, "%s\n", p.SuggestedCmdUsage) + } + glog.V(4).Infof("defaulting container name to %s", pod.Spec.Containers[0].Name) return &pod.Spec.Containers[0], nil }