Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit c718dc6

Browse files
authored
Merge pull request #1114 from tejal29/fix_copy
Add more tests for Copy and some fixes.
2 parents 861c039 + 9592f26 commit c718dc6

12 files changed

Lines changed: 516 additions & 56 deletions

File tree

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
FROM busybox as t
2-
RUN echo "hello" > /tmp/target
3-
RUN ln -s /tmp/target /tmp/link
4-
5-
## Relative link
6-
RUN cd tmp && ln -s target relative_link
7-
2+
RUN mkdir temp
3+
RUN echo "hello" > temp/target
4+
RUN ln -s target temp/link
85
## Relative link with paths
9-
RUN mkdir workdir && cd workdir && ln -s ../tmp/target relative_dir_link
6+
RUN mkdir workdir && cd workdir && ln -s ../temp/target relative_link
107

118
FROM scratch
12-
COPY --from=t /tmp/link /tmp/
13-
COPY --from=t /tmp/relative_link /tmp/
14-
COPY --from=t /workdir/relative_dir_link /workdir/
9+
COPY --from=t temp/ dest/
10+
COPY --from=t /workdir/relative_link /workdirAnother/

pkg/commands/add.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
v1 "github.com/google/go-containerregistry/pkg/v1"
2323
"github.com/moby/buildkit/frontend/dockerfile/instructions"
24+
"github.com/pkg/errors"
2425

2526
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
2627

@@ -66,18 +67,18 @@ func (a *AddCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
6667
}
6768
logrus.Infof("Adding remote URL %s to %s", src, urlDest)
6869
if err := util.DownloadFileToDest(src, urlDest); err != nil {
69-
return err
70+
return errors.Wrap(err, "downloading remote source file")
7071
}
7172
a.snapshotFiles = append(a.snapshotFiles, urlDest)
7273
} else if util.IsFileLocalTarArchive(fullPath) {
7374
tarDest, err := util.DestinationFilepath("", dest, config.WorkingDir)
7475
if err != nil {
75-
return err
76+
return errors.Wrap(err, "determining dest for tar")
7677
}
7778
logrus.Infof("Unpacking local tar archive %s to %s", src, tarDest)
7879
extractedFiles, err := util.UnpackLocalTarArchive(fullPath, tarDest)
7980
if err != nil {
80-
return err
81+
return errors.Wrap(err, "unpacking local tar")
8182
}
8283
logrus.Debugf("Added %v from local tar archive %s", extractedFiles, src)
8384
a.snapshotFiles = append(a.snapshotFiles, extractedFiles...)
@@ -98,7 +99,7 @@ func (a *AddCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
9899
}
99100

100101
if err := copyCmd.ExecuteCommand(config, buildArgs); err != nil {
101-
return err
102+
return errors.Wrap(err, "executing copy command")
102103
}
103104
a.snapshotFiles = append(a.snapshotFiles, copyCmd.snapshotFiles...)
104105
return nil

pkg/commands/copy.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
5454

5555
uid, gid, err := getUserGroup(c.cmd.Chown, replacementEnvs)
5656
if err != nil {
57-
return err
57+
return errors.Wrap(err, "getting user group from chowm")
5858
}
5959

6060
srcs, dest, err := util.ResolveEnvAndWildcards(c.cmd.SourcesAndDest, c.buildcontext, replacementEnvs)
6161
if err != nil {
62-
return err
62+
return errors.Wrap(err, "resolving src")
6363
}
6464

6565
// For each source, iterate through and copy it over
6666
for _, src := range srcs {
6767
fullPath := filepath.Join(c.buildcontext, src)
68+
6869
fi, err := os.Lstat(fullPath)
6970
if err != nil {
7071
return errors.Wrap(err, "could not copy source")
@@ -79,27 +80,27 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
7980

8081
destPath, err := util.DestinationFilepath(fullPath, dest, cwd)
8182
if err != nil {
82-
return err
83+
return errors.Wrap(err, "find destination path")
8384
}
8485

8586
// If the destination dir is a symlink we need to resolve the path and use
8687
// that instead of the symlink path
8788
destPath, err = resolveIfSymlink(destPath)
8889
if err != nil {
89-
return err
90+
return errors.Wrap(err, "resolving dest symlink")
9091
}
9192

9293
if fi.IsDir() {
9394
copiedFiles, err := util.CopyDir(fullPath, destPath, c.buildcontext, uid, gid)
9495
if err != nil {
95-
return err
96+
return errors.Wrap(err, "copying dir")
9697
}
9798
c.snapshotFiles = append(c.snapshotFiles, copiedFiles...)
9899
} else if util.IsSymlink(fi) {
99100
// If file is a symlink, we want to copy the target file to destPath
100-
exclude, err := util.CopySymlink(fullPath, destPath, c.buildcontext, uid, gid)
101+
exclude, err := util.CopySymlink(fullPath, destPath, c.buildcontext)
101102
if err != nil {
102-
return err
103+
return errors.Wrap(err, "copying symlink")
103104
}
104105
if exclude {
105106
continue
@@ -109,7 +110,7 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
109110
// ... Else, we want to copy over a file
110111
exclude, err := util.CopyFile(fullPath, destPath, c.buildcontext, uid, gid)
111112
if err != nil {
112-
return err
113+
return errors.Wrap(err, "copying file")
113114
}
114115
if exclude {
115116
continue

0 commit comments

Comments
 (0)