Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt the user to select a container if they don't provide one #4776

Merged
merged 1 commit into from
Feb 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/kubectl-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ $ kubectl log -f 123456-7890 ruby-container

```
-f, --follow=false: Specify if the logs should be streamed.
--interactive=true: If true, prompt the user for input when required. Default true.
```

### Options inherrited from parent commands
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/kubectl-log.1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Print the logs for a container in a pod. If the pod has only one container, the
\fB\-f\fP, \fB\-\-follow\fP=false
Specify if the logs should be streamed.

.PP
\fB\-\-interactive\fP=true
If true, prompt the user for input when required. Default true.


.SH OPTIONS INHERITED FROM PARENT COMMANDS
.PP
Expand Down
39 changes: 35 additions & 4 deletions pkg/kubectl/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ limitations under the License.
package cmd

import (
"fmt"
"io"
"os"
"strconv"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
libutil "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/spf13/cobra"
)

Expand All @@ -32,6 +36,28 @@ $ kubectl log 123456-7890 ruby-container
$ kubectl log -f 123456-7890 ruby-container`
)

func selectContainer(pod *api.Pod, in io.Reader, out io.Writer) string {
fmt.Fprintf(out, "Please select a container:\n")
options := libutil.StringSet{}
for ix := range pod.Spec.Containers {
fmt.Fprintf(out, "[%d] %s\n", ix+1, pod.Spec.Containers[ix].Name)
options.Insert(pod.Spec.Containers[ix].Name)
}
for {
var input string
fmt.Fprintf(out, "> ")
fmt.Fscanln(in, &input)
if options.Has(input) {
return input
}
ix, err := strconv.Atoi(input)
if err == nil && ix > 0 && ix <= len(pod.Spec.Containers) {
return pod.Spec.Containers[ix-1].Name
}
fmt.Fprintf(out, "Invalid input: %s", input)
}
}

func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "log [-f] <pod> [<container>]",
Expand Down Expand Up @@ -60,11 +86,15 @@ func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command {
var container string
if len(args) == 1 {
if len(pod.Spec.Containers) != 1 {
usageError(cmd, "<container> is required for pods with multiple containers")
if !util.GetFlagBool(cmd, "interactive") {
usageError(cmd, "<container> is required for pods with multiple containers")
} else {
container = selectContainer(pod, os.Stdin, out)
}
} else {
// Get logs for the only container in the pod
container = pod.Spec.Containers[0].Name
}

// Get logs for the only container in the pod
container = pod.Spec.Containers[0].Name
} else {
container = args[1]
}
Expand All @@ -89,5 +119,6 @@ func (f *Factory) NewCmdLog(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.")
return cmd
}
147 changes: 147 additions & 0 deletions pkg/kubectl/cmd/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
Copyright 2014 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"bytes"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
)

func TestSelectContainer(t *testing.T) {
tests := []struct {
input string
pod api.Pod
expectedContainer string
}{
{
input: "1\n",
pod: api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "foo",
},
},
},
},
expectedContainer: "foo",
},
{
input: "foo\n",
pod: api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "foo",
},
},
},
},
expectedContainer: "foo",
},
{
input: "foo\n",
pod: api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "bar",
},
{
Name: "foo",
},
},
},
},
expectedContainer: "foo",
},
{
input: "2\n",
pod: api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "bar",
},
{
Name: "foo",
},
},
},
},
expectedContainer: "foo",
},
{
input: "-1\n2\n",
pod: api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "bar",
},
{
Name: "foo",
},
},
},
},
expectedContainer: "foo",
},
{
input: "3\n2\n",
pod: api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "bar",
},
{
Name: "foo",
},
},
},
},
expectedContainer: "foo",
},
{
input: "baz\n2\n",
pod: api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "bar",
},
{
Name: "foo",
},
},
},
},
expectedContainer: "foo",
},
}

for _, test := range tests {
var buff bytes.Buffer
container := selectContainer(&test.pod, bytes.NewBufferString(test.input), &buff)
if container != test.expectedContainer {
t.Errorf("unexpected output: %s for input: %s", container, test.input)
}
}
}