Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for outputing multiple windows images from one build #480

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions frontend/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,39 @@ import (
"github.com/pkg/errors"
)

func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.Platform) (*dalec.Spec, error) {
type LoadConfig struct {
SubstituteOpts []dalec.SubstituteOpt
}

type LoadOpt func(*LoadConfig)

func WithAllowArgs(args ...string) LoadOpt {
return func(cfg *LoadConfig) {
set := make(map[string]struct{}, len(args))
for _, arg := range args {
set[arg] = struct{}{}
}
cfg.SubstituteOpts = append(cfg.SubstituteOpts, func(cfg *dalec.SubstituteConfig) {
orig := cfg.AllowArg

cfg.AllowArg = func(key string) bool {
if orig != nil && orig(key) {
return true
}
_, ok := set[key]
return ok
}
})
}
}

func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.Platform, opts ...LoadOpt) (*dalec.Spec, error) {
cfg := LoadConfig{}

for _, o := range opts {
o(&cfg)
}

src, err := client.ReadEntrypoint(ctx, "Dockerfile")
if err != nil {
return nil, fmt.Errorf("could not read spec file: %w", err)
Expand All @@ -35,7 +67,7 @@ func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.P
fillPlatformArgs("TARGET", args, *platform)
fillPlatformArgs("BUILD", args, client.BuildPlatforms[0])

if err := spec.SubstituteArgs(args); err != nil {
if err := spec.SubstituteArgs(args, cfg.SubstituteOpts...); err != nil {
return nil, errors.Wrap(err, "error resolving build args")
}
return spec, nil
Expand Down Expand Up @@ -72,15 +104,7 @@ func fillPlatformArgs(prefix string, args map[string]string, platform ocispecs.P

type PlatformBuildFunc func(ctx context.Context, client gwclient.Client, platform *ocispecs.Platform, spec *dalec.Spec, targetKey string) (gwclient.Reference, *dalec.DockerImageSpec, error)

// BuildWithPlatform is a helper function to build a spec with a given platform
// It takes care of looping through each tarrget platform and executing the build with the platform args substituted in the spec.
// This also deals with the docker-style multi-platform output.
func BuildWithPlatform(ctx context.Context, client gwclient.Client, f PlatformBuildFunc) (*gwclient.Result, error) {
dc, err := dockerui.NewClient(client)
if err != nil {
return nil, err
}

func BuildWithPlatformFromUIClient(ctx context.Context, client gwclient.Client, dc *dockerui.Client, f PlatformBuildFunc) (*gwclient.Result, error) {
rb, err := dc.Build(ctx, func(ctx context.Context, platform *ocispecs.Platform, idx int) (gwclient.Reference, *dalec.DockerImageSpec, *dalec.DockerImageSpec, error) {
spec, err := LoadSpec(ctx, dc, platform)
if err != nil {
Expand All @@ -101,6 +125,17 @@ func BuildWithPlatform(ctx context.Context, client gwclient.Client, f PlatformBu
return rb.Finalize()
}

// BuildWithPlatform is a helper function to build a spec with a given platform
// It takes care of looping through each target platform and executing the build with the platform args substituted in the spec.
// This also deals with the docker-style multi-platform output.
func BuildWithPlatform(ctx context.Context, client gwclient.Client, f PlatformBuildFunc) (*gwclient.Result, error) {
dc, err := dockerui.NewClient(client)
if err != nil {
return nil, err
}
return BuildWithPlatformFromUIClient(ctx, client, dc, f)
}

// GetBaseImage returns an image that first checks if the client provided the
// image in the build context matching the image ref.
//
Expand Down
17 changes: 10 additions & 7 deletions frontend/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,7 @@ func GetBuildArg(client gwclient.Client, k string) (string, bool) {
return "", false
}

func SourceOptFromClient(ctx context.Context, c gwclient.Client) (dalec.SourceOpts, error) {
dc, err := dockerui.NewClient(c)
if err != nil {
return dalec.SourceOpts{}, err
}

func SourceOptFromUIClient(ctx context.Context, c gwclient.Client, dc *dockerui.Client) dalec.SourceOpts {
return dalec.SourceOpts{
Resolver: c,
Forward: ForwarderFromClient(ctx, c),
Expand All @@ -125,7 +120,15 @@ func SourceOptFromClient(ctx context.Context, c gwclient.Client) (dalec.SourceOp
}
return st, nil
},
}, nil
}
}

func SourceOptFromClient(ctx context.Context, c gwclient.Client) (dalec.SourceOpts, error) {
dc, err := dockerui.NewClient(c)
if err != nil {
return dalec.SourceOpts{}, err
}
return SourceOptFromUIClient(ctx, c, dc), nil
}

var (
Expand Down
9 changes: 8 additions & 1 deletion frontend/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,14 @@ func (m *BuildMux) loadSpec(ctx context.Context, client gwclient.Client) (*dalec
}

// Note: this is not suitable for passing to builds since it does not have platform information
spec, err := LoadSpec(ctx, dc, nil)
spec, err := LoadSpec(ctx, dc, nil, func(cfg *LoadConfig) {
cfg.SubstituteOpts = append(cfg.SubstituteOpts, func(cfg *dalec.SubstituteConfig) {
// Allow any args here since we aren't trying to validate the spec at this point.
cfg.AllowArg = func(string) bool {
return true
}
})
})
if err != nil {
return nil, err
}
Expand Down
123 changes: 123 additions & 0 deletions frontend/windows/dockerui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package windows

import (
"context"
"encoding/json"
"fmt"

"github.com/containerd/platforms"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/frontend/dockerui"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)

// This is a copy of dockerui.Client.Build
// It has one modification: Instead of `platforms.Format` it uses `platforms.FormatAll`
// The value returned from this function is used as a map key to store build
// result references.
// When `platforms.Format` is used, the `OSVersion` field is not taken into account
// which means we end up overwriting map keys when there are multiple windows
// platform images being output but with different OSVerions.
// platforms.FormatAll takes OSVersion into account.
func dcBuild(ctx context.Context, bc *dockerui.Client, fn dockerui.BuildFunc) (*resultBuilder, error) {
res := gwclient.NewResult()

targets := make([]*ocispecs.Platform, 0, len(bc.TargetPlatforms))
for _, p := range bc.TargetPlatforms {
p := p
targets = append(targets, &p)
}
if len(targets) == 0 {
targets = append(targets, nil)
}
expPlatforms := &exptypes.Platforms{
Platforms: make([]exptypes.Platform, len(targets)),
}

eg, ctx := errgroup.WithContext(ctx)

for i, tp := range targets {
i, tp := i, tp
eg.Go(func() error {
ref, img, baseImg, err := fn(ctx, tp, i)
if err != nil {
return err
}

config, err := json.Marshal(img)
if err != nil {
return errors.Wrapf(err, "failed to marshal image config")
}

var baseConfig []byte
if baseImg != nil {
baseConfig, err = json.Marshal(baseImg)
if err != nil {
return errors.Wrapf(err, "failed to marshal source image config")
}
}

p := platforms.DefaultSpec()
if tp != nil {
p = *tp
}

// in certain conditions we allow input platform to be extended from base image
if p.OS == "windows" && img.OS == p.OS {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The raw string for the OS is added here and also already exists in this file for the windowsAmd64 initialization. Should we make this a constant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all copied code, I'd rather not modify what doesn't need to be.

if p.OSVersion == "" && img.OSVersion != "" {
p.OSVersion = img.OSVersion
}
if p.OSFeatures == nil && len(img.OSFeatures) > 0 {
p.OSFeatures = append([]string{}, img.OSFeatures...)
}
}

p = platforms.Normalize(p)
k := platforms.FormatAll(p)

if bc.MultiPlatformRequested {
res.AddRef(k, ref)
res.AddMeta(fmt.Sprintf("%s/%s", exptypes.ExporterImageConfigKey, k), config)
if len(baseConfig) > 0 {
res.AddMeta(fmt.Sprintf("%s/%s", exptypes.ExporterImageBaseConfigKey, k), baseConfig)
}
} else {
res.SetRef(ref)
res.AddMeta(exptypes.ExporterImageConfigKey, config)
if len(baseConfig) > 0 {
res.AddMeta(exptypes.ExporterImageBaseConfigKey, baseConfig)
}
}
expPlatforms.Platforms[i] = exptypes.Platform{
ID: k,
Platform: p,
}
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
return &resultBuilder{
Result: res,
expPlatforms: expPlatforms,
}, nil
}

type resultBuilder struct {
*gwclient.Result
expPlatforms *exptypes.Platforms
}

func (rb *resultBuilder) Finalize() (*gwclient.Result, error) {
dt, err := json.Marshal(rb.expPlatforms)
if err != nil {
return nil, err
}
rb.AddMeta(exptypes.ExporterPlatformsKey, dt)

return rb.Result, nil
}
Loading
Loading