Fix caching to respect .dockerignore

Previously kaniko would compute the cache key for any copy command by computing
the combined hash of all files in a directory, even if they were listed
as ignored.

With this change, the cache key creation was updated to be aware of ignored
files.

Related issues:
* https://github.com/GoogleContainerTools/kaniko/issues/594
This commit is contained in:
Moritz Wanzenböck
2020-01-23 15:37:37 -08:00
committed by Tejal Desai
parent a2aae6274d
commit 123dcaf83e
5 changed files with 27 additions and 13 deletions
+3 -1
View File
@@ -158,8 +158,10 @@ func (s *stageBuilder) populateCompositeKey(command fmt.Stringer, files []string
compositeKey = s.populateCopyCmdCompositeKey(command, v.From(), compositeKey)
}
srcCtx := s.opts.SrcContext
for _, f := range files {
if err := compositeKey.AddPath(f); err != nil {
if err := compositeKey.AddPath(f, srcCtx); err != nil {
return compositeKey, err
}
}
+15 -3
View File
@@ -54,14 +54,18 @@ func (s *CompositeCache) Hash() (string, error) {
return util.SHA256(strings.NewReader(s.Key()))
}
func (s *CompositeCache) AddPath(p string) error {
func (s *CompositeCache) AddPath(p, context string) error {
sha := sha256.New()
fi, err := os.Lstat(p)
if err != nil {
return err
}
if util.ExcludeFile(p, context) {
return os.ErrNotExist
}
if fi.Mode().IsDir() {
k, err := HashDir(p)
k, err := HashDir(p, context)
if err != nil {
return err
}
@@ -81,12 +85,20 @@ func (s *CompositeCache) AddPath(p string) error {
}
// HashDir returns a hash of the directory.
func HashDir(p string) (string, error) {
func HashDir(p, context string) (string, error) {
sha := sha256.New()
if err := filepath.Walk(p, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
exclude := util.ExcludeFile(path, context)
if fi.IsDir() && exclude {
return filepath.SkipDir
}
if exclude {
return nil
}
fileHash, err := util.CacheHasher()(path)
if err != nil {
return err