@@ -3,56 +3,75 @@ package images
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "os"
7
+ "path/filepath"
6
8
"strings"
7
9
8
10
"github.com/containers/image/v5/manifest"
9
11
"github.com/containers/image/v5/transports/alltransports"
10
12
"github.com/containers/image/v5/types"
11
13
)
12
14
13
- // ToFullURL converts the provided image name to the full URL, if already not in that format.
14
- // e.g. nginx becomes docker.io/library/nginx
15
- // If already in full URL format, image name is returned as is.
16
- func ToFullURL (imgName string ) string {
17
- splits := strings .Split (imgName , "/" )
18
- if len (splits ) > 2 {
19
- return imgName
20
- }
21
- if len (splits ) == 2 {
22
- return fmt .Sprintf ("docker.io/%s" , imgName )
15
+ func getDockerConfigPath () string {
16
+ home , err := os .UserHomeDir ()
17
+ if err != nil {
18
+ return ""
23
19
}
24
- return fmt . Sprintf ( "docker.io/library/%s " , imgName )
20
+ return filepath . Join ( home , ".docker " , "config.json" )
25
21
}
26
22
27
23
// CheckLinuxArm64Support checks for the existance of an arm64 linux image in the manifest
28
24
func CheckLinuxArm64Support (imgName string ) (bool , error ) {
29
25
sys := & types.SystemContext {
30
- ArchitectureChoice : "arm64" ,
31
- OSChoice : "linux" ,
26
+ ArchitectureChoice : "arm64" ,
27
+ OSChoice : "linux" ,
28
+ DockerCompatAuthFilePath : getDockerConfigPath (),
32
29
}
33
30
34
31
ref , err := alltransports .ParseImageName (fmt .Sprintf ("docker://%s" , imgName ))
35
32
if err != nil {
36
- return false , err
33
+ return false , fmt . Errorf ( "error parsing image name: %w" , err )
37
34
}
38
35
39
36
imageSource , err := ref .NewImageSource (context .Background (), sys )
40
37
if err != nil {
41
- return false , err
38
+ return false , fmt . Errorf ( "error getting image source: %w" , err )
42
39
}
43
40
defer imageSource .Close ()
44
41
45
42
rawManifest , mimeType , err := imageSource .GetManifest (context .Background (), nil )
46
43
if err != nil {
47
- return false , err
44
+ return false , fmt . Errorf ( "error getting manifest: %w" , err )
48
45
}
49
46
50
- manifestList , err := manifest .ListFromBlob (rawManifest , mimeType )
51
- if err != nil {
52
- return false , err
47
+ if manifest .MIMETypeIsMultiImage (mimeType ) {
48
+ manifestList , err := manifest .ListFromBlob (rawManifest , mimeType )
49
+ if err != nil {
50
+ return false , err
51
+ }
52
+
53
+ // This call will error if it cannot find a instance that supports linux arm64
54
+ _ , err = manifestList .ChooseInstance (sys )
55
+ return err == nil , nil
56
+ } else {
57
+ // m, err := manifest.FromBlob(rawManifest, mimeType)
58
+ // if err != nil {
59
+ // return false, nil
60
+ // }
61
+ // mInfo, err := m.Inspect(nil)
62
+ // if err != nil {
63
+ // return false, nil
64
+ // }
65
+ // return mInfo.Architecture == "arm64", nil
66
+ return false , fmt .Errorf ("image manifests not supported" )
53
67
}
68
+ }
54
69
55
- // This call will error if it cannot find a instance that supports linux arm64
56
- _ , err = manifestList .ChooseInstance (sys )
57
- return err == nil , nil
70
+ func CheckLatestLinuxArm64Support (imgName string ) (bool , error ) {
71
+ split := strings .Split (imgName , ":" )
72
+ if len (split ) < 2 {
73
+ return false , fmt .Errorf ("invalid image name" )
74
+ }
75
+ latestImageName := split [0 ] + ":latest"
76
+ return CheckLinuxArm64Support (latestImageName )
58
77
}
0 commit comments