Skip to content

Commit

Permalink
Restrict inputs for compression flag to gzip and zstd
Browse files Browse the repository at this point in the history
This change will ensure that users can only specify supported compression algorithms (`zstd`, `gzip`) to the `--compression` flag.
  • Loading branch information
LFrobeen committed Feb 23, 2023
1 parent bd331e0 commit 3e83d37
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
31 changes: 28 additions & 3 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,7 +68,7 @@ type KanikoOptions struct {
ImageNameDigestFile string
ImageNameTagDigestFile string
OCILayoutPath string
Compression compression.Compression
Compression Compression
CompressionLevel int
ImageFSExtractRetry int
SingleSnapshot bool
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 3e83d37

Please sign in to comment.