Skip to content

Commit

Permalink
Snapshot only specific files for COPY
Browse files Browse the repository at this point in the history
Before GoogleContainerTools#289 was merged, when copying over directories for COPY kaniko
would get a list of all files at the destination specified and add them
to the list of files to be snapshotted. If the destination was root it
would add all files. This worked because the snapshotter made sure the
file had been changed before adding it to the layer.

After GoogleContainerTools#289, we changed the logic to add all files snapshotted to a layer
without checking if the files had been changed. This created the bug in
got all the files at root and added them to the layer without checking
if they had been changed.

This change should fix this bug. Now, the CopyDir function returns a
list of files it copied over and only those files are added to the list
of files to be snapshotted.

Should fix GoogleContainerTools#314
  • Loading branch information
Priya Wadhwa committed Aug 27, 2018
1 parent 216b14f commit 9a93f5b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
5 changes: 1 addition & 4 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
// we need to add '/' to the end to indicate the destination is a directory
dest = filepath.Join(cwd, dest) + "/"
}
if err := util.CopyDir(fullPath, dest); err != nil {
return err
}
copiedFiles, err := util.Files(dest)
copiedFiles, err := util.CopyDir(fullPath, dest)
if err != nil {
return err
}
Expand Down
19 changes: 11 additions & 8 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,18 @@ func DownloadFileToDest(rawurl, dest string) error {
}

// CopyDir copies the file or directory at src to dest
func CopyDir(src, dest string) error {
// It returns a list of files it copied over
func CopyDir(src, dest string) ([]string, error) {
files, err := RelativeFiles("", src)
if err != nil {
return err
return nil, err
}
var copiedFiles []string
for _, file := range files {
fullPath := filepath.Join(src, file)
fi, err := os.Lstat(fullPath)
if err != nil {
return err
return nil, err
}
destPath := filepath.Join(dest, file)
if fi.IsDir() {
Expand All @@ -478,24 +480,25 @@ func CopyDir(src, dest string) error {
gid := int(fi.Sys().(*syscall.Stat_t).Gid)

if err := os.MkdirAll(destPath, fi.Mode()); err != nil {
return err
return nil, err
}
if err := os.Chown(destPath, uid, gid); err != nil {
return err
return nil, err
}
} else if fi.Mode()&os.ModeSymlink != 0 {
// If file is a symlink, we want to create the same relative symlink
if err := CopySymlink(fullPath, destPath); err != nil {
return err
return nil, err
}
} else {
// ... Else, we want to copy over a file
if err := CopyFile(fullPath, destPath); err != nil {
return err
return nil, err
}
}
copiedFiles = append(copiedFiles, destPath)
}
return nil
return copiedFiles, nil
}

// CopySymlink copies the symlink at src to dest
Expand Down

0 comments on commit 9a93f5b

Please sign in to comment.