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 Sep 29, 2018
1 parent 139d372 commit dc545e9
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit dc545e9

Please sign in to comment.