Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Symlinks not being copied across stages #971

Merged
merged 7 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions integration/dockerfiles/Dockerfile_test_copy_symlink
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM busybox as t
RUN echo "hello" > /tmp/target
RUN ln -s /tmp/target /tmp/link

FROM scratch
COPY --from=t /tmp/link /tmp
2 changes: 1 addition & 1 deletion pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
return err
}
c.snapshotFiles = append(c.snapshotFiles, copiedFiles...)
} else if fi.Mode()&os.ModeSymlink != 0 {
} else if util.IsSymlink(fi) {
// If file is a symlink, we want to create the same relative symlink
exclude, err := util.CopySymlink(fullPath, destPath, c.buildcontext)
if err != nil {
Expand Down
19 changes: 12 additions & 7 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"strconv"
"time"

otiai10Cpy "github.com/otiai10/copy"

"github.com/google/go-containerregistry/pkg/v1/partial"

"github.com/moby/buildkit/frontend/dockerfile/instructions"
Expand Down Expand Up @@ -574,8 +572,8 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
return nil, err
}
for _, p := range filesToSave {
logrus.Infof("Saving file %s for later use.", p)
otiai10Cpy.Copy(p, filepath.Join(dstDir, p))
logrus.Infof("Saving file %s for later use", p)
util.CopyFileOrSymlink(p, dstDir)
}

// Delete the filesystem
Expand All @@ -587,16 +585,23 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
return nil, err
}

// fileToSave returns all the files matching the given pattern in deps.
// If a file is a symlink, it also returns the target file.
func filesToSave(deps []string) ([]string, error) {
allFiles := []string{}
srcFiles := []string{}
for _, src := range deps {
srcs, err := filepath.Glob(src)
if err != nil {
return nil, err
}
allFiles = append(allFiles, srcs...)
for _, f := range srcs {
if link, err := util.EvalSymLink(f); err == nil {
srcFiles = append(srcFiles, link)
}
srcFiles = append(srcFiles, f)
}
}
return allFiles, nil
return srcFiles, nil
}

func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) error {
Expand Down
31 changes: 26 additions & 5 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package snapshot
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"syscall"
Expand Down Expand Up @@ -112,7 +113,6 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
if err := writeToTar(t, filesToAdd, filesToWhiteOut); err != nil {
return "", err
}

return f.Name(), nil
}

Expand Down Expand Up @@ -179,23 +179,26 @@ func (s *Snapshotter) scanFullFilesystem() ([]string, []string, error) {
return nil, nil, fmt.Errorf("could not check if file has changed %s %s", path, err)
}
if fileChanged {
logrus.Tracef("Adding %s to layer, because it was changed.", path)
filesToAdd = append(filesToAdd, path)
// Get target file for symlinks so the symlink is not a dead link.
files, err := filesWithLinks(path)
if err != nil {
return nil, nil, err
}
logrus.Tracef("Adding files %s to layer, because it was changed.", files)
filesToAdd = append(filesToAdd, files...)
tejal29 marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Also add parent directories to keep their permissions correctly.
filesToAdd = filesWithParentDirs(filesToAdd)

sort.Strings(filesToAdd)

// Add files to the layered map
for _, file := range filesToAdd {
if err := s.l.Add(file); err != nil {
return nil, nil, fmt.Errorf("unable to add file %s to layered map: %s", file, err)
}
}

return filesToAdd, filesToWhiteOut, nil
}

Expand Down Expand Up @@ -236,3 +239,21 @@ func filesWithParentDirs(files []string) []string {

return newFiles
}

