From fce72f0ec0d4de6a60e8cb4aca2aeabe5aa8c16b Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Mon, 30 Mar 2015 11:19:12 -0700 Subject: [PATCH] Compress layers on push to a v2 registry When buffering to file add support for compressing the tar contents. Since digest should be computed while writing buffer, include digest creation during buffer. Signed-off-by: Derek McGowan (github: dmcgowan) Upstream-commit: 851c64725d0b1b37e51fa0d0df744bbe82ad4c7b Component: engine --- components/engine/graph/graph.go | 24 ++++++++++++++++++------ components/engine/graph/push.go | 9 +-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/components/engine/graph/graph.go b/components/engine/graph/graph.go index 994ce30289..087a6f093c 100644 --- a/components/engine/graph/graph.go +++ b/components/engine/graph/graph.go @@ -1,6 +1,8 @@ package graph import ( + "compress/gzip" + "crypto/sha256" "fmt" "io" "io/ioutil" @@ -13,6 +15,7 @@ import ( "time" "github.com/Sirupsen/logrus" + "github.com/docker/distribution/digest" "github.com/docker/docker/autogen/dockerversion" "github.com/docker/docker/daemon/graphdriver" "github.com/docker/docker/image" @@ -242,18 +245,27 @@ func (graph *Graph) newTempFile() (*os.File, error) { return ioutil.TempFile(tmp, "") } -func bufferToFile(f *os.File, src io.Reader) (int64, error) { - n, err := io.Copy(f, src) +func bufferToFile(f *os.File, src io.Reader) (int64, digest.Digest, error) { + var ( + h = sha256.New() + w = gzip.NewWriter(io.MultiWriter(f, h)) + ) + _, err := io.Copy(w, src) + w.Close() if err != nil { - return n, err + return 0, "", err } if err = f.Sync(); err != nil { - return n, err + return 0, "", err + } + n, err := f.Seek(0, os.SEEK_CUR) + if err != nil { + return 0, "", err } if _, err := f.Seek(0, 0); err != nil { - return n, err + return 0, "", err } - return n, nil + return n, digest.NewDigest("sha256", h), nil } // setupInitLayer populates a directory with mountpoints suitable diff --git a/components/engine/graph/push.go b/components/engine/graph/push.go index 927c13c9d5..767f118c50 100644 --- a/components/engine/graph/push.go +++ b/components/engine/graph/push.go @@ -1,7 +1,6 @@ package graph import ( - "crypto/sha256" "encoding/json" "errors" "fmt" @@ -13,7 +12,6 @@ import ( "sync" "github.com/Sirupsen/logrus" - "github.com/docker/distribution/digest" "github.com/docker/docker/engine" "github.com/docker/docker/image" "github.com/docker/docker/pkg/progressreader" @@ -465,12 +463,7 @@ func (s *TagStore) pushV2Image(r *registry.Session, img *image.Image, endpoint * os.Remove(tf.Name()) }() - h := sha256.New() - size, err := bufferToFile(tf, io.TeeReader(arch, h)) - if err != nil { - return "", err - } - dgst := digest.NewDigest("sha256", h) + size, dgst, err := bufferToFile(tf, arch) // Send the layer logrus.Debugf("rendered layer for %s of [%d] size", img.ID, size)