From 6cd10d4f8724b550cffce581a965d132f3b448fb Mon Sep 17 00:00:00 2001 From: Julian Strobl Date: Mon, 11 Jan 2016 15:26:44 +0100 Subject: [PATCH] add container names kubectl logs output Present a list of container names one can choose from. --- pkg/registry/pod/strategy.go | 26 ++++++++++++++++++++++---- pkg/registry/pod/strategy_test.go | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/pkg/registry/pod/strategy.go b/pkg/registry/pod/strategy.go index 4e957fd1eb7..ffdeb988ef1 100644 --- a/pkg/registry/pod/strategy.go +++ b/pkg/registry/pod/strategy.go @@ -22,6 +22,7 @@ import ( "net/http" "net/url" "strconv" + "strings" "time" "k8s.io/kubernetes/pkg/api" @@ -231,6 +232,15 @@ func ResourceLocation(getter ResourceGetter, rt http.RoundTripper, ctx api.Conte return loc, rt, nil } +// getContainerNames returns a formatted string containing the container names +func getContainerNames(pod *api.Pod) string { + names := []string{} + for _, c := range pod.Spec.Containers { + names = append(names, c.Name) + } + return strings.Join(names, " ") +} + // LogLocation returns the log URL for a pod container. If opts.Container is blank // and only one container is present in the pod, that container is used. func LogLocation( @@ -249,10 +259,14 @@ func LogLocation( // If a container was provided, it must be valid container := opts.Container if len(container) == 0 { - if len(pod.Spec.Containers) == 1 { + switch len(pod.Spec.Containers) { + case 1: container = pod.Spec.Containers[0].Name - } else { + case 0: return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name)) + default: + containerNames := getContainerNames(pod) + return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames)) } } else { if !podHasContainerWithName(pod, container) { @@ -386,10 +400,14 @@ func streamLocation( // Try to figure out a container // If a container was provided, it must be valid if container == "" { - if len(pod.Spec.Containers) == 1 { + switch len(pod.Spec.Containers) { + case 1: container = pod.Spec.Containers[0].Name - } else { + case 0: return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name)) + default: + containerNames := getContainerNames(pod) + return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames)) } } else { if !podHasContainerWithName(pod, container) { diff --git a/pkg/registry/pod/strategy_test.go b/pkg/registry/pod/strategy_test.go index 17e746dd9b2..08ed1680e18 100644 --- a/pkg/registry/pod/strategy_test.go +++ b/pkg/registry/pod/strategy_test.go @@ -129,7 +129,7 @@ func TestCheckLogLocation(t *testing.T) { Status: api.PodStatus{}, }, opts: &api.PodLogOptions{}, - expectedErr: errors.NewBadRequest("a container name must be specified for pod test"), + expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [container1 container2]"), }, { in: &api.Pod{