@@ -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.
236246func 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 ) {
0 commit comments