Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit e5009bf

Browse files
author
k8s-merge-robot
committed
Merge pull request kubernetes#19474 from endocode/container_names_in_kubectl_logs
Auto commit by PR queue bot
2 parents c75e344 + 6cd10d4 commit e5009bf

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

pkg/registry/pod/strategy.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/http"
2323
"net/url"
2424
"strconv"
25+
"strings"
2526
"time"
2627

2728
"k8s.io/kubernetes/pkg/api"
@@ -231,6 +232,15 @@ func ResourceLocation(getter ResourceGetter, rt http.RoundTripper, ctx api.Conte
231232
return loc, rt, nil
232233
}
233234

235+
// getContainerNames returns a formatted string containing the container names
236+
func getContainerNames(pod *api.Pod) string {
237+
names := []string{}
238+
for _, c := range pod.Spec.Containers {
239+
names = append(names, c.Name)
240+
}
241+
return strings.Join(names, " ")
242+
}
243+
234244
// LogLocation returns the log URL for a pod container. If opts.Container is blank
235245
// and only one container is present in the pod, that container is used.
236246
func LogLocation(
@@ -249,10 +259,14 @@ func LogLocation(
249259
// If a container was provided, it must be valid
250260
container := opts.Container
251261
if len(container) == 0 {
252-
if len(pod.Spec.Containers) == 1 {
262+
switch len(pod.Spec.Containers) {
263+
case 1:
253264
container = pod.Spec.Containers[0].Name
254-
} else {
265+
case 0:
255266
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
267+
default:
268+
containerNames := getContainerNames(pod)
269+
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames))
256270
}
257271
} else {
258272
if !podHasContainerWithName(pod, container) {
@@ -386,10 +400,14 @@ func streamLocation(
386400
// Try to figure out a container
387401
// If a container was provided, it must be valid
388402
if container == "" {
389-
if len(pod.Spec.Containers) == 1 {
403+
switch len(pod.Spec.Containers) {
404+
case 1:
390405
container = pod.Spec.Containers[0].Name
391-
} else {
406+
case 0:
392407
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
408+
default:
409+
containerNames := getContainerNames(pod)
410+
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames))
393411
}
394412
} else {
395413
if !podHasContainerWithName(pod, container) {

pkg/registry/pod/strategy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestCheckLogLocation(t *testing.T) {
129129
Status: api.PodStatus{},
130130
},
131131
opts: &api.PodLogOptions{},
132-
expectedErr: errors.NewBadRequest("a container name must be specified for pod test"),
132+
expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [container1 container2]"),
133133
},
134134
{
135135
in: &api.Pod{

0 commit comments

Comments
 (0)