From 9d67953ed333e6c1c9afbced5d30b41e85f269fc Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Fri, 16 Nov 2018 16:05:43 -0800 Subject: [PATCH 1/4] Fix bug in extracting hardlinks When we execute multistage builds, we store the fs of each intermediate stage at /kaniko/ if it's used later in the build. This created a bug when extracting hardlinks, because we weren't appending the new directory to the link path. So, if `/tmp/file1` and `/tmp/file2` were hardlinked, kaniko was trying to link `/kaniko/0/tmp/file1` to `/tmp/file2` instead of `/kaniko/0/tmp/file2`. This change will append the correct directory to the link, and fixes #437 #362 #352 #342. --- integration/dockerfiles/Dockerfile_test_hardlink | 10 ++++++++++ pkg/util/fs_util.go | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/integration/dockerfiles/Dockerfile_test_hardlink b/integration/dockerfiles/Dockerfile_test_hardlink index fd2968ee8..e51159dfb 100644 --- a/integration/dockerfiles/Dockerfile_test_hardlink +++ b/integration/dockerfiles/Dockerfile_test_hardlink @@ -1,3 +1,13 @@ +FROM composer@sha256:5c4bd89217b50125f28e08d9f16414ecb75f90ce9b773605472b35cd55f3e5c0 AS composer +FROM php@sha256:9fe20c8003a12f5907ffdc1d7ec435b4ca4226fa4342b94cec4d66189a439f17 +COPY --from=composer /usr/bin/composer /usr/bin/composer +COPY ./ /app +WORKDIR /app +RUN ls -l + +# make sure hardlink extracts correctly +FROM jboss/base-jdk@sha256:138591422fdab93a5844c13f6cbcc685631b37a16503675e9f340d2503617a41 + FROM gcr.io/kaniko-test/hardlink-base:latest RUN ls -al /usr/libexec/git-core/git /usr/bin/git /usr/libexec/git-core/git-diff RUN stat /usr/bin/git diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 526ac5f50..0f5cdc843 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -245,8 +245,8 @@ func extractFile(dest string, hdr *tar.Header, tr io.Reader) error { return errors.Wrapf(err, "error removing %s to make way for new link", hdr.Name) } } - - if err := os.Link(filepath.Clean(filepath.Join("/", hdr.Linkname)), path); err != nil { + link := filepath.Join(dest, hdr.Linkname) + if err := os.Link(filepath.Clean(filepath.Join("/", link)), path); err != nil { return err } From 2cf6f52517a2e654f95688bb36ca1988a870f513 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Mon, 19 Nov 2018 15:03:31 -0500 Subject: [PATCH 2/4] Fix hardlink unit test Now that hardlink destinations take into account the directory that they are being extracted to, the unit test had to be updated to make sure that two hardlinks were extracted to /tmp/hardlink correctly. --- pkg/util/fs_util_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/util/fs_util_test.go b/pkg/util/fs_util_test.go index 20f80820e..3ca29aeb8 100644 --- a/pkg/util/fs_util_test.go +++ b/pkg/util/fs_util_test.go @@ -367,7 +367,7 @@ func filesAreHardlinks(first, second string) checker { if err != nil { t.Fatalf("error getting file %s", first) } - fi2, err := os.Stat(filepath.Join(second)) + fi2, err := os.Stat(filepath.Join(root, second)) if err != nil { t.Fatalf("error getting file %s", second) } @@ -499,11 +499,11 @@ func TestExtractFile(t *testing.T) { tmpdir: "/tmp/hardlink", hdrs: []*tar.Header{ fileHeader("/bin/gzip", "gzip-binary", 0751), - hardlinkHeader("/bin/uncompress", "/tmp/hardlink/bin/gzip"), + hardlinkHeader("/bin/uncompress", "/bin/gzip"), }, checkers: []checker{ fileExists("/bin/gzip"), - filesAreHardlinks("/bin/uncompress", "/tmp/hardlink/bin/gzip"), + filesAreHardlinks("/bin/uncompress", "/bin/gzip"), }, }, } From edd873d1feb9584b3b96cb5152eb824a8146b68c Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Mon, 19 Nov 2018 15:07:56 -0500 Subject: [PATCH 3/4] remove unnecessary filepath.Join since dest is always an absolute path --- pkg/util/fs_util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 0f5cdc843..ba36461b2 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -245,8 +245,8 @@ func extractFile(dest string, hdr *tar.Header, tr io.Reader) error { return errors.Wrapf(err, "error removing %s to make way for new link", hdr.Name) } } - link := filepath.Join(dest, hdr.Linkname) - if err := os.Link(filepath.Clean(filepath.Join("/", link)), path); err != nil { + link := filepath.Clean(filepath.Join(dest, hdr.Linkname)) + if err := os.Link(link, path); err != nil { return err } From acd45dc894fc5323cecaf20a3cf94c20004b7aa9 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Mon, 19 Nov 2018 15:26:02 -0500 Subject: [PATCH 4/4] fixed digests for integration test --- integration/dockerfiles/Dockerfile_test_hardlink | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/integration/dockerfiles/Dockerfile_test_hardlink b/integration/dockerfiles/Dockerfile_test_hardlink index e51159dfb..66cd7b5ab 100644 --- a/integration/dockerfiles/Dockerfile_test_hardlink +++ b/integration/dockerfiles/Dockerfile_test_hardlink @@ -1,9 +1,6 @@ -FROM composer@sha256:5c4bd89217b50125f28e08d9f16414ecb75f90ce9b773605472b35cd55f3e5c0 AS composer -FROM php@sha256:9fe20c8003a12f5907ffdc1d7ec435b4ca4226fa4342b94cec4d66189a439f17 +FROM composer@sha256:4598feb4b58b4370893a29cbc654afa9420b4debed1d574531514b78a24cd608 AS composer +FROM php@sha256:13813f20fec7ded7bf3a4305ea0ccd4df3cea900e263f7f86c3d5737f86669eb COPY --from=composer /usr/bin/composer /usr/bin/composer -COPY ./ /app -WORKDIR /app -RUN ls -l # make sure hardlink extracts correctly FROM jboss/base-jdk@sha256:138591422fdab93a5844c13f6cbcc685631b37a16503675e9f340d2503617a41