Skip to content

Commit

Permalink
Enable -c for kubectl logs container arg
Browse files Browse the repository at this point in the history
  • Loading branch information
janetkuo committed Jun 30, 2015
1 parent f41c0d0 commit 62b4883
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
2 changes: 2 additions & 0 deletions contrib/completions/bash/kubectl
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ _kubectl_logs()
flags_with_completion=()
flags_completion=()

flags+=("--container=")
two_word_flags+=("-c")
flags+=("--follow")
flags+=("-f")
flags+=("--help")
Expand Down
5 changes: 3 additions & 2 deletions docs/kubectl_logs.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 logs [-f] [-p] POD [CONTAINER]
kubectl logs [-f] [-p] POD [-c CONTAINER]
```

### Examples
Expand All @@ -27,6 +27,7 @@ $ kubectl logs -f 123456-7890 ruby-container
### Options

```
-c, --container="": Container name
-f, --follow=false: Specify if the logs should be streamed.
-h, --help=false: help for logs
--interactive=true: If true, prompt the user for input when required. Default true.
Expand Down Expand Up @@ -65,6 +66,6 @@ $ kubectl logs -f 123456-7890 ruby-container
### SEE ALSO
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager

###### Auto generated by spf13/cobra at 2015-05-21 20:24:03.06578685 +0000 UTC
###### Auto generated by spf13/cobra at 2015-06-30 16:27:32.981507725 +0000 UTC

[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/kubectl_logs.md?pixel)]()
4 changes: 4 additions & 0 deletions docs/man/man1/kubectl-logs.1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Print the logs for a container in a pod. If the pod has only one container, the


.SH OPTIONS
.PP
\fB\-c\fP, \fB\-\-container\fP=""
Container name

.PP
\fB\-f\fP, \fB\-\-follow\fP=false
Specify if the logs should be streamed.
Expand Down
30 changes: 21 additions & 9 deletions pkg/kubectl/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,33 @@ func selectContainer(pod *api.Pod, in io.Reader, out io.Writer) string {
}
}

type logParams struct {
containerName string
}

// NewCmdLog creates a new pod log command
func NewCmdLog(f *cmdutil.Factory, out io.Writer) *cobra.Command {
params := &logParams{}
cmd := &cobra.Command{
Use: "logs [-f] [-p] POD [CONTAINER]",
Use: "logs [-f] [-p] POD [-c 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,
Run: func(cmd *cobra.Command, args []string) {
err := RunLog(f, out, cmd, args)
err := RunLog(f, out, cmd, args, params)
cmdutil.CheckErr(err)
},
Aliases: []string{"log"},
}
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.")
cmd.Flags().StringVarP(&params.containerName, "container", "c", "", "Container name")
return cmd
}

// RunLog retrieves a pod log
func RunLog(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
func RunLog(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, p *logParams) error {
if len(os.Args) > 1 && os.Args[1] == "log" {
printDeprecationWarning("logs", "log")
}
Expand Down Expand Up @@ -111,13 +117,19 @@ func RunLog(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
}

var container string
if len(args) == 1 {
if len(pod.Spec.Containers) != 1 {
return fmt.Errorf("POD %s has more than one container; please specify the container to print logs for", pod.ObjectMeta.Name)
}
container = pod.Spec.Containers[0].Name
if cmdutil.GetFlagString(cmd, "container") != "" {
// [-c CONTAINER]
container = p.containerName
} else {
container = args[1]
// [CONTAINER] (container as arg not flag) is supported as legacy behavior. See PR #10519 for more details.
if len(args) == 1 {
if len(pod.Spec.Containers) != 1 {
return fmt.Errorf("POD %s has more than one container; please specify the container to print logs for", pod.ObjectMeta.Name)
}
container = pod.Spec.Containers[0].Name
} else {
container = args[1]
}
}

follow := false
Expand Down

0 comments on commit 62b4883

Please sign in to comment.