Skip to content

Commit fda0226

Browse files
committed
builder: add buildinfo for buildkit
Signed-off-by: CrazyMax <[email protected]>
1 parent b899db6 commit fda0226

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

builder/builder-next/adapters/containerimage/pull.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func (p *puller) CacheKey(ctx context.Context, g session.Group, index int) (stri
309309
if err != nil {
310310
return "", "", nil, false, err
311311
}
312-
return dgst.String(), dgst.String(), nil, false, nil
312+
return dgst.String(), p.desc.Digest.String(), nil, false, nil
313313
}
314314

315315
if p.config != nil {
@@ -329,7 +329,7 @@ func (p *puller) CacheKey(ctx context.Context, g session.Group, index int) (stri
329329
if err != nil {
330330
return "", "", nil, false, err
331331
}
332-
return dgst.String(), dgst.String(), nil, false, nil
332+
return dgst.String(), p.desc.Digest.String(), nil, false, nil
333333
}
334334

335335
if len(p.config) == 0 && p.desc.MediaType != images.MediaTypeDockerSchema1Manifest {
@@ -342,10 +342,10 @@ func (p *puller) CacheKey(ctx context.Context, g session.Group, index int) (stri
342342
if err != nil {
343343
return "", "", nil, false, err
344344
}
345-
return dgst.String(), dgst.String(), nil, true, nil
345+
return dgst.String(), p.desc.Digest.String(), nil, true, nil
346346
}
347347

348-
return k, "", nil, true, nil
348+
return k, k, nil, true, nil
349349
}
350350

351351
func (p *puller) getRef(ctx context.Context, diffIDs []layer.DiffID, opts ...cache.RefOption) (cache.ImmutableRef, error) {

builder/builder-next/exporter/export.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"strconv"
78
"strings"
89

910
distref "github.com/docker/distribution/reference"
@@ -18,7 +19,9 @@ import (
1819
)
1920

2021
const (
21-
keyImageName = "name"
22+
keyImageName = "name"
23+
keyBuildInfo = "buildinfo"
24+
keyBuildInfoAttrs = "buildinfo-attrs"
2225
)
2326

2427
// Differ can make a moby layer from a snapshot
@@ -44,11 +47,34 @@ func New(opt Opt) (exporter.Exporter, error) {
4447
}
4548

4649
func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exporter.ExporterInstance, error) {
47-
i := &imageExporterInstance{imageExporter: e}
50+
i := &imageExporterInstance{
51+
imageExporter: e,
52+
buildInfo: true,
53+
}
4854
for k, v := range opt {
4955
switch k {
5056
case keyImageName:
5157
i.targetName = v
58+
case keyBuildInfo:
59+
if v == "" {
60+
i.buildInfo = true
61+
continue
62+
}
63+
b, err := strconv.ParseBool(v)
64+
if err != nil {
65+
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
66+
}
67+
i.buildInfo = b
68+
case keyBuildInfoAttrs:
69+
if v == "" {
70+
i.buildInfoAttrs = false
71+
continue
72+
}
73+
b, err := strconv.ParseBool(v)
74+
if err != nil {
75+
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
76+
}
77+
i.buildInfoAttrs = b
5278
default:
5379
if i.meta == nil {
5480
i.meta = make(map[string][]byte)
@@ -61,8 +87,10 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
6187

6288
type imageExporterInstance struct {
6389
*imageExporter
64-
targetName string
65-
meta map[string][]byte
90+
targetName string
91+
meta map[string][]byte
92+
buildInfo bool
93+
buildInfoAttrs bool
6694
}
6795

6896
func (e *imageExporterInstance) Name() string {
@@ -93,9 +121,13 @@ func (e *imageExporterInstance) Export(ctx context.Context, inp exporter.Source,
93121
}
94122

95123
var config []byte
124+
var buildInfo []byte
96125
switch len(inp.Refs) {
97126
case 0:
98127
config = inp.Metadata[exptypes.ExporterImageConfigKey]
128+
if v, ok := inp.Metadata[exptypes.ExporterBuildInfo]; ok {
129+
buildInfo = v
130+
}
99131
case 1:
100132
platformsBytes, ok := inp.Metadata[exptypes.ExporterPlatformsKey]
101133
if !ok {
@@ -109,6 +141,9 @@ func (e *imageExporterInstance) Export(ctx context.Context, inp exporter.Source,
109141
return nil, errors.Errorf("number of platforms does not match references %d %d", len(p.Platforms), len(inp.Refs))
110142
}
111143
config = inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterImageConfigKey, p.Platforms[0].ID)]
144+
if v, ok := inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterBuildInfo, p.Platforms[0].ID)]; ok {
145+
buildInfo = v
146+
}
112147
}
113148

114149
var diffs []digest.Digest
@@ -147,7 +182,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, inp exporter.Source,
147182

148183
diffs, history = normalizeLayersAndHistory(diffs, history, ref)
149184

150-
config, err = patchImageConfig(config, diffs, history, inp.Metadata[exptypes.ExporterInlineCache])
185+
config, err = patchImageConfig(config, diffs, history, inp.Metadata[exptypes.ExporterInlineCache], buildInfo)
151186
if err != nil {
152187
return nil, err
153188
}

builder/builder-next/exporter/writer.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/containerd/containerd/platforms"
99
"github.com/moby/buildkit/cache"
10+
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
1011
"github.com/moby/buildkit/util/progress"
1112
"github.com/moby/buildkit/util/system"
1213
"github.com/opencontainers/go-digest"
@@ -43,7 +44,7 @@ func parseHistoryFromConfig(dt []byte) ([]ocispec.History, error) {
4344
return config.History, nil
4445
}
4546

46-
func patchImageConfig(dt []byte, dps []digest.Digest, history []ocispec.History, cache []byte) ([]byte, error) {
47+
func patchImageConfig(dt []byte, dps []digest.Digest, history []ocispec.History, cache []byte, buildInfo []byte) ([]byte, error) {
4748
m := map[string]json.RawMessage{}
4849
if err := json.Unmarshal(dt, &m); err != nil {
4950
return nil, errors.Wrap(err, "failed to parse image config for patch")
@@ -80,13 +81,23 @@ func patchImageConfig(dt []byte, dps []digest.Digest, history []ocispec.History,
8081
}
8182

8283
if cache != nil {
83-
dt, err = json.Marshal(cache)
84+
dt, err := json.Marshal(cache)
8485
if err != nil {
85-
return nil, errors.Wrap(err, "failed to marshal cache")
86+
return nil, err
8687
}
8788
m["moby.buildkit.cache.v0"] = dt
8889
}
8990

91+
if buildInfo != nil {
92+
dt, err := json.Marshal(buildInfo)
93+
if err != nil {
94+
return nil, err
95+
}
96+
m[binfotypes.ImageConfigField] = dt
97+
} else {
98+
delete(m, binfotypes.ImageConfigField)
99+
}
100+
90101
dt, err = json.Marshal(m)
91102
return dt, errors.Wrap(err, "failed to marshal config after patch")
92103
}

0 commit comments

Comments
 (0)