From b7d1820e139bee90e15a4be6a420e69048272d95 Mon Sep 17 00:00:00 2001 From: jonjohnsonjr Date: Tue, 25 Jun 2019 16:30:25 -0700 Subject: [PATCH] Compress layers when we Build them (#47) 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)