From cea64e0828f344c4f265d61b7e7e46a6af98ad92 Mon Sep 17 00:00:00 2001 From: Jon Johnson Date: Tue, 25 Jun 2019 14:53:52 -0700 Subject: [PATCH] Compress layers when we Build them This avoids double-compressing later. --- pkg/build/gobuild.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index 4d6b3022f9..5555d792c2 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -17,6 +17,7 @@ package build import ( "archive/tar" "bytes" + "compress/gzip" "errors" gb "go/build" "io" @@ -170,7 +171,14 @@ func tarAddDirectories(tw *tar.Writer, dir string) error { func tarBinary(name, binary string) (*bytes.Buffer, error) { buf := bytes.NewBuffer(nil) - tw := tar.NewWriter(buf) + // Compress this before calling tarball.LayerFromOpener, since it eagerly + // calculates digests and diffids. This prevents us from double compressing + // the layer when we have to actually upload the blob. + // + // https://github.com/google/go-containerregistry/issues/413 + gw, _ := gzip.NewWriterLevel(buf, gzip.BestSpeed) + defer gw.Close() + tw := tar.NewWriter(gw) defer tw.Close() // write the parent directories to the tarball archive @@ -221,7 +229,14 @@ const kodataRoot = "/var/run/ko" func tarKoData(importpath string) (*bytes.Buffer, error) { buf := bytes.NewBuffer(nil) - tw := tar.NewWriter(buf) + // Compress this before calling tarball.LayerFromOpener, since it eagerly + // calculates digests and diffids. This prevents us from double compressing + // the layer when we have to actually upload the blob. + // + // https://github.com/google/go-containerregistry/issues/413 + gw, _ := gzip.NewWriterLevel(buf, gzip.BestSpeed) + defer gw.Close() + tw := tar.NewWriter(gw) defer tw.Close() root, err := kodataPath(importpath)