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

Fix scratch multi-platform images #4526

Merged
merged 3 commits into from
Jan 9, 2024
Merged
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
132 changes: 107 additions & 25 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2266,49 +2266,131 @@ func testBuildExportScratch(t *testing.T, sb integration.Sandbox) {
require.NoError(t, err)
defer c.Close()

st := llb.Scratch()
def, err := st.Marshal(sb.Context())
require.NoError(t, err)

registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) {
t.Skip(err.Error())
}
require.NoError(t, err)

makeFrontend := func(ps []string) func(ctx context.Context, c gateway.Client) (*gateway.Result, error) {
return func(ctx context.Context, c gateway.Client) (*gateway.Result, error) {
st := llb.Scratch()
def, err := st.Marshal(sb.Context())
require.NoError(t, err)

r, err := c.Solve(ctx, gateway.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, err
}

ref, err := r.SingleRef()
if err != nil {
return nil, err
}

res := gateway.NewResult()
if ps == nil {
res.SetRef(ref)
} else {
for _, p := range ps {
res.AddRef(p, ref)
}

expPlatforms := &exptypes.Platforms{
Platforms: make([]exptypes.Platform, len(ps)),
}
for i, pk := range ps {
p := platforms.MustParse(pk)

img := ocispecs.Image{
Platform: p,
Config: ocispecs.ImageConfig{
Labels: map[string]string{
"foo": "i am platform " + platforms.Format(p),
},
},
}
config, err := json.Marshal(img)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal image config")
}
res.AddMeta(fmt.Sprintf("%s/%s", exptypes.ExporterImageConfigKey, pk), config)

expPlatforms.Platforms[i] = exptypes.Platform{
ID: pk,
Platform: p,
}
}
dt, err := json.Marshal(expPlatforms)
if err != nil {
return nil, err
}
res.AddMeta(exptypes.ExporterPlatformsKey, dt)
}

return res, nil
}
}

target := registry + "/buildkit/build/exporter:withnocompressed"

_, err = c.Solve(sb.Context(), def, SolveOpt{
_, err = c.Build(sb.Context(), SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterImage,
Attrs: map[string]string{
"name": target,
"push": "true",
"unpack": "true",
"compression": "uncompressed",
"name": target,
"push": "true",
"unpack": "true",
"compression": "uncompressed",
"attest:provenance": "mode=max",
},
},
},
}, nil)
}, "", makeFrontend(nil), nil)
require.NoError(t, err)

ctx := namespaces.WithNamespace(sb.Context(), "buildkit")
cdAddress := sb.ContainerdAddress()
var client *containerd.Client
if cdAddress != "" {
client, err = newContainerd(cdAddress)
require.NoError(t, err)
defer client.Close()
targetMulti := registry + "/buildkit/build/exporter-multi:withnocompressed"
_, err = c.Build(sb.Context(), SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterImage,
Attrs: map[string]string{
"name": targetMulti,
"push": "true",
"unpack": "true",
"compression": "uncompressed",
"attest:provenance": "mode=max",
},
},
},
}, "", makeFrontend([]string{"linux/amd64", "linux/arm64"}), nil)
require.NoError(t, err)

img, err := client.GetImage(ctx, target)
require.NoError(t, err)
mfst, err := images.Manifest(ctx, client.ContentStore(), img.Target(), nil)
require.NoError(t, err)
require.Equal(t, 0, len(mfst.Layers))
err = client.ImageService().Delete(ctx, target, images.SynchronousDelete())
require.NoError(t, err)
}
desc, provider, err := contentutil.ProviderFromRef(target)
require.NoError(t, err)
imgs, err := testutil.ReadImages(sb.Context(), provider, desc)
require.NoError(t, err)
require.Len(t, imgs.Images, 1)
img := imgs.Find(platforms.DefaultString())
require.Empty(t, img.Layers)
require.Equal(t, platforms.DefaultSpec(), img.Img.Platform)

