Skip to content

Commit

Permalink
Add support for configurable compression algorithm (gzip, zstd) and c…
Browse files Browse the repository at this point in the history
…ompression level

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.
  • Loading branch information
LFrobeen committed Mar 22, 2023
1 parent 6ca4c4b commit f056b5a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ 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 (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.")
RootCmd.PersistentFlags().BoolVarP(&opts.Cleanup, "cleanup", "", false, "Clean the filesystem at the end")
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,6 +70,8 @@ type KanikoOptions struct {
ImageNameDigestFile string
ImageNameTagDigestFile string
OCILayoutPath string
Compression compression.Compression
CompressionLevel int
ImageFSExtractRetry int
SingleSnapshot bool
Reproducible bool
Expand Down
20 changes: 16 additions & 4 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,24 @@ func (s *stageBuilder) saveSnapshotToLayer(tarPath string) (v1.Layer, error) {
return nil, nil
}

var layer v1.Layer
var layerOpts []tarball.LayerOption

if s.opts.CompressedCaching == true {
layer, err = tarball.LayerFromFile(tarPath, tarball.WithCompressedCaching)
} else {
layer, err = tarball.LayerFromFile(tarPath)
layerOpts = append(layerOpts, tarball.WithCompressedCaching)
}

if s.opts.CompressionLevel > 0 {
layerOpts = append(layerOpts, tarball.WithCompressionLevel(s.opts.CompressionLevel))
}

switch s.opts.Compression {
case "zstd":
layerOpts = append(layerOpts, tarball.WithCompression("zstd"))
default:
// layer already gzipped by default
}

layer, err := tarball.LayerFromFile(tarPath, layerOpts...)
if err != nil {
return nil, err
}
Expand Down
23 changes: 18 additions & 5 deletions pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,29 @@ func writeImageOutputs(image v1.Image, destRefs []name.Tag) error {
// pushLayerToCache pushes layer (tagged with cacheKey) to opts.CacheRepo
// if opts.CacheRepo doesn't exist, infer the cache from the given destination
func pushLayerToCache(opts *config.KanikoOptions, cacheKey string, tarPath string, createdBy string) error {
var layer v1.Layer
var err error
var layerOpts []tarball.LayerOption
if opts.CompressedCaching == true {
layer, err = tarball.LayerFromFile(tarPath, tarball.WithCompressedCaching)
} else {
layer, err = tarball.LayerFromFile(tarPath)
layerOpts = append(layerOpts, tarball.WithCompressedCaching)
}

if opts.CompressionLevel > 0 {
layerOpts = append(layerOpts, tarball.WithCompressionLevel(opts.CompressionLevel))
}

switch opts.Compression {
case "zstd":
layerOpts = append(layerOpts, tarball.WithCompression("zstd"))
case "none":
layerOpts = append(layerOpts, tarball.WithCompression("none"))
default:
// layer already gzipped by default
}

layer, err := tarball.LayerFromFile(tarPath, layerOpts...)
if err != nil {
return err
}

cache, err := cache.Destination(opts, cacheKey)
if err != nil {
return errors.Wrap(err, "getting cache destination")
Expand Down

0 comments on commit f056b5a

Please sign in to comment.