Skip to content

Commit

Permalink
cbuild.RunBuild -> build.Build
Browse files Browse the repository at this point in the history
  • Loading branch information
blampe committed Feb 13, 2024
1 parent 5c5047d commit 6c70bf9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
94 changes: 81 additions & 13 deletions provider/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import (
"sync"

"github.com/distribution/reference"
"github.com/docker/buildx/build"
"github.com/docker/buildx/builder"
cbuild "github.com/docker/buildx/controller/build"
controllerapi "github.com/docker/buildx/controller/pb"
"github.com/docker/buildx/store"
"github.com/docker/buildx/store/storeutil"
"github.com/docker/buildx/util/dockerutil"
"github.com/docker/buildx/util/platformutil"
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config"
Expand All @@ -24,6 +28,8 @@ import (
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/util/progress/progressui"
cp "github.com/otiai10/copy"

Expand All @@ -47,6 +53,7 @@ type docker struct {
mu sync.Mutex

cli *command.DockerCli
txn *store.Txn
dir string
}

Expand Down Expand Up @@ -78,8 +85,16 @@ func newDockerClient() (*docker, error) {
// TODO(github.com/pulumi/pulumi-docker/issues/946): Support TLS options
}
err = cli.Initialize(opts)
if err != nil {
return nil, err
}

txn, _, err := storeutil.GetStore(cli)
if err != nil {
return nil, err
}

return &docker{cli: cli, dir: dir}, err
return &docker{cli: cli, txn: txn, dir: dir}, err
}

func (d *docker) Auth(ctx context.Context, creds properties.RegistryAuth) error {
Expand Down Expand Up @@ -134,7 +149,7 @@ func (d *docker) Build(
pctx provider.Context,
opts controllerapi.BuildOptions,
) (*client.SolveResponse, error) {
// Use a seprate context for the build. We don't want to kill our request's
// Use a separate context for the build. We don't want to kill our request's
// context if the build fails.
bctx := context.Background()

Expand All @@ -155,7 +170,7 @@ func (d *docker) Build(
}
}()

b, err := d.builder(opts)
b, nodes, err := d.builder(opts)
if err != nil {
return nil, err
}
Expand All @@ -170,11 +185,64 @@ func (d *docker) Build(
return nil, fmt.Errorf("creating printer: %w", err)
}

cacheFrom := []client.CacheOptionsEntry{}
for _, c := range opts.CacheFrom {
cacheFrom = append(cacheFrom, client.CacheOptionsEntry{
Type: c.Type,
Attrs: c.Attrs,
})
}
cacheTo := []client.CacheOptionsEntry{}
for _, c := range opts.CacheTo {
cacheTo = append(cacheTo, client.CacheOptionsEntry{
Type: c.Type,
Attrs: c.Attrs,
})
}
exports := []client.ExportEntry{}
for _, e := range opts.Exports {
exports = append(exports, client.ExportEntry{
Type: e.Type,
Attrs: e.Attrs,
OutputDir: e.Destination,
})
}
platforms, _ := platformutil.Parse(opts.Platforms)
platforms = platformutil.Dedupe(platforms)

// Perform the build.
solve, res, err := cbuild.RunBuild(bctx, d.cli, opts, d.cli.In(), printer, true)
if res != nil {
res.Done()
targets, err := build.Build(
bctx,
nodes,
map[string]build.Options{
opts.Target: {
Inputs: build.Inputs{
ContextPath: opts.ContextPath,
DockerfilePath: opts.DockerfileName,
},
BuildArgs: opts.BuildArgs,
CacheFrom: cacheFrom,
CacheTo: cacheTo,
Exports: exports,
NoCache: opts.NoCache,
Platforms: platforms,
Pull: opts.Pull,
Tags: opts.Tags,
Target: opts.Target,

Session: []session.Attachable{
authprovider.NewDockerAuthProvider(d.cli.ConfigFile(), nil),
},
},
},
dockerutil.NewClient(d.cli),
d.dir,
printer,
)
if err != nil {
return nil, err
}
solve := targets[opts.Target]

for _, w := range printer.Warnings() {
b := &bytes.Buffer{}
Expand Down Expand Up @@ -240,7 +308,7 @@ func normalizeReference(ref string) (reference.Named, error) {
// ready.
func (d *docker) builder(
opts controllerapi.BuildOptions,
) (*builder.Builder, error) {
) (*builder.Builder, []builder.Node, error) {
d.mu.Lock()
defer d.mu.Unlock()

Expand All @@ -251,16 +319,16 @@ func (d *docker) builder(
b, err := builder.New(d.cli,
builder.WithName(opts.Builder),
builder.WithContextPathHash(contextPathHash),
builder.WithStore(d.txn),
)
if err != nil {
return nil, err
return nil, nil, err
}

// Need to load nodes in order to determine the builder's driver.
_, err = b.LoadNodes(context.Background())
nodes, err := b.LoadNodes(context.Background())
if err != nil {
return nil, err
return nil, nil, err
}

return b, nil
return b, nodes, nil
}
2 changes: 2 additions & 0 deletions provider/internal/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func (ia ImageArgs) toBuilds(
// - No --cache-from (rely on local build cache).
// - --cache-to
for _, p := range opts.Platforms {
opts := opts
// Only build for this platform.
opts.Platforms = []string{p}
// Don't push anything except caches.
Expand All @@ -253,6 +254,7 @@ func (ia ImageArgs) toBuilds(
// everything already loaded into build context, but this doesn't work
// consistently with multi-platform images.
opts.CacheFrom = nil
opts.Tags = nil

builds = append(builds, opts)
}
Expand Down

0 comments on commit 6c70bf9

Please sign in to comment.