44 "context"
55 "fmt"
66 "io"
7+ "strings"
78
89 "github.com/docker/docker/api/types"
910 "github.com/docker/docker/api/types/container"
@@ -16,39 +17,6 @@ import (
1617 "github.com/iximiuz/cdebug/pkg/uuid"
1718)
1819
19- // debugImageExistsLocally checks if the debug image exists in the host and that it matches the target container
20- // platform.
21- func debugImageExistsLocally (ctx context.Context , client * docker.Client , debugImage string , debugImagePlatform string , target types.ContainerJSON ) (bool , error ) {
22- debugImageInspect , _ , err := client .ImageInspectWithRaw (ctx , debugImage )
23- if err != nil {
24- // err means that the requested image wasn't found,
25- // therefore it needs to be pulled
26- logrus .Debugf ("The image %s wasn't found locally" , debugImage )
27- return false , nil
28- }
29-
30- debugImageOs := debugImageInspect .Os
31- debugImageArchitecture := debugImageInspect .Architecture
32- // override through --platform flag
33- if debugImagePlatform != "" {
34- fmt .Sscanf (debugImagePlatform , "%s/%s" , & debugImageOs , & debugImageArchitecture )
35- }
36-
37- // target.Platform only contains the Os but doesn't have the Arch, however we can
38- // get the Os & Arch by analyzing the image using target.Image
39- targetImageInspect , _ , err := client .ImageInspectWithRaw (ctx , target .Image )
40- if err != nil {
41- return false , fmt .Errorf ("failed to inspect image %s: %v" , target .Image , err )
42- }
43-
44- // debug image exists, also check that the platform matches the target image platform
45- if debugImageArchitecture != targetImageInspect .Architecture || debugImageOs != targetImageInspect .Os {
46- return false , nil
47- }
48-
49- return true , nil
50- }
51-
5220func runDebuggerDocker (ctx context.Context , cli cliutil.CLI , opts * options ) error {
5321 client , err := docker .NewClient (docker.Options {
5422 Out : cli .AuxStream (),
@@ -66,20 +34,19 @@ func runDebuggerDocker(ctx context.Context, cli cliutil.CLI, opts *options) erro
6634 return errTargetNotRunning
6735 }
6836
69- imageExists , err := debugImageExistsLocally (ctx , client , opts .image , opts .platform , target )
37+ platform := opts .platform
38+ if len (platform ) == 0 {
39+ platform = target .Platform
40+ }
41+
42+ imageExists , err := imageExistsLocally (ctx , client , opts .image , platform )
7043 if err != nil {
7144 return err
7245 }
73-
7446 if ! imageExists {
7547 cli .PrintAux ("Pulling debugger image...\n " )
7648 if err := client .ImagePullEx (ctx , opts .image , types.ImagePullOptions {
77- Platform : func () string {
78- if len (opts .platform ) == 0 {
79- return target .Platform
80- }
81- return opts .platform
82- }(),
49+ Platform : platform ,
8350 }); err != nil {
8451 return errCannotPull (opts .image , err )
8552 }
@@ -265,3 +232,34 @@ func (s *ioStreamer) stream(ctx context.Context) error {
265232func ptr [T any ](v T ) * T {
266233 return & v
267234}
235+
236+ func imageExistsLocally (
237+ ctx context.Context ,
238+ client * docker.Client ,
239+ image string ,
240+ platform string ,
241+ ) (bool , error ) {
242+ imageSummary , _ , err := client .ImageInspectWithRaw (ctx , image )
243+ if err != nil {
244+ logrus .Debugf ("The image %s (%s) wasn't found locally" , image , platform )
245+ return false , nil
246+ }
247+
248+ parts := strings .Split (platform , "/" )
249+ if imageSummary .Os != parts [0 ] {
250+ logrus .Debugf ("The image %s (%s) found locally, but the OS doesn't match" , image , platform )
251+ return false , nil
252+ }
253+
254+ if len (parts ) > 1 && imageSummary .Architecture != parts [1 ] {
255+ logrus .Debugf ("The image %s (%s) found locally, but the architecture doesn't match" , image , platform )
256+ return false , nil
257+ }
258+
259+ if len (parts ) > 2 && imageSummary .Variant != parts [2 ] {
260+ logrus .Debugf ("The image %s (%s) found locally, but the variant doesn't match" , image , platform )
261+ return false , nil
262+ }
263+
264+ return true , nil
265+ }
0 commit comments