Skip to content

Commit

Permalink
Introduce --previous option to kubectl log
Browse files Browse the repository at this point in the history
  • Loading branch information
dchen1107 committed May 11, 2015
1 parent 35c644a commit ecaf087
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions contrib/completions/bash/kubectl
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ _kubectl_log()
flags+=("--help")
flags+=("-h")
flags+=("--interactive")
flags+=("--previous")
flags+=("-p")

must_have_one_flag=()
must_have_one_noun=()
Expand Down
6 changes: 5 additions & 1 deletion docs/kubectl_log.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Print the logs for a container in a pod.
Print the logs for a container in a pod. If the pod has only one container, the container name is optional.

```
kubectl log [-f] POD [CONTAINER]
kubectl log [-f] [-p] POD [CONTAINER]
```

### Examples
Expand All @@ -17,6 +17,9 @@ kubectl log [-f] POD [CONTAINER]
// Returns snapshot of ruby-container logs from pod 123456-7890.
$ kubectl log 123456-7890 ruby-container
// Returns snapshot of previous terminated ruby-container logs from pod 123456-7890.
$ kubectl log -p 123456-7890 ruby-container
// Starts streaming of ruby-container logs from pod 123456-7890.
$ kubectl log -f 123456-7890 ruby-container
```
Expand All @@ -27,6 +30,7 @@ $ kubectl log -f 123456-7890 ruby-container
-f, --follow=false: Specify if the logs should be streamed.
-h, --help=false: help for log
--interactive=true: If true, prompt the user for input when required. Default true.
-p, --previous=false: If true, print the logs for the previous instance of the container in a pod if it exists.
```

### Options inherited from parent commands
Expand Down
7 changes: 7 additions & 0 deletions docs/man/man1/kubectl-log.1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Print the logs for a container in a pod. If the pod has only one container, the
\fB\-\-interactive\fP=true
If true, prompt the user for input when required. Default true.

.PP
\fB\-p\fP, \fB\-\-previous\fP=false
If true, print the logs for the previous instance of the container in a pod if it exists.


.SH OPTIONS INHERITED FROM PARENT COMMANDS
.PP
Expand Down Expand Up @@ -140,6 +144,9 @@ Print the logs for a container in a pod. If the pod has only one container, the
// Returns snapshot of ruby\-container logs from pod 123456\-7890.
$ kubectl log 123456\-7890 ruby\-container

// Returns snapshot of previous terminated ruby\-container logs from pod 123456\-7890.
$ kubectl log \-p 123456\-7890 ruby\-container

// Starts streaming of ruby\-container logs from pod 123456\-7890.
$ kubectl log \-f 123456\-7890 ruby\-container

Expand Down
12 changes: 11 additions & 1 deletion pkg/kubectl/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
log_example = `// Returns snapshot of ruby-container logs from pod 123456-7890.
$ kubectl log 123456-7890 ruby-container
// Returns snapshot of previous terminated ruby-container logs from pod 123456-7890.
$ kubectl log -p 123456-7890 ruby-container
// Starts streaming of ruby-container logs from pod 123456-7890.
$ kubectl log -f 123456-7890 ruby-container`
)
Expand Down Expand Up @@ -60,7 +63,7 @@ func selectContainer(pod *api.Pod, in io.Reader, out io.Writer) string {
// NewCmdLog creates a new pod log command
func NewCmdLog(f *cmdutil.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "log [-f] POD [CONTAINER]",
Use: "log [-f] [-p] 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.",
Example: log_example,
Expand All @@ -71,6 +74,7 @@ func NewCmdLog(f *cmdutil.Factory, out io.Writer) *cobra.Command {
}
cmd.Flags().BoolP("follow", "f", false, "Specify if the logs should be streamed.")
cmd.Flags().Bool("interactive", true, "If true, prompt the user for input when required. Default true.")
cmd.Flags().BoolP("previous", "p", false, "If true, print the logs for the previous instance of the container in a pod if it exists.")
return cmd
}

Expand Down Expand Up @@ -115,13 +119,19 @@ func RunLog(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
follow = true
}

previous := false
if cmdutil.GetFlagBool(cmd, "previous") {
previous = true
}

readCloser, err := client.RESTClient.Get().
Namespace(namespace).
Name(podID).
Resource("pods").
SubResource("log").
Param("follow", strconv.FormatBool(follow)).
Param("container", container).
Param("previous", strconv.FormatBool(previous)).
Stream()
if err != nil {
return err
Expand Down

0 comments on commit ecaf087

Please sign in to comment.