diff --git a/cmd/kaniko-docker/main.go b/cmd/kaniko-docker/main.go index 6f53c8c..990c5c1 100644 --- a/cmd/kaniko-docker/main.go +++ b/cmd/kaniko-docker/main.go @@ -104,6 +104,21 @@ func main() { Usage: "Specify one of full, redo or time as snapshot mode", EnvVar: "PLUGIN_SNAPSHOT_MODE", }, + cli.BoolFlag{ + Name: "enable-cache", + Usage: "Set this flag to opt into caching with kaniko", + EnvVar: "PLUGIN_ENABLE_CACHE", + }, + cli.StringFlag{ + Name: "cache-repo", + Usage: "Remote repository that will be used to store cached layers. enable-cache needs to be set to use this flag", + EnvVar: "PLUGIN_CACHE_REPO", + }, + cli.IntFlag{ + Name: "cache-ttl", + Usage: "Cache timeout in hours. Defaults to two weeks.", + EnvVar: "PLUGIN_CACHE_TTL", + }, } if err := app.Run(os.Args); err != nil { @@ -128,6 +143,9 @@ func run(c *cli.Context) error { Labels: c.StringSlice("custom-labels"), SkipTlsVerify: c.Bool("skip-tls-verify"), SnapshotMode: c.String("snapshot-mode"), + EnableCache: c.Bool("enable-cache"), + CacheRepo: c.String("cache-repo"), + CacheTTL: c.Int("cache-ttl"), }, } return plugin.Exec() diff --git a/cmd/kaniko-ecr/main.go b/cmd/kaniko-ecr/main.go index d150f17..2271398 100644 --- a/cmd/kaniko-ecr/main.go +++ b/cmd/kaniko-ecr/main.go @@ -94,6 +94,21 @@ func main() { Usage: "Specify one of full, redo or time as snapshot mode", EnvVar: "PLUGIN_SNAPSHOT_MODE", }, + cli.BoolFlag{ + Name: "enable-cache", + Usage: "Set this flag to opt into caching with kaniko", + EnvVar: "PLUGIN_ENABLE_CACHE", + }, + cli.StringFlag{ + Name: "cache-repo", + Usage: "Remote repository that will be used to store cached layers. enable-cache needs to be set to use this flag", + EnvVar: "PLUGIN_CACHE_REPO", + }, + cli.IntFlag{ + Name: "cache-ttl", + Usage: "Cache timeout in hours. Defaults to two weeks.", + EnvVar: "PLUGIN_CACHE_TTL", + }, } if err := app.Run(os.Args); err != nil { @@ -117,6 +132,9 @@ func run(c *cli.Context) error { Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")), Labels: c.StringSlice("custom-labels"), SnapshotMode: c.String("snapshot-mode"), + EnableCache: c.Bool("enable-cache"), + CacheRepo: c.String("cache-repo"), + CacheTTL: c.Int("cache-ttl"), }, } return plugin.Exec() diff --git a/cmd/kaniko-gcr/main.go b/cmd/kaniko-gcr/main.go index 216c015..0bd0d92 100644 --- a/cmd/kaniko-gcr/main.go +++ b/cmd/kaniko-gcr/main.go @@ -90,6 +90,21 @@ func main() { Usage: "Specify one of full, redo or time as snapshot mode", EnvVar: "PLUGIN_SNAPSHOT_MODE", }, + cli.BoolFlag{ + Name: "enable-cache", + Usage: "Set this flag to opt into caching with kaniko", + EnvVar: "PLUGIN_ENABLE_CACHE", + }, + cli.StringFlag{ + Name: "cache-repo", + Usage: "Remote repository that will be used to store cached layers. enable-cache needs to be set to use this flag", + EnvVar: "PLUGIN_CACHE_REPO", + }, + cli.IntFlag{ + Name: "cache-ttl", + Usage: "Cache timeout in hours. Defaults to two weeks.", + EnvVar: "PLUGIN_CACHE_TTL", + }, } if err := app.Run(os.Args); err != nil { @@ -117,6 +132,9 @@ func run(c *cli.Context) error { Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")), Labels: c.StringSlice("custom-labels"), SnapshotMode: c.String("snapshot-mode"), + EnableCache: c.Bool("enable-cache"), + CacheRepo: c.String("cache-repo"), + CacheTTL: c.Int("cache-ttl"), }, } return plugin.Exec() diff --git a/kaniko.go b/kaniko.go index 3a13ce7..1ca0351 100644 --- a/kaniko.go +++ b/kaniko.go @@ -19,6 +19,9 @@ type ( Labels []string // Label map SkipTlsVerify bool // Docker skip tls certificate verify for registry SnapshotMode string // Kaniko snapshot mode + EnableCache bool // Whether to enable kaniko cache + CacheRepo string // Remote repository that will be used to store cached layers + CacheTTL int // Cache timeout in hours } // Plugin defines the Docker plugin parameters. @@ -67,6 +70,18 @@ func (p Plugin) Exec() error { cmdArgs = append(cmdArgs, fmt.Sprintf("--snapshotMode=%s", p.Build.SnapshotMode)) } + if p.Build.EnableCache == true { + cmdArgs = append(cmdArgs, fmt.Sprintf("--cache=true")) + } + + if p.Build.CacheRepo != "" { + cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-repo=%s", p.Build.CacheRepo)) + } + + if p.Build.CacheTTL != 0 { + cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-ttl=%d", p.Build.CacheTTL)) + } + cmd := exec.Command("/kaniko/executor", cmdArgs...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr