-
Notifications
You must be signed in to change notification settings - Fork 25
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
cpuguy83
wants to merge
3
commits into
Azure:main
Choose a base branch
from
cpuguy83:windows_multi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.