Add support for zstd compression (#2313)
* Add support for configurable compression algorithm (gzip, zstd) and compression 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. * Restrict inputs for compression flag to gzip and zstd This change will ensure that users can only specify supported compression algorithms (`zstd`, `gzip`) to the `--compression` flag. * Fix incorrect type for switch statements on config.Compression
This commit is contained in:
parent
1aaf231c75
commit
76afb70790
|
|
@ -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.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.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.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.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.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")
|
RootCmd.PersistentFlags().BoolVarP(&opts.Cleanup, "cleanup", "", false, "Clean the filesystem at the end")
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,8 @@ type KanikoOptions struct {
|
||||||
ImageNameDigestFile string
|
ImageNameDigestFile string
|
||||||
ImageNameTagDigestFile string
|
ImageNameTagDigestFile string
|
||||||
OCILayoutPath string
|
OCILayoutPath string
|
||||||
|
Compression Compression
|
||||||
|
CompressionLevel int
|
||||||
ImageFSExtractRetry int
|
ImageFSExtractRetry int
|
||||||
SingleSnapshot bool
|
SingleSnapshot bool
|
||||||
Reproducible bool
|
Reproducible bool
|
||||||
|
|
@ -125,6 +127,33 @@ func (k *KanikoGitOptions) Set(s string) error {
|
||||||
return nil
|
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.
|
// WarmerOptions are options that are set by command line arguments to the cache warmer.
|
||||||
type WarmerOptions struct {
|
type WarmerOptions struct {
|
||||||
CacheOptions
|
CacheOptions
|
||||||
|
|
|
||||||
|
|
@ -515,12 +515,25 @@ func (s *stageBuilder) saveSnapshotToLayer(tarPath string) (v1.Layer, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var layer v1.Layer
|
var layerOpts []tarball.LayerOption
|
||||||
|
|
||||||
if s.opts.CompressedCaching == true {
|
if s.opts.CompressedCaching == true {
|
||||||
layer, err = tarball.LayerFromFile(tarPath, tarball.WithCompressedCaching)
|
layerOpts = append(layerOpts, tarball.WithCompressedCaching)
|
||||||
} else {
|
|
||||||
layer, err = tarball.LayerFromFile(tarPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -296,16 +296,28 @@ func writeImageOutputs(image v1.Image, destRefs []name.Tag) error {
|
||||||
// pushLayerToCache pushes layer (tagged with cacheKey) to opts.CacheRepo
|
// pushLayerToCache pushes layer (tagged with cacheKey) to opts.CacheRepo
|
||||||
// if opts.CacheRepo doesn't exist, infer the cache from the given destination
|
// 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 {
|
func pushLayerToCache(opts *config.KanikoOptions, cacheKey string, tarPath string, createdBy string) error {
|
||||||
var layer v1.Layer
|
var layerOpts []tarball.LayerOption
|
||||||
var err error
|
|
||||||
if opts.CompressedCaching == true {
|
if opts.CompressedCaching == true {
|
||||||
layer, err = tarball.LayerFromFile(tarPath, tarball.WithCompressedCaching)
|
layerOpts = append(layerOpts, tarball.WithCompressedCaching)
|
||||||
} else {
|
|
||||||
layer, err = tarball.LayerFromFile(tarPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache, err := cache.Destination(opts, cacheKey)
|
cache, err := cache.Destination(opts, cacheKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "getting cache destination")
|
return errors.Wrap(err, "getting cache destination")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue