2019-09-30 23:38:02 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/errdefs"
|
|
|
|
"github.com/containerd/containerd/images"
|
|
|
|
"github.com/containerd/containerd/leases"
|
|
|
|
"github.com/containerd/containerd/snapshots"
|
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
|
|
|
"github.com/moby/buildkit/snapshot"
|
2021-07-09 07:48:03 +00:00
|
|
|
"github.com/moby/buildkit/util/bklog"
|
2020-05-13 15:37:27 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2019-09-30 23:38:02 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func migrateChainID(si *metadata.StorageItem, all map[string]*metadata.StorageItem) (digest.Digest, digest.Digest, error) {
|
2021-07-09 00:09:35 +00:00
|
|
|
md := &cacheMetadata{si}
|
|
|
|
diffID := md.getDiffID()
|
2019-09-30 23:38:02 +00:00
|
|
|
if diffID == "" {
|
|
|
|
return "", "", nil
|
|
|
|
}
|
2021-07-09 00:09:35 +00:00
|
|
|
blobID := md.getBlob()
|
2019-09-30 23:38:02 +00:00
|
|
|
if blobID == "" {
|
|
|
|
return "", "", nil
|
|
|
|
}
|
2021-07-09 00:09:35 +00:00
|
|
|
chainID := md.getChainID()
|
|
|
|
blobChainID := md.getBlobChainID()
|
2019-09-30 23:38:02 +00:00
|
|
|
|
|
|
|
if chainID != "" && blobChainID != "" {
|
|
|
|
return chainID, blobChainID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
chainID = diffID
|
|
|
|
blobChainID = digest.FromBytes([]byte(blobID + " " + diffID))
|
|
|
|
|
2021-07-09 00:09:35 +00:00
|
|
|
parent := md.getParent()
|
2019-09-30 23:38:02 +00:00
|
|
|
if parent != "" {
|
|
|
|
pChainID, pBlobChainID, err := migrateChainID(all[parent], all)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
chainID = digest.FromBytes([]byte(pChainID + " " + chainID))
|
|
|
|
blobChainID = digest.FromBytes([]byte(pBlobChainID + " " + blobChainID))
|
|
|
|
}
|
|
|
|
|
2021-07-09 00:09:35 +00:00
|
|
|
md.queueChainID(chainID)
|
|
|
|
md.queueBlobChainID(blobChainID)
|
2019-09-30 23:38:02 +00:00
|
|
|
|
2021-07-09 00:09:35 +00:00
|
|
|
return chainID, blobChainID, md.commitMetadata()
|
2019-09-30 23:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MigrateV2(ctx context.Context, from, to string, cs content.Store, s snapshot.Snapshotter, lm leases.Manager) error {
|
|
|
|
_, err := os.Stat(to)
|
|
|
|
if err != nil {
|
2020-04-19 05:17:47 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2019-09-30 23:38:02 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(from)
|
|
|
|
if err != nil {
|
2020-04-19 05:17:47 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2019-09-30 23:38:02 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
tmpPath := to + ".tmp"
|
|
|
|
tmpFile, err := os.Create(tmpPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
src, err := os.Open(from)
|
|
|
|
if err != nil {
|
|
|
|
tmpFile.Close()
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(tmpFile, src); err != nil {
|
|
|
|
tmpFile.Close()
|
|
|
|
src.Close()
|
|
|
|
return errors.Wrapf(err, "failed to copy db for migration")
|
|
|
|
}
|
|
|
|
src.Close()
|
|
|
|
tmpFile.Close()
|
|
|
|
|
|
|
|
md, err := metadata.NewStore(tmpPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := md.All()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
byID := map[string]*metadata.StorageItem{}
|
|
|
|
for _, item := range items {
|
|
|
|
byID[item.ID()] = item
|
|
|
|
}
|
|
|
|
|
|
|
|
// add committed, parent, snapshot
|
|
|
|
for id, item := range byID {
|
2021-07-09 00:09:35 +00:00
|
|
|
md := &cacheMetadata{item}
|
|
|
|
em := md.getEqualMutable()
|
2019-09-30 23:38:02 +00:00
|
|
|
if em == "" {
|
|
|
|
info, err := s.Stat(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Kind == snapshots.KindCommitted {
|
2021-07-09 00:09:35 +00:00
|
|
|
md.queueCommitted(true)
|
2019-09-30 23:38:02 +00:00
|
|
|
}
|
2019-10-03 21:11:54 +00:00
|
|
|
if info.Parent != "" {
|
2021-07-09 00:09:35 +00:00
|
|
|
md.queueParent(info.Parent)
|
2019-10-03 21:11:54 +00:00
|
|
|
}
|
2019-09-30 23:38:02 +00:00
|
|
|
} else {
|
2021-07-09 00:09:35 +00:00
|
|
|
md.queueCommitted(true)
|
2019-09-30 23:38:02 +00:00
|
|
|
}
|
2021-07-09 00:09:35 +00:00
|
|
|
md.queueSnapshotID(id)
|
|
|
|
md.commitMetadata()
|
2019-09-30 23:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range byID {
|
2021-07-09 00:09:35 +00:00
|
|
|
md := &cacheMetadata{item}
|
|
|
|
em := md.getEqualMutable()
|
2019-09-30 23:38:02 +00:00
|
|
|
if em != "" {
|
2021-07-09 00:09:35 +00:00
|
|
|
if md.getParent() == "" {
|
|
|
|
emMd := &cacheMetadata{byID[em]}
|
|
|
|
md.queueParent(emMd.getParent())
|
|
|
|
md.commitMetadata()
|
2019-09-30 23:38:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type diffPair struct {
|
|
|
|
Blobsum string
|
|
|
|
DiffID string
|
|
|
|
}
|
|
|
|
// move diffID, blobsum to new location
|
|
|
|
for _, item := range byID {
|
|
|
|
v := item.Get("blobmapping.blob")
|
|
|
|
if v == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var blob diffPair
|
|
|
|
if err := v.Unmarshal(&blob); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2019-10-22 00:11:22 +00:00
|
|
|
if _, err := cs.Info(ctx, digest.Digest(blob.Blobsum)); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-07-09 00:09:35 +00:00
|
|
|
md := &cacheMetadata{item}
|
|
|
|
md.queueDiffID(digest.Digest(blob.DiffID))
|
|
|
|
md.queueBlob(digest.Digest(blob.Blobsum))
|
|
|
|
md.queueMediaType(images.MediaTypeDockerSchema2LayerGzip)
|
|
|
|
if err := md.commitMetadata(); err != nil {
|
2019-09-30 23:38:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate new chainid/blobsumid
|
|
|
|
for _, item := range byID {
|
|
|
|
if _, _, err := migrateChainID(item, byID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = context.TODO() // no cancellation allowed pass this point
|
|
|
|
|
|
|
|
// add new leases
|
|
|
|
for _, item := range byID {
|
2021-07-09 00:09:35 +00:00
|
|
|
md := &cacheMetadata{item}
|
2019-09-30 23:38:02 +00:00
|
|
|
l, err := lm.Create(ctx, func(l *leases.Lease) error {
|
|
|
|
l.ID = item.ID()
|
|
|
|
l.Labels = map[string]string{
|
|
|
|
"containerd.io/gc.flat": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// if we are running the migration twice
|
2020-04-19 05:17:47 +00:00
|
|
|
if errors.Is(err, errdefs.ErrAlreadyExists) {
|
2019-09-30 23:38:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "failed to create lease")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := lm.AddResource(ctx, l, leases.Resource{
|
2021-07-09 00:09:35 +00:00
|
|
|
ID: md.getSnapshotID(),
|
2019-09-30 23:38:02 +00:00
|
|
|
Type: "snapshots/" + s.Name(),
|
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to add snapshot %s to lease", item.ID())
|
|
|
|
}
|
|
|
|
|
2021-07-09 00:09:35 +00:00
|
|
|
if blobID := md.getBlob(); blobID != "" {
|
2019-09-30 23:38:02 +00:00
|
|
|
if err := lm.AddResource(ctx, l, leases.Resource{
|
2021-07-09 00:09:35 +00:00
|
|
|
ID: string(blobID),
|
2019-09-30 23:38:02 +00:00
|
|
|
Type: "content",
|
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to add blob %s to lease", item.ID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove old root labels
|
|
|
|
for _, item := range byID {
|
2021-07-09 00:09:35 +00:00
|
|
|
md := &cacheMetadata{item}
|
|
|
|
em := md.getEqualMutable()
|
2020-06-13 00:59:30 +00:00
|
|
|
if em == "" {
|
|
|
|
if _, err := s.Update(ctx, snapshots.Info{
|
2021-07-09 00:09:35 +00:00
|
|
|
Name: md.getSnapshotID(),
|
2020-06-13 00:59:30 +00:00
|
|
|
}, "labels.containerd.io/gc.root"); err != nil {
|
|
|
|
if !errors.Is(err, errdefs.ErrNotFound) {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-30 23:38:02 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 00:09:35 +00:00
|
|
|
if blob := md.getBlob(); blob != "" {
|
2020-06-13 00:59:30 +00:00
|
|
|
if _, err := cs.Update(ctx, content.Info{
|
|
|
|
Digest: digest.Digest(blob),
|
|
|
|
}, "labels.containerd.io/gc.root"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-30 23:38:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// previous implementation can leak views, just clean up all views
|
|
|
|
err = s.Walk(ctx, func(ctx context.Context, info snapshots.Info) error {
|
|
|
|
if info.Kind == snapshots.KindView {
|
|
|
|
if _, err := s.Update(ctx, snapshots.Info{
|
|
|
|
Name: info.Name,
|
|
|
|
}, "labels.containerd.io/gc.root"); err != nil {
|
2020-04-19 05:17:47 +00:00
|
|
|
if !errors.Is(err, errdefs.ErrNotFound) {
|
2019-09-30 23:38:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// switch to new DB
|
|
|
|
if err := md.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(tmpPath, to); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range byID {
|
2021-07-09 00:09:35 +00:00
|
|
|
md := &cacheMetadata{item}
|
|
|
|
bklog.G(ctx).Infof("migrated %s parent:%q snapshot:%v blob:%v diffid:%v chainID:%v blobChainID:%v",
|
|
|
|
item.ID(), md.getParent(), md.getSnapshotID(), md.getBlob(), md.getDiffID(), md.getChainID(), md.getBlobChainID())
|
2019-09-30 23:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|