Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kaniko): Add kaniko cache run layers flag #9465

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2934,6 +2934,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 @@ -2954,7 +2959,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)
}
Expand All @@ -380,6 +383,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
Loading