@@ -22,6 +22,7 @@ import (
22
22
"net/http"
23
23
"net/url"
24
24
"strconv"
25
+ "strings"
25
26
"time"
26
27
27
28
"k8s.io/kubernetes/pkg/api"
@@ -231,6 +232,15 @@ func ResourceLocation(getter ResourceGetter, rt http.RoundTripper, ctx api.Conte
231
232
return loc , rt , nil
232
233
}
233
234
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
+
234
244
// LogLocation returns the log URL for a pod container. If opts.Container is blank
235
245
// and only one container is present in the pod, that container is used.
236
246
func LogLocation (
@@ -249,10 +259,14 @@ func LogLocation(
249
259
// If a container was provided, it must be valid
250
260
container := opts .Container
251
261
if len (container ) == 0 {
252
- if len (pod .Spec .Containers ) == 1 {
262
+ switch len (pod .Spec .Containers ) {
263
+ case 1 :
253
264
container = pod .Spec .Containers [0 ].Name
254
- } else {
265
+ case 0 :
255
266
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 ))
256
270
}
257
271
} else {
258
272
if ! podHasContainerWithName (pod , container ) {
@@ -386,10 +400,14 @@ func streamLocation(
386
400
// Try to figure out a container
387
401
// If a container was provided, it must be valid
388
402
if container == "" {
389
- if len (pod .Spec .Containers ) == 1 {
403
+ switch len (pod .Spec .Containers ) {
404
+ case 1 :
390
405
container = pod .Spec .Containers [0 ].Name
391
- } else {
406
+ case 0 :
392
407
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 ))
393
411
}
394
412
} else {
395
413
if ! podHasContainerWithName (pod , container ) {
0 commit comments