Skip to content

Commit

Permalink
Ensure the run image os/arch always matches:
Browse files Browse the repository at this point in the history
- the builder for `pack build`
- the previous image for `pack rebase`

Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Oct 12, 2023
1 parent 3a994bd commit 5728158
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
36 changes: 22 additions & 14 deletions pkg/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,28 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
return errors.Wrapf(err, "failed to fetch builder image '%s'", builderRef.Name())
}

builderOS, err := rawBuilderImage.OS()
if err != nil {
return errors.Wrapf(err, "getting builder OS")
}

builderArch, err := rawBuilderImage.Architecture()
if err != nil {
return errors.Wrapf(err, "getting builder architecture")
}

bldr, err := c.getBuilder(rawBuilderImage)
if err != nil {
return errors.Wrapf(err, "invalid builder %s", style.Symbol(opts.Builder))
}

runImageName := c.resolveRunImage(opts.RunImage, imgRegistry, builderRef.Context().RegistryStr(), bldr.DefaultRunImage(), opts.AdditionalMirrors, opts.Publish)

fetchOptions := image.FetchOptions{Daemon: !opts.Publish, PullPolicy: opts.PullPolicy}
fetchOptions := image.FetchOptions{
Daemon: !opts.Publish,
PullPolicy: opts.PullPolicy,
Platform: fmt.Sprintf("%s/%s", builderOS, builderArch),
}
if opts.Layout() {
targetRunImagePath, err := layout.ParseRefToPath(runImageName)
if err != nil {
Expand Down Expand Up @@ -361,11 +375,6 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
return err
}

imgOS, err := rawBuilderImage.OS()
if err != nil {
return errors.Wrapf(err, "getting builder OS")
}

// Default mode: if the TrustBuilder option is not set, trust the suggested builders.
if opts.TrustBuilder == nil {
opts.TrustBuilder = IsSuggestedBuilderFunc
Expand Down Expand Up @@ -396,15 +405,14 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
lifecycleImageName = fmt.Sprintf("%s:%s", internalConfig.DefaultLifecycleImageRepo, lifecycleVersion.String())
}

imgArch, err := rawBuilderImage.Architecture()
if err != nil {
return errors.Wrapf(err, "getting builder architecture")
}

lifecycleImage, err := c.imageFetcher.Fetch(
ctx,
lifecycleImageName,
image.FetchOptions{Daemon: true, PullPolicy: opts.PullPolicy, Platform: fmt.Sprintf("%s/%s", imgOS, imgArch)},
image.FetchOptions{
Daemon: true,
PullPolicy: opts.PullPolicy,
Platform: fmt.Sprintf("%s/%s", builderOS, builderArch),
},
)
if err != nil {
return fmt.Errorf("fetching lifecycle image: %w", err)
Expand Down Expand Up @@ -455,7 +463,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
if !c.experimental {
return fmt.Errorf("experimental features must be enabled when builder contains image extensions")
}
if imgOS == "windows" {
if builderOS == "windows" {
return fmt.Errorf("builder contains image extensions which are not supported for Windows builds")
}
if !(opts.PullPolicy == image.PullAlways) {
Expand All @@ -467,7 +475,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
opts.ContainerConfig.Volumes = appendLayoutVolumes(opts.ContainerConfig.Volumes, pathsConfig)
}

processedVolumes, warnings, err := processVolumes(imgOS, opts.ContainerConfig.Volumes)
processedVolumes, warnings, err := processVolumes(builderOS, opts.ContainerConfig.Volumes)
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/client/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2085,11 +2085,12 @@ api = "0.2"
}))
h.AssertEq(t, fakeLifecycle.Opts.Publish, true)

args := fakeImageFetcher.FetchCalls["default/run"]
h.AssertEq(t, args.Daemon, false)

args = fakeImageFetcher.FetchCalls[defaultBuilderName]
args := fakeImageFetcher.FetchCalls[defaultBuilderName]
h.AssertEq(t, args.Daemon, true)

args = fakeImageFetcher.FetchCalls["default/run"]
h.AssertEq(t, args.Daemon, false)
h.AssertEq(t, args.Platform, "linux/amd64")
})

when("builder is untrusted", func() {
Expand Down
17 changes: 16 additions & 1 deletion pkg/client/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -60,6 +61,16 @@ func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
return err
}

appOS, err := appImage.OS()
if err != nil {
return errors.Wrapf(err, "getting app OS")
}

appArch, err := appImage.Architecture()
if err != nil {
return errors.Wrapf(err, "getting app architecture")
}

var md files.LayersMetadataCompat
if ok, err := dist.GetLabel(appImage, platform.LifecycleMetadataLabel, &md); err != nil {
return err
Expand Down Expand Up @@ -90,7 +101,11 @@ func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
return errors.New("run image must be specified")
}

baseImage, err := c.imageFetcher.Fetch(ctx, runImageName, image.FetchOptions{Daemon: !opts.Publish, PullPolicy: opts.PullPolicy})
baseImage, err := c.imageFetcher.Fetch(ctx, runImageName, image.FetchOptions{
Daemon: !opts.Publish,
PullPolicy: opts.PullPolicy,
Platform: fmt.Sprintf("%s/%s", appOS, appArch),
})
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/rebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ func testRebase(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"remote-top-layer-sha","reference":"remote-digest"`)
args := fakeImageFetcher.FetchCalls["some/run"]
h.AssertEq(t, args.Platform, "linux/amd64")
})
})
})
Expand Down

0 comments on commit 5728158

Please sign in to comment.