Skip to content

Commit

Permalink
fixing a little bug when creating a builder without target flag on da…
Browse files Browse the repository at this point in the history
…rwin

Signed-off-by: Juan Bustamante <jbustamante@vmware.com>
  • Loading branch information
jjbustamante committed May 21, 2024
1 parent 3f3b24d commit 6fa0cbc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pkg/buildpack/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/buildpacks/imgutil/fakes"
"github.com/buildpacks/lifecycle/api"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/system"
"github.com/golang/mock/gomock"
"github.com/heroku/color"
Expand Down Expand Up @@ -61,6 +62,7 @@ func testBuildpackDownloader(t *testing.T, when spec.G, it spec.S) {
var createPackage = func(imageName string) *fakes.Image {
packageImage := fakes.NewImage(imageName, "", nil)
mockImageFactory.EXPECT().NewImage(packageImage.Name(), false, dist.Target{OS: "linux"}).Return(packageImage, nil)
mockDockerClient.EXPECT().ServerVersion(gomock.Any()).Return(types.Version{Os: "linux", Arch: "amd64"}, nil)

pack, err := client.NewClient(
client.WithLogger(logger),
Expand Down
31 changes: 25 additions & 6 deletions pkg/client/create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,38 @@ func (c *Client) addConfig(ctx context.Context, kind string, config pubbldr.Modu

func (c *Client) processBuilderCreateTargets(ctx context.Context, opts CreateBuilderOptions) ([]dist.Target, error) {
var targets []dist.Target
if len(opts.Targets) > 0 {
// when exporting to the daemon, we need to select just one target
if !opts.Publish {
daemonTarget, err := c.daemonTarget(ctx, opts.Targets)

// For backward compatibility, we must create a target with de default platform used in the Fetch implementation for daemon and remote
// When daemon
// OS: daemonInfo.Os,
// Architecture: daemonInfo.Arch
// When remote || layout
// OS: "linux",
// Architecture: runtime.GOARCH

if !opts.Publish {
// daemon option
info, err := c.docker.ServerVersion(ctx)
if err != nil {
return targets, err
}
if len(opts.Targets) > 0 {
// when exporting to the daemon, we need to select just one target
daemonTarget, err := c.daemonTarget(info, opts.Targets)
if err != nil {
return targets, err
}
targets = append(targets, daemonTarget)
} else {
targets = opts.Targets
targets = append(targets, dist.Target{OS: info.Os, Arch: info.Arch})
}
} else {
targets = append(targets, dist.Target{OS: runtime.GOOS, Arch: runtime.GOARCH})
// remote option
if len(opts.Targets) > 0 {
targets = opts.Targets
} else {
targets = append(targets, dist.Target{OS: "linux", Arch: runtime.GOARCH})
}
}
return targets, nil
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/client/package_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"path/filepath"

"github.com/docker/docker/api/types"
"github.com/pkg/errors"

pubbldpkg "github.com/buildpacks/pack/buildpackage"
Expand Down Expand Up @@ -228,10 +229,14 @@ func (c *Client) downloadBuildpackFromURI(ctx context.Context, uri, relativeBase

func (c *Client) processPackageBuildpackTargets(ctx context.Context, opts PackageBuildpackOptions) ([]dist.Target, error) {
var targets []dist.Target
info, err := c.docker.ServerVersion(ctx)
if err != nil {
return targets, err
}
if len(opts.Targets) > 0 {
// when exporting to the daemon, we need to select just one target
if !opts.Publish && opts.Format == FormatImage {
daemonTarget, err := c.daemonTarget(ctx, opts.Targets)
daemonTarget, err := c.daemonTarget(info, opts.Targets)
if err != nil {
return targets, err
}
Expand Down Expand Up @@ -262,12 +267,8 @@ func (c *Client) validateOSPlatform(ctx context.Context, os string, publish bool
return nil
}

func (c *Client) daemonTarget(ctx context.Context, targets []dist.Target) (dist.Target, error) {
info, err := c.docker.ServerVersion(ctx)
if err != nil {
return dist.Target{}, err
}

// daemonTarget returns a target that matches with the given daemon os/arch
func (c *Client) daemonTarget(info types.Version, targets []dist.Target) (dist.Target, error) {
for _, t := range targets {
if t.Arch != "" && t.OS == info.Os && t.Arch == info.Arch {
return t, nil
Expand Down

0 comments on commit 6fa0cbc

Please sign in to comment.