@@ -250,7 +250,7 @@ func getPod(getter ResourceGetter, ctx context.Context, name string) (*api.Pod,
250
250
return pod , nil
251
251
}
252
252
253
- // returns primary IP for a Pod
253
+ // getPodIP returns primary IP for a Pod
254
254
func getPodIP (pod * api.Pod ) string {
255
255
if pod == nil {
256
256
return ""
@@ -327,25 +327,9 @@ func LogLocation(
327
327
// Try to figure out a container
328
328
// If a container was provided, it must be valid
329
329
container := opts .Container
330
- if len (container ) == 0 {
331
- switch len (pod .Spec .Containers ) {
332
- case 1 :
333
- container = pod .Spec .Containers [0 ].Name
334
- case 0 :
335
- return nil , nil , errors .NewBadRequest (fmt .Sprintf ("a container name must be specified for pod %s" , name ))
336
- default :
337
- containerNames := getContainerNames (pod .Spec .Containers )
338
- initContainerNames := getContainerNames (pod .Spec .InitContainers )
339
- err := fmt .Sprintf ("a container name must be specified for pod %s, choose one of: [%s]" , name , containerNames )
340
- if len (initContainerNames ) > 0 {
341
- err += fmt .Sprintf (" or one of the init containers: [%s]" , initContainerNames )
342
- }
343
- return nil , nil , errors .NewBadRequest (err )
344
- }
345
- } else {
346
- if ! podHasContainerWithName (pod , container ) {
347
- return nil , nil , errors .NewBadRequest (fmt .Sprintf ("container %s is not valid for pod %s" , container , name ))
348
- }
330
+ container , err = validateContainer (container , pod )
331
+ if err != nil {
332
+ return nil , nil , err
349
333
}
350
334
nodeName := types .NodeName (pod .Spec .NodeName )
351
335
if len (nodeName ) == 0 {
@@ -488,26 +472,11 @@ func streamLocation(
488
472
489
473
// Try to figure out a container
490
474
// If a container was provided, it must be valid
491
- if container == "" {
492
- switch len (pod .Spec .Containers ) {
493
- case 1 :
494
- container = pod .Spec .Containers [0 ].Name
495
- case 0 :
496
- return nil , nil , errors .NewBadRequest (fmt .Sprintf ("a container name must be specified for pod %s" , name ))
497
- default :
498
- containerNames := getContainerNames (pod .Spec .Containers )
499
- initContainerNames := getContainerNames (pod .Spec .InitContainers )
500
- err := fmt .Sprintf ("a container name must be specified for pod %s, choose one of: [%s]" , name , containerNames )
501
- if len (initContainerNames ) > 0 {
502
- err += fmt .Sprintf (" or one of the init containers: [%s]" , initContainerNames )
503
- }
504
- return nil , nil , errors .NewBadRequest (err )
505
- }
506
- } else {
507
- if ! podHasContainerWithName (pod , container ) {
508
- return nil , nil , errors .NewBadRequest (fmt .Sprintf ("container %s is not valid for pod %s" , container , name ))
509
- }
475
+ container , err = validateContainer (container , pod )
476
+ if err != nil {
477
+ return nil , nil , err
510
478
}
479
+
511
480
nodeName := types .NodeName (pod .Spec .NodeName )
512
481
if len (nodeName ) == 0 {
513
482
// If pod has not been assigned a host, return an empty location
@@ -564,3 +533,29 @@ func PortForwardLocation(
564
533
}
565
534
return loc , nodeInfo .Transport , nil
566
535
}
536
+
537
+ // validateContainer validate container is valid for pod, return valid container
538
+ func validateContainer (container string , pod * api.Pod ) (string , error ) {
539
+ if len (container ) == 0 {
540
+ switch len (pod .Spec .Containers ) {
541
+ case 1 :
542
+ container = pod .Spec .Containers [0 ].Name
543
+ case 0 :
544
+ return "" , errors .NewBadRequest (fmt .Sprintf ("a container name must be specified for pod %s" , pod .Name ))
545
+ default :
546
+ containerNames := getContainerNames (pod .Spec .Containers )
547
+ initContainerNames := getContainerNames (pod .Spec .InitContainers )
548
+ err := fmt .Sprintf ("a container name must be specified for pod %s, choose one of: [%s]" , pod .Name , containerNames )
549
+ if len (initContainerNames ) > 0 {
550
+ err += fmt .Sprintf (" or one of the init containers: [%s]" , initContainerNames )
551
+ }
552
+ return "" , errors .NewBadRequest (err )
553
+ }
554
+ } else {
555
+ if ! podHasContainerWithName (pod , container ) {
556
+ return "" , errors .NewBadRequest (fmt .Sprintf ("container %s is not valid for pod %s" , container , pod .Name ))
557
+ }
558
+ }
559
+
560
+ return container , nil
561
+ }
0 commit comments