From 0d22b5056f9dd0f43a02249b5bf62e7c2b3c78dd Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 12 Jul 2019 11:50:50 -0700 Subject: [PATCH] exporter: keep lease when exporting images As manifests may be temporary if the same image is built in parallel one build can delete the manifest+config before the export for another finishes. Signed-off-by: Tonis Tiigi --- exporter/containerimage/export.go | 10 ++++++++++ exporter/oci/export.go | 9 +++++++++ vendor/modules.txt | 2 +- worker/base/worker.go | 3 +++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/exporter/containerimage/export.go b/exporter/containerimage/export.go index e2dd2eee..c25b14bf 100644 --- a/exporter/containerimage/export.go +++ b/exporter/containerimage/export.go @@ -10,11 +10,13 @@ import ( "github.com/containerd/containerd/content" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/images" + "github.com/containerd/containerd/leases" "github.com/containerd/containerd/platforms" "github.com/containerd/containerd/rootfs" "github.com/moby/buildkit/exporter" "github.com/moby/buildkit/session" "github.com/moby/buildkit/snapshot" + "github.com/moby/buildkit/util/leaseutil" "github.com/moby/buildkit/util/push" "github.com/moby/buildkit/util/resolver" digest "github.com/opencontainers/go-digest" @@ -37,6 +39,7 @@ type Opt struct { ImageWriter *ImageWriter Images images.Store ResolverOpt resolver.ResolveOptionsFunc + LeaseManager leases.Manager } type imageExporter struct { @@ -140,6 +143,13 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source) for k, v := range e.meta { src.Metadata[k] = v } + + ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager) + if err != nil { + return nil, err + } + defer done(context.TODO()) + desc, err := e.opt.ImageWriter.Commit(ctx, src, e.ociTypes) if err != nil { return nil, err diff --git a/exporter/oci/export.go b/exporter/oci/export.go index 3349157a..1d20fa45 100644 --- a/exporter/oci/export.go +++ b/exporter/oci/export.go @@ -8,12 +8,14 @@ import ( "github.com/containerd/containerd/images" "github.com/containerd/containerd/images/oci" + "github.com/containerd/containerd/leases" "github.com/docker/distribution/reference" "github.com/moby/buildkit/exporter" "github.com/moby/buildkit/exporter/containerimage" "github.com/moby/buildkit/session" "github.com/moby/buildkit/session/filesync" "github.com/moby/buildkit/util/dockerexporter" + "github.com/moby/buildkit/util/leaseutil" "github.com/moby/buildkit/util/progress" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" @@ -32,6 +34,7 @@ type Opt struct { SessionManager *session.Manager ImageWriter *containerimage.ImageWriter Variant ExporterVariant + LeaseManager leases.Manager } type imageExporter struct { @@ -113,6 +116,12 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source) src.Metadata[k] = v } + ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager) + if err != nil { + return nil, err + } + defer done(context.TODO()) + desc, err := e.opt.ImageWriter.Commit(ctx, src, e.ociTypes) if err != nil { return nil, err diff --git a/vendor/modules.txt b/vendor/modules.txt index 889c9fbe..314f0f7a 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -46,13 +46,13 @@ github.com/containerd/containerd/containers github.com/containerd/containerd/contrib/seccomp github.com/containerd/containerd/namespaces github.com/containerd/containerd/errdefs +github.com/containerd/containerd/leases github.com/containerd/containerd/rootfs github.com/containerd/containerd/images/oci github.com/containerd/containerd/api/services/content/v1 github.com/containerd/containerd/content/proxy github.com/containerd/containerd/services/content/contentserver github.com/containerd/containerd/reference -github.com/containerd/containerd/leases github.com/containerd/containerd/metadata github.com/containerd/containerd/remotes/docker/schema1 github.com/containerd/containerd/images/archive diff --git a/worker/base/worker.go b/worker/base/worker.go index c12180e5..cacb7807 100644 --- a/worker/base/worker.go +++ b/worker/base/worker.go @@ -255,6 +255,7 @@ func (w *Worker) Exporter(name string, sm *session.Manager) (exporter.Exporter, SessionManager: sm, ImageWriter: w.imageWriter, ResolverOpt: w.ResolveOptionsFunc, + LeaseManager: w.LeaseManager, }) case client.ExporterLocal: return localexporter.New(localexporter.Opt{ @@ -269,12 +270,14 @@ func (w *Worker) Exporter(name string, sm *session.Manager) (exporter.Exporter, SessionManager: sm, ImageWriter: w.imageWriter, Variant: ociexporter.VariantOCI, + LeaseManager: w.LeaseManager, }) case client.ExporterDocker: return ociexporter.New(ociexporter.Opt{ SessionManager: sm, ImageWriter: w.imageWriter, Variant: ociexporter.VariantDocker, + LeaseManager: w.LeaseManager, }) default: return nil, errors.Errorf("exporter %q could not be found", name)