builder: add buildinfo for buildkit
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
b899db6423
commit
fda0226a89
3 changed files with 58 additions and 12 deletions
|
@ -309,7 +309,7 @@ func (p *puller) CacheKey(ctx context.Context, g session.Group, index int) (stri
|
|||
if err != nil {
|
||||
return "", "", nil, false, err
|
||||
}
|
||||
return dgst.String(), dgst.String(), nil, false, nil
|
||||
return dgst.String(), p.desc.Digest.String(), nil, false, nil
|
||||
}
|
||||
|
||||
if p.config != nil {
|
||||
|
@ -329,7 +329,7 @@ func (p *puller) CacheKey(ctx context.Context, g session.Group, index int) (stri
|
|||
if err != nil {
|
||||
return "", "", nil, false, err
|
||||
}
|
||||
return dgst.String(), dgst.String(), nil, false, nil
|
||||
return dgst.String(), p.desc.Digest.String(), nil, false, nil
|
||||
}
|
||||
|
||||
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
|
|||
if err != nil {
|
||||
return "", "", nil, false, err
|
||||
}
|
||||
return dgst.String(), dgst.String(), nil, true, nil
|
||||
return dgst.String(), p.desc.Digest.String(), nil, true, nil
|
||||
}
|
||||
|
||||
return k, "", nil, true, nil
|
||||
return k, k, nil, true, nil
|
||||
}
|
||||
|
||||
func (p *puller) getRef(ctx context.Context, diffIDs []layer.DiffID, opts ...cache.RefOption) (cache.ImmutableRef, error) {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
distref "github.com/docker/distribution/reference"
|
||||
|
@ -18,7 +19,9 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
keyImageName = "name"
|
||||
keyImageName = "name"
|
||||
keyBuildInfo = "buildinfo"
|
||||
keyBuildInfoAttrs = "buildinfo-attrs"
|
||||
)
|
||||
|
||||
// Differ can make a moby layer from a snapshot
|
||||
|
@ -44,11 +47,34 @@ func New(opt Opt) (exporter.Exporter, error) {
|
|||
}
|
||||
|
||||
func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exporter.ExporterInstance, error) {
|
||||
i := &imageExporterInstance{imageExporter: e}
|
||||
i := &imageExporterInstance{
|
||||
imageExporter: e,
|
||||
buildInfo: true,
|
||||
}
|
||||
for k, v := range opt {
|
||||
switch k {
|
||||
case keyImageName:
|
||||
i.targetName = v
|
||||
case keyBuildInfo:
|
||||
if v == "" {
|
||||
i.buildInfo = true
|
||||
continue
|
||||
}
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
|
||||
}
|
||||
i.buildInfo = b
|
||||
case keyBuildInfoAttrs:
|
||||
if v == "" {
|
||||
i.buildInfoAttrs = false
|
||||
continue
|
||||
}
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
|
||||
}
|
||||
i.buildInfoAttrs = b
|
||||
default:
|
||||
if i.meta == nil {
|
||||
i.meta = make(map[string][]byte)
|
||||
|
@ -61,8 +87,10 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
|
|||
|
||||
type imageExporterInstance struct {
|
||||
*imageExporter
|
||||
targetName string
|
||||
meta map[string][]byte
|
||||
targetName string
|
||||
meta map[string][]byte
|
||||
buildInfo bool
|
||||
buildInfoAttrs bool
|
||||
}
|
||||
|
||||
func (e *imageExporterInstance) Name() string {
|
||||
|
@ -93,9 +121,13 @@ func (e *imageExporterInstance) Export(ctx context.Context, inp exporter.Source,
|
|||
}
|
||||
|
||||
var config []byte
|
||||
var buildInfo []byte
|
||||
switch len(inp.Refs) {
|
||||
case 0:
|
||||
config = inp.Metadata[exptypes.ExporterImageConfigKey]
|
||||
if v, ok := inp.Metadata[exptypes.ExporterBuildInfo]; ok {
|
||||
buildInfo = v
|
||||
}
|
||||
case 1:
|
||||
platformsBytes, ok := inp.Metadata[exptypes.ExporterPlatformsKey]
|
||||
if !ok {
|
||||
|
@ -109,6 +141,9 @@ func (e *imageExporterInstance) Export(ctx context.Context, inp exporter.Source,
|
|||
return nil, errors.Errorf("number of platforms does not match references %d %d", len(p.Platforms), len(inp.Refs))
|
||||
}
|
||||
config = inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterImageConfigKey, p.Platforms[0].ID)]
|
||||
if v, ok := inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterBuildInfo, p.Platforms[0].ID)]; ok {
|
||||
buildInfo = v
|
||||
}
|
||||
}
|
||||
|
||||
var diffs []digest.Digest
|
||||
|
@ -147,7 +182,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, inp exporter.Source,
|
|||
|
||||
diffs, history = normalizeLayersAndHistory(diffs, history, ref)
|
||||
|
||||
config, err = patchImageConfig(config, diffs, history, inp.Metadata[exptypes.ExporterInlineCache])
|
||||
config, err = patchImageConfig(config, diffs, history, inp.Metadata[exptypes.ExporterInlineCache], buildInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/containerd/containerd/platforms"
|
||||
"github.com/moby/buildkit/cache"
|
||||
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
|
||||
"github.com/moby/buildkit/util/progress"
|
||||
"github.com/moby/buildkit/util/system"
|
||||
"github.com/opencontainers/go-digest"
|
||||
|
@ -43,7 +44,7 @@ func parseHistoryFromConfig(dt []byte) ([]ocispec.History, error) {
|
|||
return config.History, nil
|
||||
}
|
||||
|
||||
func patchImageConfig(dt []byte, dps []digest.Digest, history []ocispec.History, cache []byte) ([]byte, error) {
|
||||
func patchImageConfig(dt []byte, dps []digest.Digest, history []ocispec.History, cache []byte, buildInfo []byte) ([]byte, error) {
|
||||
m := map[string]json.RawMessage{}
|
||||
if err := json.Unmarshal(dt, &m); err != nil {
|
||||
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,
|
|||
}
|
||||
|
||||
if cache != nil {
|
||||
dt, err = json.Marshal(cache)
|
||||
dt, err := json.Marshal(cache)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to marshal cache")
|
||||
return nil, err
|
||||
}
|
||||
m["moby.buildkit.cache.v0"] = dt
|
||||
}
|
||||
|
||||
if buildInfo != nil {
|
||||
dt, err := json.Marshal(buildInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m[binfotypes.ImageConfigField] = dt
|
||||
} else {
|
||||
delete(m, binfotypes.ImageConfigField)
|
||||
}
|
||||
|
||||
dt, err = json.Marshal(m)
|
||||
return dt, errors.Wrap(err, "failed to marshal config after patch")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue