Skip to content

Commit

Permalink
Option to mount HostPath in each Kaniko Pod to be used as cache volume
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhoefling committed Feb 22, 2019
1 parent 0e175e2 commit 4c933f4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/annotated-skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ build:
# localDir: {}
# cache:
# repo: gcr.io/my-project/skaffold/cache
# hostPath: host path used as base image cache to mount in each kaniko container.
# If set, must exist on each node and prepopulated with kaniko-warmer
# flags:
# - --aditional-flag
# pullSecret: /a/secret/path/serviceaccount.json
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Artifact struct {
}

// Builder is an interface to the Build API of Skaffold.
// It must build and make the resulting image accesible to the cluster.
// It must build and make the resulting image accessible to the cluster.
// This could include pushing to a authorized repository or loading the nodes with the image.
// If artifacts is supplied, the builder should only rebuild those artifacts.
type Builder interface {
Expand Down
4 changes: 4 additions & 0 deletions pkg/skaffold/build/kaniko/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/kaniko/sources"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
Expand Down Expand Up @@ -62,6 +63,9 @@ func (b *Builder) run(ctx context.Context, out io.Writer, artifact *latest.Artif
if b.Cache.Repo != "" {
args = append(args, fmt.Sprintf("--cache-repo=%s", b.Cache.Repo))
}
if b.Cache.HostPath != "" {
args = append(args, fmt.Sprintf("--cache-dir=%s", constants.DefaultKanikoDockerConfigPath))
}
}

podSpec := s.Pod(args)
Expand Down
19 changes: 19 additions & 0 deletions pkg/skaffold/build/kaniko/sources/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ func podTemplate(cfg *latest.KanikoBuild, args []string) *v1.Pod {
},
}

if cfg.Cache.HostPath != "" {
volumeMount := v1.VolumeMount{
Name: constants.DefaultKanikoCacheDirName,
MountPath: constants.DefaultKanikoCacheDirMountPath,
}

pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, volumeMount)

volume := v1.Volume{
Name: constants.DefaultKanikoCacheDirName,
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: cfg.Cache.HostPath,
},
},
}
pod.Spec.Volumes = append(pod.Spec.Volumes, volume)
}

if cfg.DockerConfig == nil {
return pod
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (
DefaultKanikoContainerName = "kaniko"
DefaultKanikoEmptyDirName = "kaniko-emptydir"
DefaultKanikoEmptyDirMountPath = "/kaniko/buildcontext"
DefaultKanikoCacheDirName = "kaniko-cache"
DefaultKanikoCacheDirMountPath = "/cache"
DefaultKanikoDockerConfigSecretName = "docker-cfg"
DefaultKanikoDockerConfigPath = "/kaniko/.docker"

Expand Down
3 changes: 3 additions & 0 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ type KanikoCache struct {
// Repo is a remote repository to store cached layers. If none is specified, one will be
// inferred from the image name. See [Kaniko Caching](https://github.com/GoogleContainerTools/kaniko#caching).
Repo string `yaml:"repo,omitempty"`

// HostPath specifies a path on the host that is mounted to each pod as read only cache volume containing base images.
HostPath string `yaml:"hostPath,omitempty"`
}

// KanikoBuild (beta) describes how to do an on-cluster build using
Expand Down

0 comments on commit 4c933f4

Please sign in to comment.