2017-12-13 00:58:48 +00:00
|
|
|
package containerimage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-12-13 00:58:48 +00:00
|
|
|
"encoding/json"
|
2017-12-28 19:56:04 +00:00
|
|
|
"fmt"
|
2020-10-12 05:41:08 +00:00
|
|
|
"strings"
|
2017-12-13 00:58:48 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/diff"
|
2018-06-26 22:24:33 +00:00
|
|
|
"github.com/containerd/containerd/images"
|
2019-04-23 01:01:30 +00:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2017-12-13 00:58:48 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2018-07-13 18:28:36 +00:00
|
|
|
"github.com/moby/buildkit/exporter"
|
|
|
|
"github.com/moby/buildkit/exporter/containerimage/exptypes"
|
2020-10-27 06:13:39 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2017-12-13 00:58:48 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2021-09-17 23:39:19 +00:00
|
|
|
"github.com/moby/buildkit/util/bklog"
|
|
|
|
"github.com/moby/buildkit/util/buildinfo"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/util/compression"
|
2022-01-14 22:15:50 +00:00
|
|
|
"github.com/moby/buildkit/util/convert"
|
2017-12-13 00:58:48 +00:00
|
|
|
"github.com/moby/buildkit/util/progress"
|
|
|
|
"github.com/moby/buildkit/util/system"
|
2021-09-17 23:39:19 +00:00
|
|
|
"github.com/moby/buildkit/util/tracing"
|
2017-12-13 00:58:48 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2018-06-26 22:24:33 +00:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go"
|
2021-07-26 08:53:30 +00:00
|
|
|
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-12-13 00:58:48 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-09-17 23:39:19 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2018-07-13 18:28:36 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-12-13 00:58:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WriterOpt struct {
|
|
|
|
Snapshotter snapshot.Snapshotter
|
|
|
|
ContentStore content.Store
|
2019-03-28 09:26:34 +00:00
|
|
|
Applier diff.Applier
|
2018-02-09 19:39:48 +00:00
|
|
|
Differ diff.Comparer
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewImageWriter(opt WriterOpt) (*ImageWriter, error) {
|
|
|
|
return &ImageWriter{opt: opt}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImageWriter struct {
|
|
|
|
opt WriterOpt
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:15:50 +00:00
|
|
|
func (ic *ImageWriter) Commit(ctx context.Context, inp exporter.Source, oci bool, comp compression.Config, buildInfoMode buildinfo.ExportMode, propagateNonDist bool, sessionID string) (*ocispecs.Descriptor, error) {
|
2018-07-13 18:28:36 +00:00
|
|
|
platformsBytes, ok := inp.Metadata[exptypes.ExporterPlatformsKey]
|
|
|
|
|
|
|
|
if len(inp.Refs) > 0 && !ok {
|
|
|
|
return nil, errors.Errorf("unable to export multiple refs, missing platforms mapping")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(inp.Refs) == 0 {
|
2022-01-14 22:15:50 +00:00
|
|
|
// TODO: pass through non-dist layers?
|
2022-02-01 05:42:15 +00:00
|
|
|
remotes, err := ic.exportLayers(ctx, comp, session.NewGroup(sessionID), inp.Ref)
|
2018-07-13 18:28:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-17 23:39:19 +00:00
|
|
|
|
|
|
|
var buildInfo []byte
|
|
|
|
if buildInfoMode&buildinfo.ExportImageConfig > 0 {
|
|
|
|
buildInfo = inp.Metadata[exptypes.ExporterBuildInfo]
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:15:50 +00:00
|
|
|
mfstDesc, configDesc, err := ic.commitDistributionManifest(ctx, inp.Ref, inp.Metadata[exptypes.ExporterImageConfigKey], &remotes[0], oci, inp.Metadata[exptypes.ExporterInlineCache], buildInfo, propagateNonDist)
|
2020-12-14 21:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if mfstDesc.Annotations == nil {
|
|
|
|
mfstDesc.Annotations = make(map[string]string)
|
|
|
|
}
|
2021-07-15 18:42:30 +00:00
|
|
|
mfstDesc.Annotations[exptypes.ExporterConfigDigestKey] = configDesc.Digest.String()
|
2021-09-17 23:39:19 +00:00
|
|
|
|
2020-12-14 21:25:34 +00:00
|
|
|
return mfstDesc, nil
|
2018-07-13 18:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var p exptypes.Platforms
|
|
|
|
if err := json.Unmarshal(platformsBytes, &p); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to parse platforms passed to exporter")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.Platforms) != len(inp.Refs) {
|
|
|
|
return nil, errors.Errorf("number of platforms does not match references %d %d", len(p.Platforms), len(inp.Refs))
|
|
|
|
}
|
|
|
|
|
|
|
|
refs := make([]cache.ImmutableRef, 0, len(inp.Refs))
|
2020-05-28 20:46:33 +00:00
|
|
|
remotesMap := make(map[string]int, len(inp.Refs))
|
2018-07-13 18:28:36 +00:00
|
|
|
for id, r := range inp.Refs {
|
2020-05-28 20:46:33 +00:00
|
|
|
remotesMap[id] = len(refs)
|
2018-07-13 18:28:36 +00:00
|
|
|
refs = append(refs, r)
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:15:50 +00:00
|
|
|
// TODO: Pass through non-distributable layers
|
2022-02-01 05:42:15 +00:00
|
|
|
remotes, err := ic.exportLayers(ctx, comp, session.NewGroup(sessionID), refs...)
|
2018-07-13 18:28:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := struct {
|
|
|
|
// MediaType is reserved in the OCI spec but
|
|
|
|
// excluded from go types.
|
|
|
|
MediaType string `json:"mediaType,omitempty"`
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
ocispecs.Index
|
2018-07-13 18:28:36 +00:00
|
|
|
}{
|
2021-07-26 08:53:30 +00:00
|
|
|
MediaType: ocispecs.MediaTypeImageIndex,
|
|
|
|
Index: ocispecs.Index{
|
2018-07-13 18:28:36 +00:00
|
|
|
Versioned: specs.Versioned{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !oci {
|
|
|
|
idx.MediaType = images.MediaTypeDockerSchema2ManifestList
|
|
|
|
}
|
|
|
|
|
2019-11-06 21:13:45 +00:00
|
|
|
labels := map[string]string{}
|
|
|
|
|
|
|
|
for i, p := range p.Platforms {
|
2018-07-13 18:28:36 +00:00
|
|
|
r, ok := inp.Refs[p.ID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("failed to find ref for ID %s", p.ID)
|
|
|
|
}
|
|
|
|
config := inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterImageConfigKey, p.ID)]
|
2021-09-17 23:39:19 +00:00
|
|
|
inlineCache := inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterInlineCache, p.ID)]
|
|
|
|
|
|
|
|
var buildInfo []byte
|
|
|
|
if buildInfoMode&buildinfo.ExportImageConfig > 0 {
|
|
|
|
buildInfo = inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterBuildInfo, p.ID)]
|
|
|
|
}
|
2018-07-13 18:28:36 +00:00
|
|
|
|
2022-01-14 22:15:50 +00:00
|
|
|
desc, _, err := ic.commitDistributionManifest(ctx, r, config, &remotes[remotesMap[p.ID]], oci, inlineCache, buildInfo, propagateNonDist)
|
2018-07-13 18:28:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dp := p.Platform
|
|
|
|
desc.Platform = &dp
|
|
|
|
idx.Manifests = append(idx.Manifests, *desc)
|
2019-11-06 21:13:45 +00:00
|
|
|
|
|
|
|
labels[fmt.Sprintf("containerd.io/gc.ref.content.%d", i)] = desc.Digest.String()
|
2018-07-13 18:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
idxBytes, err := json.MarshalIndent(idx, "", " ")
|
2017-12-13 00:58:48 +00:00
|
|
|
if err != nil {
|
2018-07-13 18:28:36 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to marshal index")
|
|
|
|
}
|
|
|
|
|
|
|
|
idxDigest := digest.FromBytes(idxBytes)
|
2021-07-26 08:53:30 +00:00
|
|
|
idxDesc := ocispecs.Descriptor{
|
2018-07-13 18:28:36 +00:00
|
|
|
Digest: idxDigest,
|
|
|
|
Size: int64(len(idxBytes)),
|
|
|
|
MediaType: idx.MediaType,
|
|
|
|
}
|
|
|
|
idxDone := oneOffProgress(ctx, "exporting manifest list "+idxDigest.String())
|
|
|
|
|
2019-11-06 21:13:45 +00:00
|
|
|
if err := content.WriteBlob(ctx, ic.opt.ContentStore, idxDigest.String(), bytes.NewReader(idxBytes), idxDesc, content.WithLabels(labels)); err != nil {
|
2018-07-13 18:28:36 +00:00
|
|
|
return nil, idxDone(errors.Wrapf(err, "error writing manifest list blob %s", idxDigest))
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
2018-07-13 18:28:36 +00:00
|
|
|
idxDone(nil)
|
|
|
|
|
|
|
|
return &idxDesc, nil
|
|
|
|
}
|
|
|
|
|
2022-02-01 05:42:15 +00:00
|
|
|
func (ic *ImageWriter) exportLayers(ctx context.Context, comp compression.Config, s session.Group, refs ...cache.ImmutableRef) ([]solver.Remote, error) {
|
|
|
|
attr := []attribute.KeyValue{
|
|
|
|
attribute.String("exportLayers.compressionType", comp.Type.String()),
|
|
|
|
attribute.Bool("exportLayers.forceCompression", comp.Force),
|
|
|
|
}
|
|
|
|
if comp.Level != nil {
|
|
|
|
attr = append(attr, attribute.Int("exportLayers.compressionLevel", *comp.Level))
|
|
|
|
}
|
|
|
|
span, ctx := tracing.StartSpan(ctx, "export layers", trace.WithAttributes(attr...))
|
2021-07-14 16:39:07 +00:00
|
|
|
|
2018-07-13 18:28:36 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
layersDone := oneOffProgress(ctx, "exporting layers")
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
out := make([]solver.Remote, len(refs))
|
2018-07-13 18:28:36 +00:00
|
|
|
|
|
|
|
for i, ref := range refs {
|
|
|
|
func(i int, ref cache.ImmutableRef) {
|
2020-05-28 20:46:33 +00:00
|
|
|
if ref == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-13 18:28:36 +00:00
|
|
|
eg.Go(func() error {
|
2022-02-01 05:42:15 +00:00
|
|
|
remotes, err := ref.GetRemotes(ctx, true, comp, false, s)
|
2018-07-13 18:28:36 +00:00
|
|
|
if err != nil {
|
2020-05-28 20:46:33 +00:00
|
|
|
return err
|
2018-07-13 18:28:36 +00:00
|
|
|
}
|
2021-10-05 06:43:22 +00:00
|
|
|
remote := remotes[0]
|
2020-05-28 20:46:33 +00:00
|
|
|
out[i] = *remote
|
2018-07-13 18:28:36 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}(i, ref)
|
|
|
|
}
|
|
|
|
|
2021-07-14 16:39:07 +00:00
|
|
|
err := layersDone(eg.Wait())
|
|
|
|
tracing.FinishWithError(span, err)
|
|
|
|
return out, err
|
2018-07-13 18:28:36 +00:00
|
|
|
}
|
2017-12-13 00:58:48 +00:00
|
|
|
|
2022-01-14 22:15:50 +00:00
|
|
|
func (ic *ImageWriter) commitDistributionManifest(ctx context.Context, ref cache.ImmutableRef, config []byte, remote *solver.Remote, oci bool, inlineCache []byte, buildInfo []byte, propagateNonDist bool) (*ocispecs.Descriptor, *ocispecs.Descriptor, error) {
|
2017-12-13 00:58:48 +00:00
|
|
|
if len(config) == 0 {
|
2018-07-13 18:28:36 +00:00
|
|
|
var err error
|
2017-12-13 00:58:48 +00:00
|
|
|
config, err = emptyImageConfig()
|
|
|
|
if err != nil {
|
2020-12-14 21:25:34 +00:00
|
|
|
return nil, nil, err
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
if remote == nil {
|
|
|
|
remote = &solver.Remote{
|
|
|
|
Provider: ic.opt.ContentStore,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 00:58:48 +00:00
|
|
|
history, err := parseHistoryFromConfig(config)
|
|
|
|
if err != nil {
|
2020-12-14 21:25:34 +00:00
|
|
|
return nil, nil, err
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 07:48:03 +00:00
|
|
|
remote, history = normalizeLayersAndHistory(ctx, remote, history, ref, oci)
|
2017-12-13 00:58:48 +00:00
|
|
|
|
2021-09-17 23:39:19 +00:00
|
|
|
config, err = patchImageConfig(config, remote.Descriptors, history, inlineCache, buildInfo)
|
2017-12-13 00:58:48 +00:00
|
|
|
if err != nil {
|
2020-12-14 21:25:34 +00:00
|
|
|
return nil, nil, err
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:24:33 +00:00
|
|
|
var (
|
|
|
|
configDigest = digest.FromBytes(config)
|
2021-07-26 08:53:30 +00:00
|
|
|
manifestType = ocispecs.MediaTypeImageManifest
|
|
|
|
configType = ocispecs.MediaTypeImageConfig
|
2018-06-26 22:24:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Use docker media types for older Docker versions and registries
|
|
|
|
if !oci {
|
|
|
|
manifestType = images.MediaTypeDockerSchema2Manifest
|
|
|
|
configType = images.MediaTypeDockerSchema2Config
|
|
|
|
}
|
2017-12-13 00:58:48 +00:00
|
|
|
|
2018-06-26 22:24:33 +00:00
|
|
|
mfst := struct {
|
|
|
|
// MediaType is reserved in the OCI spec but
|
|
|
|
// excluded from go types.
|
|
|
|
MediaType string `json:"mediaType,omitempty"`
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
ocispecs.Manifest
|
2018-06-26 22:24:33 +00:00
|
|
|
}{
|
|
|
|
MediaType: manifestType,
|
2021-07-26 08:53:30 +00:00
|
|
|
Manifest: ocispecs.Manifest{
|
2018-06-26 22:24:33 +00:00
|
|
|
Versioned: specs.Versioned{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
},
|
2021-07-26 08:53:30 +00:00
|
|
|
Config: ocispecs.Descriptor{
|
2018-06-26 22:24:33 +00:00
|
|
|
Digest: configDigest,
|
|
|
|
Size: int64(len(config)),
|
|
|
|
MediaType: configType,
|
|
|
|
},
|
2017-12-13 00:58:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-06 21:13:45 +00:00
|
|
|
labels := map[string]string{
|
|
|
|
"containerd.io/gc.ref.content.0": configDigest.String(),
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
for i, desc := range remote.Descriptors {
|
2020-10-12 05:41:08 +00:00
|
|
|
// oci supports annotations but don't export internal annotations
|
|
|
|
if oci {
|
|
|
|
delete(desc.Annotations, "containerd.io/uncompressed")
|
|
|
|
delete(desc.Annotations, "buildkit/createdat")
|
|
|
|
for k := range desc.Annotations {
|
|
|
|
if strings.HasPrefix(k, "containerd.io/distribution.source.") {
|
|
|
|
delete(desc.Annotations, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
desc.Annotations = nil
|
|
|
|
}
|
2022-01-14 22:15:50 +00:00
|
|
|
if !propagateNonDist {
|
|
|
|
desc.MediaType = convert.LayerToDistributable(oci, desc.MediaType)
|
|
|
|
desc.URLs = nil
|
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
mfst.Layers = append(mfst.Layers, desc)
|
|
|
|
labels[fmt.Sprintf("containerd.io/gc.ref.content.%d", i+1)] = desc.Digest.String()
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:24:33 +00:00
|
|
|
mfstJSON, err := json.MarshalIndent(mfst, "", " ")
|
2017-12-13 00:58:48 +00:00
|
|
|
if err != nil {
|
2020-12-14 21:25:34 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "failed to marshal manifest")
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mfstDigest := digest.FromBytes(mfstJSON)
|
2021-07-26 08:53:30 +00:00
|
|
|
mfstDesc := ocispecs.Descriptor{
|
2018-06-06 08:02:01 +00:00
|
|
|
Digest: mfstDigest,
|
|
|
|
Size: int64(len(mfstJSON)),
|
|
|
|
}
|
2017-12-13 00:58:48 +00:00
|
|
|
mfstDone := oneOffProgress(ctx, "exporting manifest "+mfstDigest.String())
|
|
|
|
|
2019-11-06 21:13:45 +00:00
|
|
|
if err := content.WriteBlob(ctx, ic.opt.ContentStore, mfstDigest.String(), bytes.NewReader(mfstJSON), mfstDesc, content.WithLabels((labels))); err != nil {
|
2020-12-14 21:25:34 +00:00
|
|
|
return nil, nil, mfstDone(errors.Wrapf(err, "error writing manifest blob %s", mfstDigest))
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
mfstDone(nil)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
configDesc := ocispecs.Descriptor{
|
2018-06-26 22:24:33 +00:00
|
|
|
Digest: configDigest,
|
|
|
|
Size: int64(len(config)),
|
|
|
|
MediaType: configType,
|
2018-06-06 08:02:01 +00:00
|
|
|
}
|
2017-12-28 19:56:04 +00:00
|
|
|
configDone := oneOffProgress(ctx, "exporting config "+configDigest.String())
|
|
|
|
|
2018-06-06 08:02:01 +00:00
|
|
|
if err := content.WriteBlob(ctx, ic.opt.ContentStore, configDigest.String(), bytes.NewReader(config), configDesc); err != nil {
|
2020-12-14 21:25:34 +00:00
|
|
|
return nil, nil, configDone(errors.Wrap(err, "error writing config blob"))
|
2017-12-28 19:56:04 +00:00
|
|
|
}
|
|
|
|
configDone(nil)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
return &ocispecs.Descriptor{
|
2017-12-13 00:58:48 +00:00
|
|
|
Digest: mfstDigest,
|
|
|
|
Size: int64(len(mfstJSON)),
|
2018-06-26 22:24:33 +00:00
|
|
|
MediaType: manifestType,
|
2020-12-14 21:25:34 +00:00
|
|
|
}, &configDesc, nil
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ic *ImageWriter) ContentStore() content.Store {
|
|
|
|
return ic.opt.ContentStore
|
|
|
|
}
|
|
|
|
|
2019-03-28 09:26:34 +00:00
|
|
|
func (ic *ImageWriter) Snapshotter() snapshot.Snapshotter {
|
|
|
|
return ic.opt.Snapshotter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ic *ImageWriter) Applier() diff.Applier {
|
|
|
|
return ic.opt.Applier
|
|
|
|
}
|
|
|
|
|
2017-12-13 00:58:48 +00:00
|
|
|
func emptyImageConfig() ([]byte, error) {
|
2019-04-23 01:01:30 +00:00
|
|
|
pl := platforms.Normalize(platforms.DefaultSpec())
|
|
|
|
|
|
|
|
type image struct {
|
2021-07-26 08:53:30 +00:00
|
|
|
ocispecs.Image
|
2019-04-23 01:01:30 +00:00
|
|
|
|
|
|
|
// Variant defines platform variant. To be added to OCI.
|
|
|
|
Variant string `json:"variant,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
img := image{
|
2021-07-26 08:53:30 +00:00
|
|
|
Image: ocispecs.Image{
|
2019-04-23 01:01:30 +00:00
|
|
|
Architecture: pl.Architecture,
|
|
|
|
OS: pl.OS,
|
|
|
|
},
|
|
|
|
Variant: pl.Variant,
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
img.RootFS.Type = "layers"
|
|
|
|
img.Config.WorkingDir = "/"
|
2020-10-23 18:17:41 +00:00
|
|
|
img.Config.Env = []string{"PATH=" + system.DefaultPathEnv(pl.OS)}
|
2017-12-13 00:58:48 +00:00
|
|
|
dt, err := json.Marshal(img)
|
|
|
|
return dt, errors.Wrap(err, "failed to create empty image config")
|
|
|
|
}
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
func parseHistoryFromConfig(dt []byte) ([]ocispecs.History, error) {
|
2017-12-13 00:58:48 +00:00
|
|
|
var config struct {
|
2021-07-26 08:53:30 +00:00
|
|
|
History []ocispecs.History
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal(dt, &config); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to unmarshal history from config")
|
|
|
|
}
|
|
|
|
return config.History, nil
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:39:19 +00:00
|
|
|
func patchImageConfig(dt []byte, descs []ocispecs.Descriptor, history []ocispecs.History, cache []byte, buildInfo []byte) ([]byte, error) {
|
2017-12-13 00:58:48 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
var rootFS ocispecs.RootFS
|
2017-12-13 00:58:48 +00:00
|
|
|
rootFS.Type = "layers"
|
2020-05-28 20:46:33 +00:00
|
|
|
for _, desc := range descs {
|
|
|
|
rootFS.DiffIDs = append(rootFS.DiffIDs, digest.Digest(desc.Annotations["containerd.io/uncompressed"]))
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
dt, err := json.Marshal(rootFS)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal rootfs")
|
|
|
|
}
|
|
|
|
m["rootfs"] = dt
|
|
|
|
|
|
|
|
dt, err = json.Marshal(history)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal history")
|
|
|
|
}
|
|
|
|
m["history"] = dt
|
|
|
|
|
2018-05-04 20:29:04 +00:00
|
|
|
if _, ok := m["created"]; !ok {
|
|
|
|
var tm *time.Time
|
|
|
|
for _, h := range history {
|
|
|
|
if h.Created != nil {
|
|
|
|
tm = h.Created
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dt, err = json.Marshal(&tm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal creation time")
|
|
|
|
}
|
|
|
|
m["created"] = dt
|
2018-01-05 18:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 23:32:28 +00:00
|
|
|
if cache != nil {
|
2019-01-17 02:06:03 +00:00
|
|
|
dt, err := json.Marshal(cache)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m["moby.buildkit.cache.v0"] = dt
|
2019-01-04 23:32:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 23:39:19 +00:00
|
|
|
if buildInfo != nil {
|
|
|
|
dt, err := json.Marshal(buildInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m[buildinfo.ImageConfigField] = dt
|
|
|
|
} else if _, ok := m[buildinfo.ImageConfigField]; ok {
|
|
|
|
delete(m, buildinfo.ImageConfigField)
|
|
|
|
}
|
|
|
|
|
2017-12-13 00:58:48 +00:00
|
|
|
dt, err = json.Marshal(m)
|
|
|
|
return dt, errors.Wrap(err, "failed to marshal config after patch")
|
|
|
|
}
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
func normalizeLayersAndHistory(ctx context.Context, remote *solver.Remote, history []ocispecs.History, ref cache.ImmutableRef, oci bool) (*solver.Remote, []ocispecs.History) {
|
2020-05-28 20:46:33 +00:00
|
|
|
refMeta := getRefMetadata(ref, len(remote.Descriptors))
|
2018-05-04 20:29:04 +00:00
|
|
|
|
2017-12-13 00:58:48 +00:00
|
|
|
var historyLayers int
|
|
|
|
for _, h := range history {
|
|
|
|
if !h.EmptyLayer {
|
2020-07-18 16:11:39 +00:00
|
|
|
historyLayers++
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-04 20:29:04 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
if historyLayers > len(remote.Descriptors) {
|
2017-12-13 00:58:48 +00:00
|
|
|
// this case shouldn't happen but if it does force set history layers empty
|
|
|
|
// from the bottom
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(ctx).Warn("invalid image config with unaccounted layers")
|
2021-07-26 08:53:30 +00:00
|
|
|
historyCopy := make([]ocispecs.History, 0, len(history))
|
2017-12-13 00:58:48 +00:00
|
|
|
var l int
|
|
|
|
for _, h := range history {
|
2020-05-28 20:46:33 +00:00
|
|
|
if l >= len(remote.Descriptors) {
|
2017-12-13 00:58:48 +00:00
|
|
|
h.EmptyLayer = true
|
|
|
|
}
|
|
|
|
if !h.EmptyLayer {
|
|
|
|
l++
|
|
|
|
}
|
|
|
|
historyCopy = append(historyCopy, h)
|
|
|
|
}
|
|
|
|
history = historyCopy
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
if len(remote.Descriptors) > historyLayers {
|
2017-12-13 00:58:48 +00:00
|
|
|
// some history items are missing. add them based on the ref metadata
|
2018-05-04 20:29:04 +00:00
|
|
|
for _, md := range refMeta[historyLayers:] {
|
2021-07-26 08:53:30 +00:00
|
|
|
history = append(history, ocispecs.History{
|
2020-05-28 20:46:33 +00:00
|
|
|
Created: md.createdAt,
|
2018-05-04 20:29:04 +00:00
|
|
|
CreatedBy: md.description,
|
2017-12-13 00:58:48 +00:00
|
|
|
Comment: "buildkit.exporter.image.v0",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var layerIndex int
|
|
|
|
for i, h := range history {
|
|
|
|
if !h.EmptyLayer {
|
2020-10-19 04:55:50 +00:00
|
|
|
if remote.Descriptors[layerIndex].Digest == exptypes.EmptyGZLayer {
|
2017-12-13 00:58:48 +00:00
|
|
|
h.EmptyLayer = true
|
2020-05-28 20:46:33 +00:00
|
|
|
remote.Descriptors = append(remote.Descriptors[:layerIndex], remote.Descriptors[layerIndex+1:]...)
|
2017-12-13 00:58:48 +00:00
|
|
|
} else {
|
2020-10-19 04:02:44 +00:00
|
|
|
if h.Created == nil {
|
|
|
|
h.Created = refMeta[layerIndex].createdAt
|
|
|
|
}
|
2017-12-13 00:58:48 +00:00
|
|
|
layerIndex++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
history[i] = h
|
|
|
|
}
|
|
|
|
|
2019-01-09 19:28:13 +00:00
|
|
|
// Find the first new layer time. Otherwise, the history item for a first
|
|
|
|
// metadata command would be the creation time of a base image layer.
|
|
|
|
// If there is no such then the last layer with timestamp.
|
|
|
|
var created *time.Time
|
|
|
|
var noCreatedTime bool
|
|
|
|
for _, h := range history {
|
|
|
|
if h.Created != nil {
|
|
|
|
created = h.Created
|
|
|
|
if noCreatedTime {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
noCreatedTime = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in created times for all history items to be either the first new
|
|
|
|
// layer time or the previous layer.
|
|
|
|
noCreatedTime = false
|
|
|
|
for i, h := range history {
|
|
|
|
if h.Created != nil {
|
|
|
|
if noCreatedTime {
|
|
|
|
created = h.Created
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
noCreatedTime = true
|
|
|
|
h.Created = created
|
|
|
|
}
|
|
|
|
history[i] = h
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
// convert between oci and docker media types (or vice versa) if needed
|
|
|
|
remote.Descriptors = compression.ConvertAllLayerMediaTypes(oci, remote.Descriptors...)
|
|
|
|
|
|
|
|
return remote, history
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:29:04 +00:00
|
|
|
type refMetadata struct {
|
|
|
|
description string
|
2020-05-28 20:46:33 +00:00
|
|
|
createdAt *time.Time
|
2018-05-04 20:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRefMetadata(ref cache.ImmutableRef, limit int) []refMetadata {
|
2017-12-13 00:58:48 +00:00
|
|
|
if ref == nil {
|
2021-10-28 19:38:08 +00:00
|
|
|
return make([]refMetadata, limit)
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
2021-10-28 19:38:08 +00:00
|
|
|
|
|
|
|
layerChain := ref.LayerChain()
|
|
|
|
defer layerChain.Release(context.TODO())
|
|
|
|
|
|
|
|
if limit < len(layerChain) {
|
|
|
|
layerChain = layerChain[len(layerChain)-limit:]
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
2021-10-28 19:38:08 +00:00
|
|
|
|
|
|
|
metas := make([]refMetadata, len(layerChain))
|
|
|
|
for i, layer := range layerChain {
|
|
|
|
meta := &metas[i]
|
|
|
|
|
|
|
|
if description := layer.GetDescription(); description != "" {
|
|
|
|
meta.description = description
|
|
|
|
} else {
|
|
|
|
meta.description = "created by buildkit" // shouldn't be shown but don't fail build
|
|
|
|
}
|
|
|
|
|
|
|
|
createdAt := layer.GetCreatedAt()
|
|
|
|
meta.createdAt = &createdAt
|
2017-12-28 23:03:49 +00:00
|
|
|
}
|
2021-10-28 19:38:08 +00:00
|
|
|
return metas
|
2017-12-13 00:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func oneOffProgress(ctx context.Context, id string) func(err error) error {
|
2021-06-28 16:49:32 +00:00
|
|
|
pw, _, _ := progress.NewFromContext(ctx)
|
2017-12-13 00:58:48 +00:00
|
|
|
now := time.Now()
|
|
|
|
st := progress.Status{
|
|
|
|
Started: &now,
|
|
|
|
}
|
|
|
|
pw.Write(id, st)
|
|
|
|
return func(err error) error {
|
|
|
|
// TODO: set error on status
|
|
|
|
now := time.Now()
|
|
|
|
st.Completed = &now
|
|
|
|
pw.Write(id, st)
|
|
|
|
pw.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|