diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 5e10d16b54..54a05ae08b 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -18,13 +18,14 @@ package executor import ( "bytes" - "encoding/json" + "crypto/sha256" "fmt" "io" "io/ioutil" "os" "path/filepath" "strconv" + "strings" "time" "github.com/google/go-containerregistry/pkg/name" @@ -85,23 +86,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") @@ -129,6 +113,27 @@ func (s *stageBuilder) extractCachedLayer(layer v1.Image, createdBy string) erro return err } +func cacheKey(chainID string) (string, error) { + return util.SHA256(strings.NewReader(chainID)) +} + +func hashDir(dir string) (string, error) { + sha := sha256.New() + if err := filepath.Walk(dir, 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 + } + return string(sha.Sum(nil)), nil +} + func (s *stageBuilder) build(opts *config.KanikoOptions) error { // Unpack file system to root if _, err := util.GetFSFromImage(constants.RootDir, s.image); err != nil { @@ -138,7 +143,16 @@ func (s *stageBuilder) build(opts *config.KanikoOptions) error { if err := s.snapshotter.Init(); err != nil { return err } + args := dockerfile.NewBuildArgs(opts.BuildArgs) + + // Set the initial chainId to be the base image digest, the build args and the SrcContext. + srcContextHash, err := hashDir(opts.SrcContext) + if err != nil { + return err + } + chainID := s.baseImageDigest + "-" + strings.Join([]string(opts.BuildArgs), ",") + "-" + srcContextHash + for index, cmd := range s.stage.Commands { finalCmd := index == len(s.stage.Commands)-1 command, err := commands.GetCommand(cmd, opts.SrcContext) @@ -148,13 +162,16 @@ func (s *stageBuilder) build(opts *config.KanikoOptions) error { if command == nil { continue } + + // Add the next command to the cache key. + chainID += "-" + command.String() logrus.Info(command.String()) - cacheKey, err := s.key(command.String()) + ck, err := cacheKey(chainID) if err != nil { return errors.Wrap(err, "getting key") } 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 +230,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 } }