Merge pull request #2796 from jhadvig/stream_logs

Streaming container logs
This commit is contained in:
bgrant0607 2014-12-15 08:24:10 -08:00
commit 1abc3e423c

View File

@ -18,15 +18,22 @@ package cmd
import ( import (
"io" "io"
"strconv"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command { func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "log <pod> [<container>]", Use: "log [-f] <pod> [<container>]",
Short: "Print the logs for a container in a pod.", 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
<returns snapshot of ruby-container logs from pod 123456-7890>
$ kubectl log -f 123456-7890 ruby-container
<starts streaming of ruby-container logs from pod 123456-7890>`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 { if len(args) == 0 {
usageError(cmd, "<pod> is required for log") usageError(cmd, "<pod> is required for log")
@ -57,19 +64,27 @@ func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command {
container = args[1] 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("proxy/minions").
Path(pod.Status.Host). Path(pod.Status.Host).
Path("containerLogs"). Path("containerLogs").
Path(namespace). Path(namespace).
Path(podID). Path(podID).
Path(container). Path(container).
Do(). Param("follow", strconv.FormatBool(follow)).
Raw() Stream()
checkErr(err) 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 return cmd
} }