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

Skip the /kaniko directory when copying root #2863

Merged
merged 1 commit into from
Nov 29, 2023
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
5 changes: 5 additions & 0 deletions integration/dockerfiles/Dockerfile_test_copy_root_multistage
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM busybox:1.31
COPY context/foo foo

FROM alpine@sha256:5ce5f501c457015c4b91f91a15ac69157d9b06f1a75cf9107bf2b62e0843983a
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have full context, but could you help explain how does this dockerfile tests the logic change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is the release note related to the PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have full context, but could you help explain how does this dockerfile tests the logic change?

Prior to this change, when the source "/" is specified, we will see errors described at #2033 (comment) with recursive /kaniko/0 in the names. This test case test copying from root and the kaniko/0 folder shall not be apparent.

I also added a sentence for the previous situation in the message. Thanks for pointing this out.

COPY / /foo
35 changes: 35 additions & 0 deletions pkg/executor/copy_multistage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ COPY --from=first copied/bam.txt output/`
testutil.CheckDeepEqual(t, 1, len(files))
testutil.CheckDeepEqual(t, files[0].Name(), "bam.txt")
})

t.Run("copy directory across multistage into a directory", func(t *testing.T) {
testDir, fn := setupMultistageTests(t)
defer fn()
Expand Down Expand Up @@ -117,6 +118,40 @@ COPY --from=first copied another`
//testutil.CheckDeepEqual(t, linkName, "bam.txt")
})

t.Run("copy root across multistage", func(t *testing.T) {
testDir, fn := setupMultistageTests(t)
defer fn()
dockerFile := `
FROM scratch as first
COPY foo copied
ENV test test
From scratch as second
COPY --from=first / output/`
ioutil.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
opts := &config.KanikoOptions{
DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"),
SrcContext: filepath.Join(testDir, "workspace"),
SnapshotMode: constants.SnapshotModeFull,
}
_, err := DoBuild(opts)
testutil.CheckNoError(t, err)

filesUnderRoot, err := ioutil.ReadDir(filepath.Join(testDir, "output/"))
if err != nil {
t.Fatal(err)
}
testutil.CheckDeepEqual(t, 3, len(filesUnderRoot))

files, err := ioutil.ReadDir(filepath.Join(testDir, "output/workspace/foo"))
if err != nil {
t.Fatal(err)
}
testutil.CheckDeepEqual(t, 2, len(files))
testutil.CheckDeepEqual(t, "bam.link", files[0].Name())
testutil.CheckDeepEqual(t, "bam.txt", files[1].Name())
})

}

func setupMultistageTests(t *testing.T) (string, func()) {
Expand Down
9 changes: 8 additions & 1 deletion pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ var ignorelist = append([]IgnoreListEntry{}, defaultIgnoreList...)

var volumes = []string{}

// skipKanikoDir opts to skip the '/kaniko' dir for otiai10.copy which should be ignored in root
var skipKanikoDir = otiai10Cpy.Options{
Skip: func(info os.FileInfo, src, dest string) (bool, error) {
return strings.HasSuffix(src, "/kaniko"), nil
},
}

type FileContext struct {
Root string
ExcludedFiles []string
Expand Down Expand Up @@ -956,7 +963,7 @@ func CopyFileOrSymlink(src string, destDir string, root string) error {
}
return os.Symlink(link, destFile)
}
if err := otiai10Cpy.Copy(src, destFile); err != nil {
if err := otiai10Cpy.Copy(src, destFile, skipKanikoDir); err != nil {
return errors.Wrap(err, "copying file")
}
if err := CopyOwnership(src, destDir, root); err != nil {
Expand Down
Loading