desc, provider, err = contentutil.ProviderFromRef(targetMulti)
require.NoError(t, err)
imgs, err = testutil.ReadImages(sb.Context(), provider, desc)
require.NoError(t, err)
require.Len(t, imgs.Images, 2)
img = imgs.Find("linux/amd64")
require.Empty(t, img.Layers)
require.Equal(t, "linux/amd64", platforms.Format(img.Img.Platform))
require.Equal(t, "i am platform linux/amd64", img.Img.Config.Labels["foo"])
img = imgs.Find("linux/arm64")
require.Empty(t, img.Layers)
require.Equal(t, "linux/arm64", platforms.Format(img.Img.Platform))
require.Equal(t, "i am platform linux/arm64", img.Img.Config.Labels["foo"])
}

func testBuildHTTPSource(t *testing.T, sb integration.Sandbox) {
Expand Down
6 changes: 6 additions & 0 deletions exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
refs = append(refs, src.Ref)
}
for _, ref := range src.Refs {
if ref == nil {
continue
}
refs = append(refs, ref)
}
eg, ctx := errgroup.WithContext(ctx)
Expand Down Expand Up @@ -358,6 +361,9 @@ func (e *imageExporterInstance) pushImage(ctx context.Context, src *exporter.Sou
refs = append(refs, src.Ref)
}
for _, ref := range src.Refs {
if ref == nil {
continue
}
refs = append(refs, ref)
}

Expand Down
19 changes: 9 additions & 10 deletions frontend/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,14 +682,15 @@ func (lbf *llbBridgeForwarder) Solve(ctx context.Context, req *pb.SolveRequest)
ids := make(map[string]string, len(res.Refs))
defs := make(map[string]*opspb.Definition, len(res.Refs))
for k, ref := range res.Refs {
id := identity.NewID()
if ref == nil {
id = ""
} else {
var id string
var def *opspb.Definition
if ref != nil {
id = identity.NewID()
def = ref.Definition()
lbf.refs[id] = ref
}
ids[k] = id
defs[k] = ref.Definition()
defs[k] = def
}

if req.AllowResultArrayRef {
Expand All @@ -703,12 +704,10 @@ func (lbf *llbBridgeForwarder) Solve(ctx context.Context, req *pb.SolveRequest)
}
} else {
ref := res.Ref
id := identity.NewID()

var id string
var def *opspb.Definition
if ref == nil {
id = ""
} else {
if ref != nil {
id = identity.NewID()
def = ref.Definition()
lbf.refs[id] = ref
}
Expand Down
3 changes: 3 additions & 0 deletions solver/llbsolver/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (b *provenanceBridge) requests(r *frontend.Result) (*resultRequests, error)
}

for k, ref := range r.Refs {
if ref == nil {
continue
}
r, ok := b.findByResult(ref)
if !ok {
return nil, errors.Errorf("could not find request for ref %s", ref.ID())
Expand Down
6 changes: 6 additions & 0 deletions solver/llbsolver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ func (s *Solver) recordBuildHistory(ctx context.Context, id string, req frontend
}

for k, r := range res.Refs {
if r == nil {
continue
}
k, r := k, r
cp := res.Provenance.Refs[k]
eg.Go(func() error {
Expand Down Expand Up @@ -719,6 +722,9 @@ func addProvenanceToResult(res *frontend.Result, br *provenanceBridge) (*Result,
out.Provenance.Refs = make(map[string]*provenance.Capture, len(res.Refs))
}
for k, ref := range res.Refs {
if ref == nil {
continue
}
cp, err := getProvenance(ref, reqs.refs[k].bridge, k, reqs)
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions solver/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func EachRef[U comparable, V comparable](a *Result[U], b *Result[V], fn func(U,
return err
}

// ConvertResult transforms a Result[U] into a Result[V], using a transfomer
// function that converts a U to a V. Zero values of type U are converted to
// zero values of type V directly, without passing through the transformer
// function.
func ConvertResult[U comparable, V comparable](r *Result[U], fn func(U) (V, error)) (*Result[V], error) {
var zero U

Expand All @@ -160,6 +164,8 @@ func ConvertResult[U comparable, V comparable](r *Result[U], fn func(U) (V, erro
}
for k, r := range r.Refs {
if r == zero {
var zero V
r2.Refs[k] = zero
continue
}
r2.Refs[k], err = fn(r)
Expand Down
Loading