Skip to content

Commit

Permalink
Merge pull request kubernetes#2796 from jhadvig/stream_logs
Browse files Browse the repository at this point in the history
Streaming container logs
  • Loading branch information
bgrant0607 committed Dec 15, 2014
2 parents 5ef34bf + 019ca75 commit 1abc3e4
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions pkg/kubectl/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pod> [<container>]",
Use: "log [-f] <pod> [<container>]",
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) {
if len(args) == 0 {
usageError(cmd, "<pod> is required for log")
Expand Down Expand Up @@ -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
}

0 comments on commit 1abc3e4

Please sign in to comment.