Skip to content

Commit

Permalink
Rework cache key generation a bit.
Browse files Browse the repository at this point in the history
Cache keys are now based on the previous commands, rather than the previous state
of the filesystem.
  • Loading branch information
dlorenc committed Oct 2, 2018
1 parent e4e6fb6 commit e57778e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 22 deletions.
39 changes: 17 additions & 22 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package executor

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -85,23 +84,6 @@ func newStageBuilder(opts *config.KanikoOptions, stage config.KanikoStage) (*sta
}, nil
}

// key will return a string representation of the build at the cmd
func (s *stageBuilder) key(cmd string) (string, error) {
fsKey, err := s.snapshotter.Key()
if err != nil {
return "", err
}
c := bytes.NewBuffer([]byte{})
enc := json.NewEncoder(c)
enc.Encode(s.cf)
cf, err := util.SHA256(c)
if err != nil {
return "", err
}
logrus.Debugf("%s\n%s\n%s\n%s\n", s.baseImageDigest, fsKey, cf, cmd)
return util.SHA256(bytes.NewReader([]byte(s.baseImageDigest + fsKey + cf + cmd)))
}

// extractCachedLayer will extract the cached layer and append it to the config file
func (s *stageBuilder) extractCachedLayer(layer v1.Image, createdBy string) error {
logrus.Infof("Found cached layer, extracting to filesystem")
Expand Down Expand Up @@ -139,6 +121,14 @@ func (s *stageBuilder) build(opts *config.KanikoOptions) error {
return err
}
var volumes []string

// Set the initial cache key to be the base image digest, the build args and the SrcContext.
compositeKey := NewCompositeCache(s.baseImageDigest)
if err := compositeKey.AddDir(opts.SrcContext); err != nil {
return err
}
compositeKey.AddKey(opts.BuildArgs...)

args := dockerfile.NewBuildArgs(opts.BuildArgs)
for index, cmd := range s.stage.Commands {
finalCmd := index == len(s.stage.Commands)-1
Expand All @@ -149,13 +139,18 @@ func (s *stageBuilder) build(opts *config.KanikoOptions) error {
if command == nil {
continue
}

// Add the next command to the cache key.
compositeKey.AddKey(command.String())
logrus.Info(command.String())
cacheKey, err := s.key(command.String())

ck, err := compositeKey.Hash()
if err != nil {
return errors.Wrap(err, "getting key")
return err
}

if command.CacheCommand() && opts.Cache {
image, err := cache.RetrieveLayer(opts, cacheKey)
image, err := cache.RetrieveLayer(opts, ck)
if err == nil {
if err := s.extractCachedLayer(image, command.String()); err != nil {
return errors.Wrap(err, "extracting cached layer")
Expand Down Expand Up @@ -222,7 +217,7 @@ func (s *stageBuilder) build(opts *config.KanikoOptions) error {
}
// Push layer to cache now along with new config file
if command.CacheCommand() && opts.Cache {
if err := pushLayerToCache(opts, cacheKey, layer, command.String()); err != nil {
if err := pushLayerToCache(opts, ck, layer, command.String()); err != nil {
return err
}
}
Expand Down
77 changes: 77 additions & 0 deletions pkg/executor/composite_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package executor

import (
"crypto/sha256"
"os"
"path/filepath"
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/util"
)

// NewCompositeCache returns an initialized composite cache object.
func NewCompositeCache(initial ...string) *CompositeCache {
c := CompositeCache{
keys: initial,
}
return &c
}

// CompositeCache is a type that generates a cache key from a series of keys.
type CompositeCache struct {
keys []string
}

// AddKey adds the specified key to the sequence.
func (s *CompositeCache) AddKey(k ...string) {
s.keys = append(s.keys, k...)
}

// AddDir adds the contents of a directory to the composite key.
func (s *CompositeCache) AddDir(p string) error {
sha := sha256.New()
if err := filepath.Walk(p, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
fileHash, err := util.CacheHasher()(path)
if err != nil {
return err
}
if _, err := sha.Write([]byte(fileHash)); err != nil {
return err
}
return nil
}); err != nil {
return err
}

s.AddKey(string(sha.Sum(nil)))
return nil
}

// Key returns the human readable composite key as a string.
func (s *CompositeCache) Key() string {
return strings.Join(s.keys, "-")
}

// Hash returns the composite key in a string SHA256 format.
func (s *CompositeCache) Hash() (string, error) {
return util.SHA256(strings.NewReader(s.Key()))
}

0 comments on commit e57778e

Please sign in to comment.