@@ -195,13 +195,18 @@ func LogLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, ct
195
195
}
196
196
197
197
// Try to figure out a container
198
+ // If a container was provided, it must be valid
198
199
container := opts .Container
199
- if container == "" {
200
+ if len ( container ) == 0 {
200
201
if len (pod .Spec .Containers ) == 1 {
201
202
container = pod .Spec .Containers [0 ].Name
202
203
} else {
203
204
return nil , nil , errors .NewBadRequest (fmt .Sprintf ("a container name must be specified for pod %s" , name ))
204
205
}
206
+ } else {
207
+ if ! podHasContainerWithName (pod , container ) {
208
+ return nil , nil , errors .NewBadRequest (fmt .Sprintf ("container %s is not valid for pod %s" , container , name ))
209
+ }
205
210
}
206
211
nodeHost := pod .Spec .NodeName
207
212
if len (nodeHost ) == 0 {
@@ -222,12 +227,21 @@ func LogLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, ct
222
227
loc := & url.URL {
223
228
Scheme : nodeScheme ,
224
229
Host : fmt .Sprintf ("%s:%d" , nodeHost , nodePort ),
225
- Path : fmt .Sprintf ("/containerLogs/%s/%s/%s" , pod .Namespace , name , container ),
230
+ Path : fmt .Sprintf ("/containerLogs/%s/%s/%s" , pod .Namespace , pod . Name , container ),
226
231
RawQuery : params .Encode (),
227
232
}
228
233
return loc , nodeTransport , nil
229
234
}
230
235
236
+ func podHasContainerWithName (pod * api.Pod , containerName string ) bool {
237
+ for _ , c := range pod .Spec .Containers {
238
+ if c .Name == containerName {
239
+ return true
240
+ }
241
+ }
242
+ return false
243
+ }
244
+
231
245
// ExecLocation returns the exec URL for a pod container. If opts.Container is blank
232
246
// and only one container is present in the pod, that container is used.
233
247
func ExecLocation (getter ResourceGetter , connInfo client.ConnectionInfoGetter , ctx api.Context , name string , opts * api.PodExecOptions ) (* url.URL , http.RoundTripper , error ) {
@@ -238,13 +252,18 @@ func ExecLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, c
238
252
}
239
253
240
254
// Try to figure out a container
255
+ // If a container was provided, it must be valid
241
256
container := opts .Container
242
- if container == "" {
257
+ if len ( container ) == 0 {
243
258
if len (pod .Spec .Containers ) == 1 {
244
259
container = pod .Spec .Containers [0 ].Name
245
260
} else {
246
261
return nil , nil , errors .NewBadRequest (fmt .Sprintf ("a container name must be specified for pod %s" , name ))
247
262
}
263
+ } else {
264
+ if ! podHasContainerWithName (pod , container ) {
265
+ return nil , nil , errors .NewBadRequest (fmt .Sprintf ("container %s is not valid for pod %s" , container , name ))
266
+ }
248
267
}
249
268
nodeHost := pod .Spec .NodeName
250
269
if len (nodeHost ) == 0 {
@@ -274,7 +293,7 @@ func ExecLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, c
274
293
loc := & url.URL {
275
294
Scheme : nodeScheme ,
276
295
Host : fmt .Sprintf ("%s:%d" , nodeHost , nodePort ),
277
- Path : fmt .Sprintf ("/exec/%s/%s/%s" , pod .Namespace , name , container ),
296
+ Path : fmt .Sprintf ("/exec/%s/%s/%s" , pod .Namespace , pod . Name , container ),
278
297
RawQuery : params .Encode (),
279
298
}
280
299
return loc , nodeTransport , nil
@@ -300,7 +319,7 @@ func PortForwardLocation(getter ResourceGetter, connInfo client.ConnectionInfoGe
300
319
loc := & url.URL {
301
320
Scheme : nodeScheme ,
302
321
Host : fmt .Sprintf ("%s:%d" , nodeHost , nodePort ),
303
- Path : fmt .Sprintf ("/portForward/%s/%s" , pod .Namespace , name ),
322
+ Path : fmt .Sprintf ("/portForward/%s/%s" , pod .Namespace , pod . Name ),
304
323
}
305
324
return loc , nodeTransport , nil
306
325
}
0 commit comments