Skip to content

Commit aac0f96

Browse files
committed
Pass platform to docker inspect, default local platform to linux/$GOARCH
Signed-off-by: Bozhidar Marinov <bozhidar.marinov1@mail.schwarz>
1 parent 51c1c8c commit aac0f96

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

local/new.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package local
33
import (
44
"context"
55
"fmt"
6+
"runtime"
67

78
cerrdefs "github.com/containerd/errdefs"
89
v1 "github.com/google/go-containerregistry/pkg/v1"
910
"github.com/moby/moby/api/types/image"
1011
"github.com/moby/moby/client"
12+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1113

1214
"github.com/buildpacks/imgutil"
1315
)
@@ -26,7 +28,7 @@ func NewImage(repoName string, dockerClient DockerClient, ops ...imgutil.ImageOp
2628
return nil, err
2729
}
2830

29-
previousImage, err := processImageOption(options.PreviousImageRepoName, dockerClient, true)
31+
previousImage, err := processImageOption(options.PreviousImageRepoName, options.Platform, dockerClient, true)
3032
if err != nil {
3133
return nil, err
3234
}
@@ -38,7 +40,7 @@ func NewImage(repoName string, dockerClient DockerClient, ops ...imgutil.ImageOp
3840
baseIdentifier string
3941
store *Store
4042
)
41-
baseImage, err := processImageOption(options.BaseImageRepoName, dockerClient, false)
43+
baseImage, err := processImageOption(options.BaseImageRepoName, options.Platform, dockerClient, false)
4244
if err != nil {
4345
return nil, err
4446
}
@@ -69,6 +71,12 @@ func defaultPlatform(dockerClient DockerClient) (imgutil.Platform, error) {
6971
if err != nil {
7072
return imgutil.Platform{}, err
7173
}
74+
if daemonInfo.Os == "linux" {
75+
return imgutil.Platform{
76+
OS: "linux",
77+
Architecture: runtime.GOARCH,
78+
}, nil
79+
}
7280
return imgutil.Platform{
7381
OS: daemonInfo.Os,
7482
Architecture: daemonInfo.Arch,
@@ -96,11 +104,11 @@ type imageResult struct {
96104
layerStore *Store
97105
}
98106

99-
func processImageOption(repoName string, dockerClient DockerClient, downloadLayersOnAccess bool) (imageResult, error) {
107+
func processImageOption(repoName string, platform imgutil.Platform, dockerClient DockerClient, downloadLayersOnAccess bool) (imageResult, error) {
100108
if repoName == "" {
101109
return imageResult{}, nil
102110
}
103-
inspect, history, err := getInspectAndHistory(repoName, dockerClient)
111+
inspect, history, err := getInspectAndHistory(repoName, platform, dockerClient)
104112
if err != nil {
105113
return imageResult{}, err
106114
}
@@ -119,8 +127,19 @@ func processImageOption(repoName string, dockerClient DockerClient, downloadLaye
119127
}, nil
120128
}
121129

122-
func getInspectAndHistory(repoName string, dockerClient DockerClient) (*image.InspectResponse, []image.HistoryResponseItem, error) {
123-
inspect, err := dockerClient.ImageInspect(context.Background(), repoName)
130+
func getInspectAndHistory(repoName string, platform imgutil.Platform, dockerClient DockerClient) (*image.InspectResponse, []image.HistoryResponseItem, error) {
131+
platformOpt := client.ImageInspectWithPlatform(&ocispec.Platform{
132+
Architecture: platform.Architecture,
133+
OS: platform.OS,
134+
OSVersion: platform.OSVersion,
135+
Variant: platform.Variant,
136+
})
137+
// Try to inspect the image with the default platform/arch
138+
inspect, err := dockerClient.ImageInspect(context.Background(), repoName, platformOpt)
139+
if err != nil {
140+
// ...and if that fails, inspect without the platfrom
141+
inspect, err = dockerClient.ImageInspect(context.Background(), repoName)
142+
}
124143
if err != nil {
125144
if cerrdefs.IsNotFound(err) {
126145
return nil, nil, nil

local/v1_facade.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func newV1ImageFacadeFromInspect(dockerInspect image.InspectResponse, history []
2727
return nil, err
2828
}
2929
configFile := &v1.ConfigFile{
30-
Architecture: dockerInspect.Architecture, // FIXME: this should come from options.Platform
30+
Architecture: dockerInspect.Architecture,
3131
Author: dockerInspect.Author,
3232
Created: toV1Time(dockerInspect.Created),
3333
History: imgutil.NormalizedHistory(toV1History(history), len(dockerInspect.RootFS.Layers)),
3434
OS: dockerInspect.Os,
3535
RootFS: rootFS,
3636
Config: toV1Config(dockerInspect.Config),
37-
OSVersion: dockerInspect.OsVersion, // FIXME: this should come from options.Platform
38-
Variant: dockerInspect.Variant, // FIXME: this should come from options.Platform
37+
OSVersion: dockerInspect.OsVersion,
38+
Variant: dockerInspect.Variant,
3939
}
4040
layersToSet := newEmptyLayerListFrom(configFile, downloadLayersOnAccess, withStore, dockerInspect.ID)
4141
return imageFrom(layersToSet, configFile, imgutil.DockerTypes) // FIXME: this should be configurable with options.MediaTypes

0 commit comments

Comments
 (0)