From 3f88f27d7bac0e2574b75061a2714ad82625133a Mon Sep 17 00:00:00 2001 From: Dylan Carney Date: Thu, 11 Dec 2014 10:39:20 -0800 Subject: [PATCH 1/2] Modifies kubectl log cmd to not require container arg for single-container pods (for #2847) --- pkg/kubectl/cmd/log.go | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/pkg/kubectl/cmd/log.go b/pkg/kubectl/cmd/log.go index 27e6989a0a8..3d7818829c8 100644 --- a/pkg/kubectl/cmd/log.go +++ b/pkg/kubectl/cmd/log.go @@ -24,27 +24,42 @@ import ( func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "log ", - Short: "Print the logs for a container in a pod", + Use: "log []", + Short: "Print the logs for a container in a pod.", + Long: "Print the logs for a container in a pod. If the pod has only one container, the container name is optional.", Run: func(cmd *cobra.Command, args []string) { - if len(args) != 2 { - usageError(cmd, " and are required for log") + if len(args) == 0 { + usageError(cmd, " is required for log") } namespace := GetKubeNamespace(cmd) - client, err := f.ClientBuilder.Client() checkErr(err) - pod, err := client.Pods(namespace).Get(args[0]) + + podID := args[0] + + pod, err := client.Pods(namespace).Get(podID) checkErr(err) + var container string + if len(args) == 1 { + if len(pod.Spec.Containers) == 1 { + // Get logs for the only container in the pod + container = pod.Spec.Containers[0].Name + } else { + usageError(cmd, " is required for pods with multiple containers") + } + } else { + container = args[1] + } + data, err := client.RESTClient.Get(). Path("proxy/minions"). Path(pod.Status.Host). Path("containerLogs"). Path(namespace). - Path(args[0]). - Path(args[1]). + Path(podID). + Path(container). Do(). Raw() checkErr(err) From ba5c18816ebbc041fb2ed1555cc0da74f8fd897a Mon Sep 17 00:00:00 2001 From: Dylan Carney Date: Thu, 11 Dec 2014 17:40:47 -0800 Subject: [PATCH 2/2] Updates kubectl log cmd per PR feedback --- pkg/kubectl/cmd/log.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/kubectl/cmd/log.go b/pkg/kubectl/cmd/log.go index 3d7818829c8..fb664129ed0 100644 --- a/pkg/kubectl/cmd/log.go +++ b/pkg/kubectl/cmd/log.go @@ -32,6 +32,10 @@ func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command { usageError(cmd, " is required for log") } + if len(args) > 2 { + usageError(cmd, "log []") + } + namespace := GetKubeNamespace(cmd) client, err := f.ClientBuilder.Client() checkErr(err) @@ -43,12 +47,12 @@ func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command { var container string if len(args) == 1 { - if len(pod.Spec.Containers) == 1 { - // Get logs for the only container in the pod - container = pod.Spec.Containers[0].Name - } else { + if len(pod.Spec.Containers) != 1 { usageError(cmd, " is required for pods with multiple containers") } + + // Get logs for the only container in the pod + container = pod.Spec.Containers[0].Name } else { container = args[1] }