diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 5e10d16b54..9931cae572 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -18,7 +18,6 @@ package executor import ( "bytes" - "encoding/json" "fmt" "io" "io/ioutil" @@ -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") @@ -138,6 +120,14 @@ func (s *stageBuilder) build(opts *config.KanikoOptions) error { if err := s.snapshotter.Init(); err != nil { return err } + + // 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 @@ -148,13 +138,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") @@ -213,7 +208,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 } } diff --git a/pkg/executor/composite_cache.go b/pkg/executor/composite_cache.go new file mode 100644 index 0000000000..a944eb5591 --- /dev/null +++ b/pkg/executor/composite_cache.go @@ -0,0 +1,74 @@ +/* +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 { + 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())) +}