Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for zstd compression #2313

Merged
merged 4 commits into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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().VarP(&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
29 changes: 29 additions & 0 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type KanikoOptions struct {
ImageNameDigestFile string
ImageNameTagDigestFile string
OCILayoutPath string
Compression Compression
CompressionLevel int
ImageFSExtractRetry int
SingleSnapshot bool
Reproducible bool
Expand Down Expand Up @@ -125,6 +127,33 @@ func (k *KanikoGitOptions) Set(s string) error {
return nil
}

// Compression is an enumeration of the supported compression algorithms
type Compression string

// The collection of known MediaType values.
const (
GZip Compression = "gzip"
ZStd Compression = "zstd"
)

func (c *Compression) String() string {
return string(*c)
}

func (c *Compression) Set(v string) error {
switch v {
case "gzip", "zstd":
*c = Compression(v)
return nil
default:
return errors.New(`must be either "gzip" or "zstd"`)
}
}

func (c *Compression) Type() string {
return "compression"
}

// WarmerOptions are options that are set by command line arguments to the cache warmer.
type WarmerOptions struct {
CacheOptions
Expand Down
21 changes: 17 additions & 4 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,25 @@ 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 config.ZStd:
layerOpts = append(layerOpts, tarball.WithCompression("zstd"))

case config.GZip:
// layer already gzipped by default
}

layer, err := tarball.LayerFromFile(tarPath, layerOpts...)
if err != nil {
return nil, err
}
Expand Down
22 changes: 17 additions & 5 deletions pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,28 @@ 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 config.ZStd:
layerOpts = append(layerOpts, tarball.WithCompression("zstd"))

case config.GZip:
// 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