From 3e83d37373550dc6da40ebd848b9e584405d7c20 Mon Sep 17 00:00:00 2001 From: Lavrenti Frobeen Date: Thu, 23 Feb 2023 16:22:10 +0100 Subject: [PATCH] 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. --- cmd/executor/cmd/root.go | 2 +- pkg/config/options.go | 31 ++++++++++++++++++++++++++++--- pkg/executor/push.go | 2 -- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index 257e1e8a34..5597d33dfc 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 (gzip, zstd)") + 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.") diff --git a/pkg/config/options.go b/pkg/config/options.go index 761c3933c3..b330f4a263 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -22,8 +22,6 @@ import ( "strconv" "strings" "time" - - "github.com/google/go-containerregistry/pkg/compression" ) // CacheOptions are base image cache options that are set by command line arguments @@ -70,7 +68,7 @@ type KanikoOptions struct { ImageNameDigestFile string ImageNameTagDigestFile string OCILayoutPath string - Compression compression.Compression + Compression Compression CompressionLevel int ImageFSExtractRetry int SingleSnapshot bool @@ -129,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 diff --git a/pkg/executor/push.go b/pkg/executor/push.go index 1a8b308aa0..f9ae1a432b 100644 --- a/pkg/executor/push.go +++ b/pkg/executor/push.go @@ -308,8 +308,6 @@ func pushLayerToCache(opts *config.KanikoOptions, cacheKey string, tarPath strin 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 }