Skip to content

Commit

Permalink
feat(kaniko): Add --cache-run-layers flag (#9465)
Browse files Browse the repository at this point in the history
Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
  • Loading branch information
idsulik authored Sep 12, 2024
1 parent 1d83c09 commit f4e1501
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs-v2/content/en/schemas/v4beta12.json
Original file line number Diff line number Diff line change
Expand Up @@ -2941,6 +2941,11 @@
"x-intellij-html-description": "enables caching of copy layers.",
"default": "false"
},
"cacheRunLayers": {
"type": "boolean",
"description": "enables caching of run layers (default=true).",
"x-intellij-html-description": "enables caching of run layers (default=true)."
},
"hostPath": {
"type": "string",
"description": "specifies a path on the host that is mounted to each pod as read only cache volume containing base images. If set, must exist on each node and prepopulated with kaniko-warmer.",
Expand All @@ -2961,7 +2966,8 @@
"repo",
"hostPath",
"ttl",
"cacheCopyLayers"
"cacheCopyLayers",
"cacheRunLayers"
],
"additionalProperties": false,
"type": "object",
Expand Down
27 changes: 27 additions & 0 deletions pkg/skaffold/build/gcb/kaniko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package gcb

import (
"context"
"fmt"
"testing"

"google.golang.org/api/cloudbuild/v1"
Expand Down Expand Up @@ -104,6 +105,28 @@ func TestKanikoBuildSpec(t *testing.T) {
kaniko.CacheCopyLayersFlag,
},
},
{
description: "with Cache Run Layers is false",
artifact: &latest.KanikoArtifact{
DockerfilePath: "Dockerfile",
Cache: &latest.KanikoCache{CacheRunLayers: boolPtr(false)},
},
expectedArgs: []string{
kaniko.CacheFlag,
fmt.Sprintf("%s=%t", kaniko.CacheRunLayersFlag, false),
},
},
{
description: "with Cache Run Layers is true",
artifact: &latest.KanikoArtifact{
DockerfilePath: "Dockerfile",
Cache: &latest.KanikoCache{CacheRunLayers: boolPtr(true)},
},
expectedArgs: []string{
kaniko.CacheFlag,
fmt.Sprintf("%s=%t", kaniko.CacheRunLayersFlag, true),
},
},
{
description: "with Cleanup",
artifact: &latest.KanikoArtifact{
Expand Down Expand Up @@ -482,6 +505,10 @@ func TestKanikoBuildSpec(t *testing.T) {
}
}

func boolPtr(b bool) *bool {
return &b
}

type mockArtifactStore map[string]string

func (m mockArtifactStore) GetImageTag(imageName string) (string, bool) { return m[imageName], true }
Expand Down
3 changes: 3 additions & 0 deletions pkg/skaffold/build/kaniko/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func Args(artifact *latest.KanikoArtifact, tag, context string) ([]string, error
if artifact.Cache.CacheCopyLayers {
args = append(args, CacheCopyLayersFlag)
}
if artifact.Cache.CacheRunLayers != nil {
args = append(args, fmt.Sprintf("%s=%t", CacheRunLayersFlag, *artifact.Cache.CacheRunLayers))
}
}

if artifact.Target != "" {
Expand Down
2 changes: 2 additions & 0 deletions pkg/skaffold/build/kaniko/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
CacheFlag = "--cache"
// CacheCopyLayersFlag additional flag
CacheCopyLayersFlag = "--cache-copy-layers"
// CacheRunLayersFlag additional flag
CacheRunLayersFlag = "--cache-run-layers"
// CacheDirFlag additional flag
CacheDirFlag = "--cache-dir"
// CacheRepoFlag additional flag
Expand Down
10 changes: 10 additions & 0 deletions pkg/skaffold/schema/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ func setKanikoArtifactDefaults(a *latest.KanikoArtifact) {
a.DockerfilePath = valueOrDefault(a.DockerfilePath, constants.DefaultDockerfilePath)
a.InitImage = valueOrDefault(a.InitImage, constants.DefaultBusyboxImage)
a.DigestFile = valueOrDefault(a.DigestFile, constants.DefaultKanikoDigestFile)
if a.Cache != nil {
a.Cache.CacheRunLayers = valueOrDefaultBool(a.Cache.CacheRunLayers, true)
}
a.CopyMaxRetries = valueOrDefaultInt(a.CopyMaxRetries, kaniko.DefaultCopyMaxRetries)
a.CopyTimeout = valueOrDefault(a.CopyTimeout, kaniko.DefaultCopyTimeout)
a.BuildContextCompressionLevel = valueOrDefaultInt(a.BuildContextCompressionLevel, kaniko.DefaultBuildContextCompressionLevel)
Expand All @@ -381,6 +384,13 @@ func valueOrDefaultInt(v *int, def int) *int {
return &def
}

func valueOrDefaultBool(v *bool, def bool) *bool {
if v != nil {
return v
}
return &def
}

func currentNamespace() (string, error) {
cfg, err := kubectx.CurrentConfig()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ type KanikoCache struct {
TTL string `yaml:"ttl,omitempty"`
// CacheCopyLayers enables caching of copy layers.
CacheCopyLayers bool `yaml:"cacheCopyLayers,omitempty"`
// CacheRunLayers enables caching of run layers (default=true).
CacheRunLayers *bool `yaml:"cacheRunLayers,omitempty"`
}

// ClusterDetails *beta* describes how to do an on-cluster build.
Expand Down

0 comments on commit f4e1501

Please sign in to comment.