From 019ca75fc6c62359030ba2625e72d3d6beb4c155 Mon Sep 17 00:00:00 2001 From: jhadvig Date: Mon, 15 Dec 2014 12:07:25 +0100 Subject: [PATCH] Streaming container logs --- pkg/kubectl/cmd/log.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/pkg/kubectl/cmd/log.go b/pkg/kubectl/cmd/log.go index fb664129ed0bf..fed160cc78427 100644 --- a/pkg/kubectl/cmd/log.go +++ b/pkg/kubectl/cmd/log.go @@ -18,15 +18,22 @@ package cmd import ( "io" + "strconv" "github.com/spf13/cobra" ) func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "log []", + Use: "log [-f] []", 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.", + Long: `Print the logs for a container in a pod. If the pod has only one container, the container name is optional +Examples: + $ kubectl log 123456-7890 ruby-container + + + $ kubectl log -f 123456-7890 ruby-container + `, Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { usageError(cmd, " is required for log") @@ -57,19 +64,27 @@ func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command { container = args[1] } - data, err := client.RESTClient.Get(). + follow := false + if GetFlagBool(cmd, "follow") { + follow = true + } + + readCloser, err := client.RESTClient.Get(). Path("proxy/minions"). Path(pod.Status.Host). Path("containerLogs"). Path(namespace). Path(podID). Path(container). - Do(). - Raw() + Param("follow", strconv.FormatBool(follow)). + Stream() checkErr(err) - out.Write(data) + defer readCloser.Close() + _, err = io.Copy(out, readCloser) + checkErr(err) }, } + cmd.Flags().BoolP("follow", "f", false, "Specify if the logs should be streamed.") return cmd }