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

Modifies kubectl log cmd to not require container arg for single-contain... #2871

Merged
merged 2 commits into from
Dec 12, 2014
Merged
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
35 changes: 27 additions & 8 deletions pkg/kubectl/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,46 @@ import (

func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "log <pod> <container>",
Short: "Print the logs for a container in a pod",
Use: "log <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.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
usageError(cmd, "<pod> and <container> are required for log")
if len(args) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check > 2

usageError(cmd, "<pod> is required for log")
}

namespace := GetKubeNamespace(cmd)
if len(args) > 2 {
usageError(cmd, "log <pod> [<container>]")
}

namespace := GetKubeNamespace(cmd)
client, err := f.ClientBuilder.Client()
checkErr(err)
pod, err := client.Pods(namespace).Get(args[0])

podID := args[0]

pod, err := client.Pods(namespace).Get(podID)
checkErr(err)

var container string
if len(args) == 1 {
if len(pod.Spec.Containers) != 1 {
usageError(cmd, "<container> is required for pods with multiple containers")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invert this condition and remove the else - usageError terminates execution

}

// Get logs for the only container in the pod
container = pod.Spec.Containers[0].Name
} else {
container = args[1]
}

data, err := client.RESTClient.Get().
Path("proxy/minions").
Path(pod.Status.Host).
Path("containerLogs").
Path(namespace).
Path(args[0]).
Path(args[1]).
Path(podID).
Path(container).
Do().
Raw()
checkErr(err)
Expand Down