// filesWithLinks returns the symlink and the target path if its exists.
func filesWithLinks(path string) ([]string, error) {
link, err := util.GetSymLink(path)
if err == util.ErrNotSymLink {
return []string{path}, nil
} else if err != nil {
return nil, err
}
// Add symlink if it exists in the FS
if !filepath.IsAbs(link) {
link = filepath.Join(filepath.Dir(path), link)
}
if _, err := os.Stat(link); err != nil {
return []string{path}, nil
}
return []string{path, link}, nil
}
105 changes: 90 additions & 15 deletions pkg/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func TestSnapshotFSFileChange(t *testing.T) {
testDir, snapshotter, cleanup, err := setUpTestDir()
testDir, snapshotter, cleanup, err := setUpTest()
testDirWithoutLeadingSlash := strings.TrimLeft(testDir, "/")
defer cleanup()
if err != nil {
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestSnapshotFSFileChange(t *testing.T) {
}

func TestSnapshotFSIsReproducible(t *testing.T) {
testDir, snapshotter, cleanup, err := setUpTestDir()
testDir, snapshotter, cleanup, err := setUpTest()
defer cleanup()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestSnapshotFSIsReproducible(t *testing.T) {
}

func TestSnapshotFSChangePermissions(t *testing.T) {
testDir, snapshotter, cleanup, err := setUpTestDir()
testDir, snapshotter, cleanup, err := setUpTest()
testDirWithoutLeadingSlash := strings.TrimLeft(testDir, "/")
defer cleanup()
if err != nil {
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestSnapshotFSChangePermissions(t *testing.T) {
}

func TestSnapshotFiles(t *testing.T) {
testDir, snapshotter, cleanup, err := setUpTestDir()
testDir, snapshotter, cleanup, err := setUpTest()
testDirWithoutLeadingSlash := strings.TrimLeft(testDir, "/")
defer cleanup()
if err != nil {
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestSnapshotFiles(t *testing.T) {
}

func TestEmptySnapshotFS(t *testing.T) {
_, snapshotter, cleanup, err := setUpTestDir()
_, snapshotter, cleanup, err := setUpTest()
if err != nil {
t.Fatal(err)
}
Expand All @@ -253,29 +253,104 @@ func TestEmptySnapshotFS(t *testing.T) {
}
}

func setUpTestDir() (string, *Snapshotter, func(), error) {
testDir, err := ioutil.TempDir("", "")
if err != nil {
return "", nil, nil, errors.Wrap(err, "setting up temp dir")
func TestFileWithLinks(t *testing.T) {

link := "baz/link"
tcs := []struct {
name string
path string
linkFileTarget string
expected []string
shouldErr bool
}{
{
name: "given path is a symlink that points to a valid target",
path: link,
linkFileTarget: "file",
expected: []string{link, "baz/file"},
},
{
name: "given path is a symlink points to non existing path",
path: link,
linkFileTarget: "does-not-exists",
expected: []string{link},
},
{
name: "given path is a regular file",
path: "kaniko/file",
linkFileTarget: "file",
expected: []string{"kaniko/file"},
},
}

snapshotPath, err := ioutil.TempDir("", "")
if err != nil {
return "", nil, nil, errors.Wrap(err, "setting up temp dir")
for _, tt := range tcs {
t.Run(tt.name, func(t *testing.T) {
testDir, cleanup, err := setUpTestDir()
if err != nil {
t.Fatal(err)
}
defer cleanup()
if err := setupSymlink(testDir, link, tt.linkFileTarget); err != nil {
t.Fatalf("could not set up symlink due to %s", err)
}
actual, err := filesWithLinks(filepath.Join(testDir, tt.path))
if err != nil {
t.Fatalf("unexpected error %s", err)
}
sortAndCompareFilepaths(t, testDir, tt.expected, actual)
})
}
}

snapshotPathPrefix = snapshotPath
func setupSymlink(dir string, link string, target string) error {
return os.Symlink(target, filepath.Join(dir, link))
}

func sortAndCompareFilepaths(t *testing.T, testDir string, expected []string, actual []string) {
expectedFullPaths := make([]string, len(expected))
for i, file := range expected {
expectedFullPaths[i] = filepath.Join(testDir, file)
}
sort.Strings(expectedFullPaths)
sort.Strings(actual)
testutil.CheckDeepEqual(t, expectedFullPaths, actual)
}

func setUpTestDir() (string, func(), error) {
testDir, err := ioutil.TempDir("", "")
if err != nil {
return "", nil, errors.Wrap(err, "setting up temp dir")
}
files := map[string]string{
"foo": "baz1",
"bar/bat": "baz2",
"kaniko/file": "file",
"baz/file": "testfile",
}
// Set up initial files
if err := testutil.SetupFiles(testDir, files); err != nil {
return "", nil, nil, errors.Wrap(err, "setting up file system")
return "", nil, errors.Wrap(err, "setting up file system")
}

cleanup := func() {
os.RemoveAll(testDir)
}

return testDir, cleanup, nil
}

func setUpTest() (string, *Snapshotter, func(), error) {
testDir, dirCleanUp, err := setUpTestDir()
if err != nil {
return "", nil, nil, err
}
snapshotPath, err := ioutil.TempDir("", "")
if err != nil {
return "", nil, nil, errors.Wrap(err, "setting up temp dir")
}

snapshotPathPrefix = snapshotPath

// Take the initial snapshot
l := NewLayeredMap(util.Hasher(), util.CacheHasher())
snapshotter := NewSnapshotter(l, testDir)
Expand All @@ -285,7 +360,7 @@ func setUpTestDir() (string, *Snapshotter, func(), error) {

cleanup := func() {
os.RemoveAll(snapshotPath)
os.RemoveAll(testDir)
dirCleanUp()
}

return testDir, snapshotter, cleanup, nil
Expand Down
Loading