@@ -240,13 +240,18 @@ func LogLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, ct
240
240
}
241
241
242
242
// Try to figure out a container
243
+ // If a container was provided, it must be valid
243
244
container := opts .Container
244
- if container == "" {
245
+ if len ( container ) == 0 {
245
246
if len (pod .Spec .Containers ) == 1 {
246
247
container = pod .Spec .Containers [0 ].Name
247
248
} else {
248
249
return nil , nil , errors .NewBadRequest (fmt .Sprintf ("a container name must be specified for pod %s" , name ))
249
250
}
251
+ } else {
252
+ if ! podHasContainerWithName (pod , container ) {
253
+ return nil , nil , errors .NewBadRequest (fmt .Sprintf ("container %s is not valid for pod %s" , container , name ))
254
+ }
250
255
}
251
256
nodeHost := pod .Spec .NodeName
252
257
if len (nodeHost ) == 0 {
@@ -282,12 +287,21 @@ func LogLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, ct
282
287
loc := & url.URL {
283
288
Scheme : nodeScheme ,
284
289
Host : fmt .Sprintf ("%s:%d" , nodeHost , nodePort ),
285
- Path : fmt .Sprintf ("/containerLogs/%s/%s/%s" , pod .Namespace , name , container ),
290
+ Path : fmt .Sprintf ("/containerLogs/%s/%s/%s" , pod .Namespace , pod . Name , container ),
286
291
RawQuery : params .Encode (),
287
292
}
288
293
return loc , nodeTransport , nil
289
294
}
290
295
296
+ func podHasContainerWithName (pod * api.Pod , containerName string ) bool {
297
+ for _ , c := range pod .Spec .Containers {
298
+ if c .Name == containerName {
299
+ return true
300
+ }
301
+ }
302
+ return false
303
+ }
304
+
291
305
func streamParams (params url.Values , opts runtime.Object ) error {
292
306
switch opts := opts .(type ) {
293
307
case * api.PodExecOptions :
@@ -344,12 +358,17 @@ func streamLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter,
344
358
}
345
359
346
360
// Try to figure out a container
361
+ // If a container was provided, it must be valid
347
362
if container == "" {
348
363
if len (pod .Spec .Containers ) == 1 {
349
364
container = pod .Spec .Containers [0 ].Name
350
365
} else {
351
366
return nil , nil , errors .NewBadRequest (fmt .Sprintf ("a container name must be specified for pod %s" , name ))
352
367
}
368
+ } else {
369
+ if ! podHasContainerWithName (pod , container ) {
370
+ return nil , nil , errors .NewBadRequest (fmt .Sprintf ("container %s is not valid for pod %s" , container , name ))
371
+ }
353
372
}
354
373
nodeHost := pod .Spec .NodeName
355
374
if len (nodeHost ) == 0 {
@@ -367,7 +386,7 @@ func streamLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter,
367
386
loc := & url.URL {
368
387
Scheme : nodeScheme ,
369
388
Host : fmt .Sprintf ("%s:%d" , nodeHost , nodePort ),
370
- Path : fmt .Sprintf ("/%s/%s/%s/%s" , path , pod .Namespace , name , container ),
389
+ Path : fmt .Sprintf ("/%s/%s/%s/%s" , path , pod .Namespace , pod . Name , container ),
371
390
RawQuery : params .Encode (),
372
391
}
373
392
return loc , nodeTransport , nil
@@ -392,7 +411,7 @@ func PortForwardLocation(getter ResourceGetter, connInfo client.ConnectionInfoGe
392
411
loc := & url.URL {
393
412
Scheme : nodeScheme ,
394
413
Host : fmt .Sprintf ("%s:%d" , nodeHost , nodePort ),
395
- Path : fmt .Sprintf ("/portForward/%s/%s" , pod .Namespace , name ),
414
+ Path : fmt .Sprintf ("/portForward/%s/%s" , pod .Namespace , pod . Name ),
396
415
}
397
416
return loc , nodeTransport , nil
398
417
}
0 commit comments