@@ -17,6 +17,7 @@ import (
1717 "github.com/containers/buildah/define"
1818 "github.com/containers/buildah/docker"
1919 "github.com/containers/buildah/imagebuildah"
20+ "github.com/containers/buildah/pkg/parse"
2021 "github.com/containers/common/libimage"
2122 "github.com/containers/image/v5/manifest"
2223 imgstor "github.com/containers/image/v5/storage"
@@ -62,6 +63,8 @@ type NativeBuildah struct {
6263 Runtime libimage.Runtime
6364 DefaultSystemContext imgtypes.SystemContext
6465 DefaultCommonBuildOptions define.CommonBuildOptions
66+
67+ platforms []struct { OS , Arch , Variant string }
6568}
6669
6770func NewNativeBuildah (commonOpts CommonBuildahOpts , opts NativeModeOpts ) (* NativeBuildah , error ) {
@@ -95,6 +98,21 @@ func NewNativeBuildah(commonOpts CommonBuildahOpts, opts NativeModeOpts) (*Nativ
9598 DockerDaemonInsecureSkipTLSVerify : b .Insecure ,
9699 }
97100
101+ if opts .Platform != "" {
102+ os , arch , variant , err := parse .Platform (opts .Platform )
103+ if err != nil {
104+ return nil , fmt .Errorf ("unable to parse platform %q: %w" , opts .Platform , err )
105+ }
106+
107+ b .DefaultSystemContext .OSChoice = os
108+ b .DefaultSystemContext .ArchitectureChoice = arch
109+ b .DefaultSystemContext .VariantChoice = variant
110+
111+ b .platforms = []struct { OS , Arch , Variant string }{
112+ {os , arch , variant },
113+ }
114+ }
115+
98116 b .DefaultCommonBuildOptions = define.CommonBuildOptions {
99117 ShmSize : DefaultShmSize ,
100118 }
@@ -177,6 +195,7 @@ func (b *NativeBuildah) BuildFromDockerfile(ctx context.Context, dockerfile []by
177195 Target : opts .Target ,
178196 MaxPullPushRetries : MaxPullPushRetries ,
179197 PullPushRetryDelay : PullPushRetryDelay ,
198+ Platforms : b .platforms ,
180199 }
181200
182201 errLog := & bytes.Buffer {}
@@ -287,10 +306,36 @@ func (b *NativeBuildah) Pull(ctx context.Context, ref string, opts PullOpts) err
287306 PullPolicy : define .PullIfNewer ,
288307 }
289308
290- if _ , err := buildah .Pull (ctx , ref , pullOpts ); err != nil {
309+ imageID , err := buildah .Pull (ctx , ref , pullOpts )
310+ if err != nil {
291311 return fmt .Errorf ("error pulling image %q: %w" , ref , err )
292312 }
293313
314+ imageInspect , err := b .Inspect (ctx , imageID )
315+ if err != nil {
316+ return fmt .Errorf ("unable to inspect pulled image %q: %w" , imageID , err )
317+ }
318+
319+ platformMismatch := false
320+ if b .DefaultSystemContext .OSChoice != "" && b .DefaultSystemContext .OSChoice != imageInspect .OCIv1 .OS {
321+ platformMismatch = true
322+ }
323+ if b .DefaultSystemContext .ArchitectureChoice != "" && b .DefaultSystemContext .ArchitectureChoice != imageInspect .OCIv1 .Architecture {
324+ platformMismatch = true
325+ }
326+ if b .DefaultSystemContext .VariantChoice != "" && b .DefaultSystemContext .VariantChoice != imageInspect .OCIv1 .Variant {
327+ platformMismatch = true
328+ }
329+
330+ if platformMismatch {
331+ imagePlatform := fmt .Sprintf ("%s/%s/%s" , imageInspect .OCIv1 .OS , imageInspect .OCIv1 .Architecture , imageInspect .OCIv1 .Variant )
332+ expectedPlatform := fmt .Sprintf ("%s/%s" , b .DefaultSystemContext .OSChoice , b .DefaultSystemContext .ArchitectureChoice )
333+ if b .DefaultSystemContext .VariantChoice != "" {
334+ expectedPlatform = fmt .Sprintf ("%s/%s" , expectedPlatform , b .DefaultSystemContext .VariantChoice )
335+ }
336+ return fmt .Errorf ("image platform mismatch: image uses %s, expecting %s platform" , imagePlatform , expectedPlatform )
337+ }
338+
294339 return nil
295340}
296341
0 commit comments