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

Persistent volume caching for base images #383

Merged
merged 21 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fcfc2f4
comments
sharifelgamal Aug 31, 2018
4db3991
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into c…
sharifelgamal Sep 10, 2018
f12eadc
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into c…
sharifelgamal Sep 12, 2018
e915460
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into c…
sharifelgamal Sep 27, 2018
8dd6d47
initial commit for persisent volume caching
sharifelgamal Oct 2, 2018
fc1d3e1
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into c…
sharifelgamal Oct 2, 2018
979092e
cache warmer works
sharifelgamal Oct 3, 2018
1667b39
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into c…
sharifelgamal Oct 3, 2018
ad4f604
general cleanup
sharifelgamal Oct 3, 2018
69a760e
adding some debugging
sharifelgamal Oct 8, 2018
c44bd34
adding missing files
sharifelgamal Oct 8, 2018
123d5f3
Fixing up cache retrieval and cleanup
sharifelgamal Oct 9, 2018
88c6945
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into c…
sharifelgamal Oct 9, 2018
72e71ce
fix tests
sharifelgamal Oct 9, 2018
78c2111
removing auth since we only cache public images
sharifelgamal Oct 9, 2018
b8ac1be
simplifying the caching logic
sharifelgamal Oct 10, 2018
27888f3
fixing logic
sharifelgamal Oct 10, 2018
d158f70
adding volume cache to integration tests. remove auth from cache warm…
sharifelgamal Oct 11, 2018
0a550f0
add building warmer to integration-test
sharifelgamal Oct 11, 2018
47c8ff4
move sample yaml files to examples dir
sharifelgamal Oct 11, 2018
78d4898
small test fix
sharifelgamal Oct 11, 2018
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
1 change: 1 addition & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func addKanikoOptionsFlags(cmd *cobra.Command) {
RootCmd.PersistentFlags().StringVarP(&opts.Target, "target", "", "", "Set the target build stage to build")
RootCmd.PersistentFlags().BoolVarP(&opts.NoPush, "no-push", "", false, "Do not push the image to the registry")
RootCmd.PersistentFlags().StringVarP(&opts.CacheRepo, "cache-repo", "", "", "Specify a repository to use as a cache, otherwise one will be inferred from the destination provided")
RootCmd.PersistentFlags().StringVarP(&opts.CacheDir, "cache-dir", "", "/cache", "Specify a local directory to use as a cache.")
RootCmd.PersistentFlags().BoolVarP(&opts.Cache, "cache", "", false, "Use cache when building image")
RootCmd.PersistentFlags().BoolVarP(&opts.Cleanup, "cleanup", "", false, "Clean the filesystem at the end")
}
Expand Down
4 changes: 3 additions & 1 deletion kaniko-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ spec:
image: gcr.io/kaniko-project/executor:latest
args: ["--dockerfile=<dockerfile>",
"--context=<context>",
"--destination=<destination>"]
"--destination=<destination>",
"--cache",
"--cache-dir=/cache"]
volumeMounts:
- name: kaniko-secret
mountPath: /secret
Expand Down
9 changes: 4 additions & 5 deletions kaniko-warmer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ metadata:
spec:
containers:
- name: kaniko-warmer
image: gcr.io/sharif-test/warmer@sha256:9bf8affcf6630a9b17298e54b79247a22516aebff0c88733f04e6e2974ca58c4
args: ["-c /cache",
"-i gcr.io/google-appengine/debian9",
"-vdebug"]
image: gcr.io/kaniko-project/warmer:latest
args: ["--cache-dir=/cache",
"--image=gcr.io/google-appengine/debian9"]
volumeMounts:
- name: kaniko-secret
mountPath: /secret
Expand All @@ -21,7 +20,7 @@ spec:
volumes:
- name: kaniko-secret
secret:
secretName: default-token-wtlnt
secretName: kaniko-secret
- name: kaniko-cache
persistentVolumeClaim:
claimName: kaniko-cache-claim
Expand Down
16 changes: 4 additions & 12 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ import (

// RetrieveLayer checks the specified cache for a layer with the tag :cacheKey
func RetrieveLayer(opts *config.KanikoOptions, cacheKey string) (v1.Image, error) {
if local, _ := LocalDestination(opts, cacheKey); local != nil {
return local, nil
}

cache, err := Destination(opts, cacheKey)
if err != nil {
return nil, errors.Wrap(err, "getting cache destination")
Expand Down Expand Up @@ -75,21 +71,17 @@ func Destination(opts *config.KanikoOptions, cacheKey string) (string, error) {
return fmt.Sprintf("%s:%s", cache, cacheKey), nil
}

func LocalDestination(opts *config.KanikoOptions, cacheKey string) (v1.Image, error) {
func LocalSource(opts *config.KanikoOptions, cacheKey string, source string) (v1.Image, error) {
cache := opts.CacheDir
if cache == "" {
return nil, nil
}

path := path.Join(cache, cacheKey)
destination := opts.Destinations[0]
destRef, err := name.NewTag(destination, name.WeakValidation)
if err != nil {
return nil, errors.Wrap(err, "volume cache: getting tag for destination")
}
tag, err := name.NewTag(fmt.Sprintf("%s/cache:%s", destRef.Context(), cacheKey), name.WeakValidation)

tag, err := name.NewTag(source, name.WeakValidation)
if err != nil {
return nil, errors.Wrap(err, "volume cache: create new cache tag")
return nil, errors.Wrap(err, "volume cache: creating new cache tag")
}
imgTar, err := tarball.ImageFromPath(path, &tag)
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions pkg/cache/warm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,24 @@ func WarmCache(opts *config.WarmerOptions) error {
for _, image := range images {
cacheRef, err := name.NewTag(image, name.WeakValidation)
if err != nil {
logrus.Debugf("Failed to verify image name %v", err)
errors.Wrap(err, fmt.Sprintf("Failed to verify image name: %s", image))
}
k8sc, err := k8schain.NewNoClient()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, should we use a normal keychain if we're not on k8s?

if err != nil {
logrus.Debugf("Failed to create auth keychain: %v", err)
errors.Wrap(err, fmt.Sprintf("Failed to create auth keychain: %s", image))
}
img, err := remote.Image(cacheRef, remote.WithAuthFromKeychain(k8sc))
if err != nil {
logrus.Debugf("Failed to retrieve image: %v", err)
errors.Wrap(err, fmt.Sprintf("Failed to retrieve image: %s", image))
}

digest, err := img.Digest()
if err != nil {
logrus.Debugf("Failed to retrieve digest: %v", err)
errors.Wrap(err, fmt.Sprintf("Failed to retrieve digest: %s", image))
}
cachePath := path.Join(cacheDir, digest.String())
logrus.Debugf("CACHE PATH = %s\n", cachePath)
err = tarball.WriteToFile(cachePath, cacheRef, img)
if err != nil {
logrus.Debugf("Failed to write to cache %v", err)
errors.Wrap(err, fmt.Sprintf("Failed to write %s to cache", image))
} else {
logrus.Debugf("Wrote %s to cache", image)
Expand Down
22 changes: 18 additions & 4 deletions pkg/util/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package util

import (
"fmt"
"path/filepath"
"strconv"

Expand Down Expand Up @@ -106,7 +105,22 @@ func cachedImage(opts *config.KanikoOptions, image string) (v1.Image, error) {
if err != nil {
return nil, err
}
cacheKey := ref.Name()
fmt.Printf("CACHEKEY=%s", cacheKey)
return cache.LocalDestination(opts, cacheKey)

k8sc, err := k8schain.NewNoClient()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err != nil {
return nil, err
}
kc := authn.NewMultiKeychain(authn.DefaultKeychain, k8sc)

img, err := remote.Image(ref, remote.WithAuthFromKeychain(kc))
if err != nil {
return nil, err
}

cacheKey, err := img.Digest()
if err != nil {
return nil, err
}

return cache.LocalSource(opts, cacheKey.String(), image)
}
6 changes: 3 additions & 3 deletions pkg/util/image_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Test_StandardImage(t *testing.T) {
retrieveRemoteImage = mock
actual, err := RetrieveSourceImage(config.KanikoStage{
Stage: stages[0],
}, nil)
}, nil, nil)
testutil.CheckErrorAndDeepEqual(t, false, err, nil, actual)
}
func Test_ScratchImage(t *testing.T) {
Expand All @@ -67,7 +67,7 @@ func Test_ScratchImage(t *testing.T) {
}
actual, err := RetrieveSourceImage(config.KanikoStage{
Stage: stages[1],
}, nil)
}, nil, nil)
expected := empty.Image
testutil.CheckErrorAndDeepEqual(t, false, err, expected, actual)
}
Expand All @@ -89,7 +89,7 @@ func Test_TarImage(t *testing.T) {
BaseImageStoredLocally: true,
BaseImageIndex: 0,
Stage: stages[2],
}, nil)
}, nil, nil)
testutil.CheckErrorAndDeepEqual(t, false, err, nil, actual)
}

Expand Down