Skip to content

Commit 62b4883

Browse files
committed
Enable -c for kubectl logs container arg
1 parent f41c0d0 commit 62b4883

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

contrib/completions/bash/kubectl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ _kubectl_logs()
428428
flags_with_completion=()
429429
flags_completion=()
430430

431+
flags+=("--container=")
432+
two_word_flags+=("-c")
431433
flags+=("--follow")
432434
flags+=("-f")
433435
flags+=("--help")

docs/kubectl_logs.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Print the logs for a container in a pod.
88
Print the logs for a container in a pod. If the pod has only one container, the container name is optional.
99

1010
```
11-
kubectl logs [-f] [-p] POD [CONTAINER]
11+
kubectl logs [-f] [-p] POD [-c CONTAINER]
1212
```
1313

1414
### Examples
@@ -27,6 +27,7 @@ $ kubectl logs -f 123456-7890 ruby-container
2727
### Options
2828

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

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

7071
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/kubectl_logs.md?pixel)]()

docs/man/man1/kubectl-logs.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Print the logs for a container in a pod. If the pod has only one container, the
1717

1818

1919
.SH OPTIONS
20+
.PP
21+
\fB\-c\fP, \fB\-\-container\fP=""
22+
Container name
23+
2024
.PP
2125
\fB\-f\fP, \fB\-\-follow\fP=false
2226
Specify if the logs should be streamed.

pkg/kubectl/cmd/log.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,33 @@ func selectContainer(pod *api.Pod, in io.Reader, out io.Writer) string {
6161
}
6262
}
6363

64+
type logParams struct {
65+
containerName string
66+
}
67+
6468
// NewCmdLog creates a new pod log command
6569
func NewCmdLog(f *cmdutil.Factory, out io.Writer) *cobra.Command {
70+
params := &logParams{}
6671
cmd := &cobra.Command{
67-
Use: "logs [-f] [-p] POD [CONTAINER]",
72+
Use: "logs [-f] [-p] POD [-c CONTAINER]",
6873
Short: "Print the logs for a container in a pod.",
6974
Long: "Print the logs for a container in a pod. If the pod has only one container, the container name is optional.",
7075
Example: log_example,
7176
Run: func(cmd *cobra.Command, args []string) {
72-
err := RunLog(f, out, cmd, args)
77+
err := RunLog(f, out, cmd, args, params)
7378
cmdutil.CheckErr(err)
7479
},
7580
Aliases: []string{"log"},
7681
}
7782
cmd.Flags().BoolP("follow", "f", false, "Specify if the logs should be streamed.")
7883
cmd.Flags().Bool("interactive", true, "If true, prompt the user for input when required. Default true.")
7984
cmd.Flags().BoolP("previous", "p", false, "If true, print the logs for the previous instance of the container in a pod if it exists.")
85+
cmd.Flags().StringVarP(&params.containerName, "container", "c", "", "Container name")
8086
return cmd
8187
}
8288

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

113119
var container string
114-
if len(args) == 1 {
115-
if len(pod.Spec.Containers) != 1 {
116-
return fmt.Errorf("POD %s has more than one container; please specify the container to print logs for", pod.ObjectMeta.Name)
117-
}
118-
container = pod.Spec.Containers[0].Name
120+
if cmdutil.GetFlagString(cmd, "container") != "" {
121+
// [-c CONTAINER]
122+
container = p.containerName
119123
} else {
120-
container = args[1]
124+
// [CONTAINER] (container as arg not flag) is supported as legacy behavior. See PR #10519 for more details.
125+
if len(args) == 1 {
126+
if len(pod.Spec.Containers) != 1 {
127+
return fmt.Errorf("POD %s has more than one container; please specify the container to print logs for", pod.ObjectMeta.Name)
128+
}
129+
container = pod.Spec.Containers[0].Name
130+
} else {
131+
container = args[1]
132+
}
121133
}
122134

123135
follow := false

0 commit comments

Comments
 (0)