From dbc0ffaa875e4b3c7f22debe8c6d7f643a5a3c3c Mon Sep 17 00:00:00 2001 From: Lavrenti Frobeen Date: Thu, 26 Jan 2023 10:40:51 +0100 Subject: [PATCH] Add support for configurable compression algorithm (gzip, zstd) and compression level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to make the layer compression in kaniko configurable, so we have added two optional command line arguments “--compression” and “--compression-level”. The former allows the user to specify a compression algorithm (zstd, gzip) and the latter can be used to specify the compression level. Depending on the selected compression algorithm and level we modify the set of layerOptions that are used to create tarball layers in `push.go` and `build.go`. The actual implementation of the zstd support can be found in our fork of the go-containerregistry package for which we have filed this PR: google/go-containerregistry#1487 The changes should be fully backwards compatible. --- cmd/executor/cmd/root.go | 2 +- pkg/config/options.go | 4 +++- pkg/executor/build.go | 2 -- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index 68e7e86030..257e1e8a34 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -225,7 +225,7 @@ func addKanikoOptionsFlags() { RootCmd.PersistentFlags().StringVarP(&opts.ImageNameDigestFile, "image-name-with-digest-file", "", "", "Specify a file to save the image name w/ digest of the built image to.") RootCmd.PersistentFlags().StringVarP(&opts.ImageNameTagDigestFile, "image-name-tag-with-digest-file", "", "", "Specify a file to save the image name w/ image tag w/ digest of the built image to.") RootCmd.PersistentFlags().StringVarP(&opts.OCILayoutPath, "oci-layout-path", "", "", "Path to save the OCI image layout of the built image.") - RootCmd.PersistentFlags().StringVarP(&opts.Compression, "compression", "", "", "Compression algorithm") + RootCmd.PersistentFlags().StringVarP(&opts.Compression, "compression", "", "", "Compression algorithm (gzip, zstd)") RootCmd.PersistentFlags().IntVarP(&opts.CompressionLevel, "compression-level", "", -1, "Compression level") RootCmd.PersistentFlags().BoolVarP(&opts.Cache, "cache", "", false, "Use cache when building image") RootCmd.PersistentFlags().BoolVarP(&opts.CompressedCaching, "compressed-caching", "", true, "Compress the cached layers. Decreases build time, but increases memory usage.") diff --git a/pkg/config/options.go b/pkg/config/options.go index b6c24c3c2a..761c3933c3 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -22,6 +22,8 @@ import ( "strconv" "strings" "time" + + "github.com/google/go-containerregistry/pkg/compression" ) // CacheOptions are base image cache options that are set by command line arguments @@ -68,7 +70,7 @@ type KanikoOptions struct { ImageNameDigestFile string ImageNameTagDigestFile string OCILayoutPath string - Compression string + Compression compression.Compression CompressionLevel int ImageFSExtractRetry int SingleSnapshot bool diff --git a/pkg/executor/build.go b/pkg/executor/build.go index eead277794..46f3e57001 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -528,8 +528,6 @@ func (s *stageBuilder) saveSnapshotToLayer(tarPath string) (v1.Layer, error) { switch s.opts.Compression { case "zstd": layerOpts = append(layerOpts, tarball.WithCompression("zstd")) - case "none": - layerOpts = append(layerOpts, tarball.WithCompression("none")) default: // layer already gzipped by default }