Skip to content

Commit

Permalink
leaseutil: mark temporary leases with timestamps
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Oct 16, 2019
1 parent 31a9aee commit 339d4b2
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 57 deletions.
8 changes: 4 additions & 4 deletions cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/snapshot"
"github.com/opencontainers/go-digest"
imagespaceidentity "github.com/opencontainers/image-spec/identity"
imagespecidentity "github.com/opencontainers/image-spec/identity"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -98,7 +98,7 @@ func (cm *cacheManager) GetByBlob(ctx context.Context, desc ocispec.Descriptor,
return nil, err
}
chainID := diffID
blobChainID := imagespaceidentity.ChainID([]digest.Digest{desc.Digest, diffID})
blobChainID := imagespecidentity.ChainID([]digest.Digest{desc.Digest, diffID})

if _, err := cm.ContentStore.Info(ctx, desc.Digest); err != nil {
return nil, errors.Wrapf(err, "failed to get blob %s", desc.Digest)
Expand All @@ -111,8 +111,8 @@ func (cm *cacheManager) GetByBlob(ctx context.Context, desc ocispec.Descriptor,
if pInfo.ChainID == "" || pInfo.BlobChainID == "" {
return nil, errors.Errorf("failed to get ref by blob on non-adressable parent")
}
chainID = imagespaceidentity.ChainID([]digest.Digest{pInfo.ChainID, chainID})
blobChainID = imagespaceidentity.ChainID([]digest.Digest{pInfo.BlobChainID, blobChainID})
chainID = imagespecidentity.ChainID([]digest.Digest{pInfo.ChainID, chainID})
blobChainID = imagespecidentity.ChainID([]digest.Digest{pInfo.BlobChainID, blobChainID})
p2, err := cm.Get(ctx, parent.ID(), NoUpdateLastUsed)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion cache/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func queueParent(si *metadata.StorageItem, parent string) error {
}
v, err := metadata.NewValue(parent)
if err != nil {
return errors.Wrap(err, "failed to create size value")
return errors.Wrap(err, "failed to create parent value")
}
si.Update(func(b *bolt.Bucket) error {
return si.SetValue(b, keyParent, v)
Expand Down
6 changes: 3 additions & 3 deletions cache/migrate_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func MigrateV2(ctx context.Context, from, to string, cs content.Store, s snapsho
// add committed, parent, snapshot
for id, item := range byID {
em := getEqualMutable(item)
var parent string
if em == "" {
info, err := s.Stat(ctx, id)
if err != nil {
Expand All @@ -115,8 +114,9 @@ func MigrateV2(ctx context.Context, from, to string, cs content.Store, s snapsho
if info.Kind == snapshots.KindCommitted {
queueCommitted(item)
}
parent = info.Parent
queueParent(item, parent)
if info.Parent != "" {
queueParent(item, info.Parent)
}
} else {
queueCommitted(item)
}
Expand Down
14 changes: 7 additions & 7 deletions cache/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/util/flightcontrol"
"github.com/moby/buildkit/util/leaseutil"
"github.com/opencontainers/go-digest"
imagespaceidentity "github.com/opencontainers/image-spec/identity"
imagespecidentity "github.com/opencontainers/image-spec/identity"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -230,11 +231,10 @@ func (cr *cacheRecord) Mount(ctx context.Context, readonly bool) (snapshot.Mount
l, err := cr.cm.LeaseManager.Create(ctx, func(l *leases.Lease) error {
l.ID = view
l.Labels = map[string]string{
"containerd.io/gc.flat": time.Now().UTC().Format(time.RFC3339Nano),
"buildkit/lease.temporary": "",
"containerd.io/gc.flat": time.Now().UTC().Format(time.RFC3339Nano),
}
return nil
})
}, leaseutil.MakeTemporary)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -404,10 +404,10 @@ func (sr *immutableRef) SetBlob(ctx context.Context, desc ocispec.Descriptor) er
queueDiffID(sr.md, diffID.String())
queueBlob(sr.md, desc.Digest.String())
chainID := diffID
blobChainID := imagespaceidentity.ChainID([]digest.Digest{desc.Digest, diffID})
blobChainID := imagespecidentity.ChainID([]digest.Digest{desc.Digest, diffID})
if parentChainID != "" {
chainID = imagespaceidentity.ChainID([]digest.Digest{parentChainID, chainID})
blobChainID = imagespaceidentity.ChainID([]digest.Digest{parentBlobChainID, blobChainID})
chainID = imagespecidentity.ChainID([]digest.Digest{parentChainID, chainID})
blobChainID = imagespecidentity.ChainID([]digest.Digest{parentBlobChainID, blobChainID})
}
queueChainID(sr.md, chainID.String())
queueBlobChainID(sr.md, blobChainID.String())
Expand Down
8 changes: 1 addition & 7 deletions exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source)
src.Metadata[k] = v
}

ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager, func(l *leases.Lease) error {
if l.Labels == nil {
l.Labels = map[string]string{}
}
l.Labels["buildkit/lease.temporary"] = ""
return nil
})
ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager, leaseutil.MakeTemporary)
if err != nil {
return nil, err
}
Expand Down
8 changes: 1 addition & 7 deletions exporter/oci/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source)
src.Metadata[k] = v
}

ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager, func(l *leases.Lease) error {
if l.Labels == nil {
l.Labels = map[string]string{}
}
l.Labels["buildkit/lease.temporary"] = ""
return nil
})
ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager, leaseutil.MakeTemporary)
if err != nil {
return nil, err
}
Expand Down
8 changes: 1 addition & 7 deletions source/containerimage/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,7 @@ func (p *puller) Snapshot(ctx context.Context) (ir cache.ImmutableRef, err error
// workaround for gcr, authentication not supported on blob endpoints
pull.EnsureManifestRequested(ctx, p.Puller.Resolver, p.Puller.Src.String())

ctx, done, err := leaseutil.WithLease(ctx, p.LeaseManager, func(l *leases.Lease) error {
if l.Labels == nil {
l.Labels = map[string]string{}
}
l.Labels["buildkit/lease.temporary"] = ""
return nil
})
ctx, done, err := leaseutil.WithLease(ctx, p.LeaseManager, leaseutil.MakeTemporary)
if err != nil {
return nil, err
}
Expand Down
8 changes: 1 addition & 7 deletions util/imageutil/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ func Config(ctx context.Context, str string, resolver remotes.Resolver, cache Co
}

if leaseManager != nil {
ctx2, done, err := leaseutil.WithLease(ctx, leaseManager, leases.WithExpiration(5*time.Minute), func(l *leases.Lease) error {
if l.Labels == nil {
l.Labels = map[string]string{}
}
l.Labels["buildkit/lease.temporary"] = ""
return nil
})
ctx2, done, err := leaseutil.WithLease(ctx, leaseManager, leases.WithExpiration(5*time.Minute), leaseutil.MakeTemporary)
if err != nil {
return "", nil, errors.WithStack(err)
}
Expand Down
8 changes: 8 additions & 0 deletions util/leaseutil/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func WithLease(ctx context.Context, ls leases.Manager, opts ...leases.Opt) (cont
}, nil
}

func MakeTemporary(l *leases.Lease) error {
if l.Labels == nil {
l.Labels = map[string]string{}
}
l.Labels["buildkit/lease.temporary"] = time.Now().UTC().Format(time.RFC3339Nano)
return nil
}

func WithNamespace(lm leases.Manager, ns string) leases.Manager {
return &nsLM{manager: lm, ns: ns}
}
Expand Down
16 changes: 2 additions & 14 deletions worker/base/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,7 @@ func (w *Worker) Exporter(name string, sm *session.Manager) (exporter.Exporter,
}

func (w *Worker) GetRemote(ctx context.Context, ref cache.ImmutableRef, createIfNeeded bool) (*solver.Remote, error) {
ctx, done, err := leaseutil.WithLease(ctx, w.LeaseManager, func(l *leases.Lease) error {
if l.Labels == nil {
l.Labels = map[string]string{}
}
l.Labels["buildkit/lease.temporary"] = ""
return nil
})
ctx, done, err := leaseutil.WithLease(ctx, w.LeaseManager, leaseutil.MakeTemporary)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -407,13 +401,7 @@ func getCreatedTimes(ref cache.ImmutableRef) (out []time.Time) {
}

func (w *Worker) FromRemote(ctx context.Context, remote *solver.Remote) (ref cache.ImmutableRef, err error) {
ctx, done, err := leaseutil.WithLease(ctx, w.LeaseManager, func(l *leases.Lease) error {
if l.Labels == nil {
l.Labels = map[string]string{}
}
l.Labels["buildkit/lease.temporary"] = ""
return nil
})
ctx, done, err := leaseutil.WithLease(ctx, w.LeaseManager, leaseutil.MakeTemporary)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 339d4b2

Please sign in to comment.