@@ -29,6 +29,7 @@ import (
29
29
"github.com/openshift/origin/pkg/cmd/templates"
30
30
cmdutil "github.com/openshift/origin/pkg/cmd/util"
31
31
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
32
+ deployapi "github.com/openshift/origin/pkg/deploy/api"
32
33
generateapp "github.com/openshift/origin/pkg/generate/app"
33
34
imageapi "github.com/openshift/origin/pkg/image/api"
34
35
)
@@ -394,18 +395,103 @@ func (o *DebugOptions) Debug() error {
394
395
})
395
396
}
396
397
397
- func (o * DebugOptions ) getContainerImageCommand (container * kapi.Container ) ([]string , error ) {
398
- image := container .Image [strings .LastIndex (container .Image , "/" )+ 1 :]
399
- name , id , ok := imageapi .SplitImageStreamImage (image )
400
- if ! ok {
401
- return nil , errors .New ("container image did not contain an id" )
398
+ // getContainerImageViaDeploymentConfig attempts to return an Image for a given
399
+ // Container. It tries to walk from the Container's Pod to its DeploymentConfig
400
+ // (via the "openshift.io/deployment-config.name" annotation), then tries to
401
+ // find the ImageStream from which the DeploymentConfig is deploying, then tries
402
+ // to find a match for the Container's image in the ImageStream's Images.
403
+ func (o * DebugOptions ) getContainerImageViaDeploymentConfig (pod * kapi.Pod , container * kapi.Container ) (* imageapi.Image , error ) {
404
+ ref , err := imageapi .ParseDockerImageReference (container .Image )
405
+ if err != nil {
406
+ return nil , err
407
+ }
408
+
409
+ dcname := pod .Annotations [deployapi .DeploymentConfigAnnotation ]
410
+ if dcname == "" {
411
+ return nil , nil // Pod doesn't appear to have been created by a DeploymentConfig
412
+ }
413
+
414
+ dc , err := o .Client .DeploymentConfigs (o .Attach .Pod .Namespace ).Get (dcname )
415
+ if err != nil {
416
+ return nil , err
417
+ }
418
+
419
+ for _ , trigger := range dc .Spec .Triggers {
420
+ if trigger .Type == deployapi .DeploymentTriggerOnImageChange &&
421
+ trigger .ImageChangeParams != nil &&
422
+ trigger .ImageChangeParams .From .Kind == "ImageStreamTag" {
423
+
424
+ isname , _ , err := imageapi .ParseImageStreamTagName (trigger .ImageChangeParams .From .Name )
425
+ if err != nil {
426
+ return nil , err
427
+ }
428
+
429
+ namespace := trigger .ImageChangeParams .From .Namespace
430
+ if namespace == "" {
431
+ namespace = o .Attach .Pod .Namespace
432
+ }
433
+
434
+ isi , err := o .Client .ImageStreamImages (namespace ).Get (isname , ref .ID )
435
+ if err != nil {
436
+ return nil , err
437
+ }
438
+
439
+ return & isi .Image , nil
440
+ }
441
+ }
442
+
443
+ return nil , nil // DeploymentConfig doesn't have an ImageChange Trigger
444
+ }
445
+
446
+ // getContainerImageViaImageStreamImport attempts to return an Image for a given
447
+ // Container. It does this by submiting a ImageStreamImport request with Import
448
+ // set to false. The request will not succeed if the backing repository
449
+ // requires Insecure to be set to true, which cannot be hard-coded for security
450
+ // reasons.
451
+ func (o * DebugOptions ) getContainerImageViaImageStreamImport (container * kapi.Container ) (* imageapi.Image , error ) {
452
+ isi := & imageapi.ImageStreamImport {
453
+ ObjectMeta : kapi.ObjectMeta {
454
+ Name : "oc-debug" ,
455
+ },
456
+ Spec : imageapi.ImageStreamImportSpec {
457
+ Images : []imageapi.ImageImportSpec {
458
+ {
459
+ From : kapi.ObjectReference {
460
+ Kind : "DockerImage" ,
461
+ Name : container .Image ,
462
+ },
463
+ },
464
+ },
465
+ },
402
466
}
403
- isimage , err := o .Client .ImageStreamImages (o .Attach .Pod .Namespace ).Get (name , id )
467
+
468
+ isi , err := o .Client .ImageStreams (o .Attach .Pod .Namespace ).Import (isi )
404
469
if err != nil {
405
470
return nil , err
406
471
}
407
472
408
- config := isimage .Image .DockerImageMetadata .Config
473
+ if len (isi .Status .Images ) > 0 {
474
+ return isi .Status .Images [0 ].Image , nil
475
+ }
476
+
477
+ return nil , nil
478
+ }
479
+
480
+ func (o * DebugOptions ) getContainerImageCommand (pod * kapi.Pod , container * kapi.Container ) ([]string , error ) {
481
+ if len (container .Command ) > 0 {
482
+ return container .Command , nil
483
+ }
484
+
485
+ image , _ := o .getContainerImageViaDeploymentConfig (pod , container )
486
+ if image == nil {
487
+ image , _ = o .getContainerImageViaImageStreamImport (container )
488
+ }
489
+
490
+ if image == nil || image .DockerImageMetadata .Config == nil {
491
+ return nil , errors .New ("error: no usable image found" )
492
+ }
493
+
494
+ config := image .DockerImageMetadata .Config
409
495
return append (config .Entrypoint , config .Cmd ... ), nil
410
496
}
411
497
@@ -421,13 +507,13 @@ func (o *DebugOptions) transformPodForDebug(annotations map[string]string) (*kap
421
507
container := containerForName (pod , o .Attach .ContainerName )
422
508
423
509
// identify the command to be run
424
- originalCommand := append (container .Command , container .Args ... )
425
- container .Command = o .Command
426
- if len (originalCommand ) == 0 {
427
- originalCommand , _ = o .getContainerImageCommand (container )
510
+ originalCommand , _ := o .getContainerImageCommand (pod , container )
511
+ if len (originalCommand ) > 0 {
512
+ originalCommand = append (originalCommand , container .Args ... )
428
513
}
429
- container .Args = nil
430
514
515
+ container .Command = o .Command
516
+ container .Args = nil
431
517
container .TTY = o .Attach .Stdin && o .Attach .TTY
432
518
container .Stdin = o .Attach .Stdin
433
519
container .StdinOnce = o .Attach .Stdin
0 commit comments