Skip to content

Commit

Permalink
Merge pull request #1114 from tejal29/fix_copy
Browse files Browse the repository at this point in the history
Add more tests for Copy and some fixes.
  • Loading branch information
tejal29 authored Mar 10, 2020
2 parents 861c039 + 9592f26 commit c718dc6
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 56 deletions.
16 changes: 6 additions & 10 deletions integration/dockerfiles/Dockerfile_test_copy_symlink
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
FROM busybox as t
RUN echo "hello" > /tmp/target
RUN ln -s /tmp/target /tmp/link

## Relative link
RUN cd tmp && ln -s target relative_link

RUN mkdir temp
RUN echo "hello" > temp/target
RUN ln -s target temp/link
## Relative link with paths
RUN mkdir workdir && cd workdir && ln -s ../tmp/target relative_dir_link
RUN mkdir workdir && cd workdir && ln -s ../temp/target relative_link

FROM scratch
COPY --from=t /tmp/link /tmp/
COPY --from=t /tmp/relative_link /tmp/
COPY --from=t /workdir/relative_dir_link /workdir/
COPY --from=t temp/ dest/
COPY --from=t /workdir/relative_link /workdirAnother/
9 changes: 5 additions & 4 deletions pkg/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/pkg/errors"

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

Expand Down Expand Up @@ -66,18 +67,18 @@ func (a *AddCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
}
logrus.Infof("Adding remote URL %s to %s", src, urlDest)
if err := util.DownloadFileToDest(src, urlDest); err != nil {
return err
return errors.Wrap(err, "downloading remote source file")
}
a.snapshotFiles = append(a.snapshotFiles, urlDest)
} else if util.IsFileLocalTarArchive(fullPath) {
tarDest, err := util.DestinationFilepath("", dest, config.WorkingDir)
if err != nil {
return err
return errors.Wrap(err, "determining dest for tar")
}
logrus.Infof("Unpacking local tar archive %s to %s", src, tarDest)
extractedFiles, err := util.UnpackLocalTarArchive(fullPath, tarDest)
if err != nil {
return err
return errors.Wrap(err, "unpacking local tar")
}
logrus.Debugf("Added %v from local tar archive %s", extractedFiles, src)
a.snapshotFiles = append(a.snapshotFiles, extractedFiles...)
Expand All @@ -98,7 +99,7 @@ func (a *AddCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
}

if err := copyCmd.ExecuteCommand(config, buildArgs); err != nil {
return err
return errors.Wrap(err, "executing copy command")
}
a.snapshotFiles = append(a.snapshotFiles, copyCmd.snapshotFiles...)
return nil
Expand Down
17 changes: 9 additions & 8 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu

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

srcs, dest, err := util.ResolveEnvAndWildcards(c.cmd.SourcesAndDest, c.buildcontext, replacementEnvs)
if err != nil {
return err
return errors.Wrap(err, "resolving src")
}

// For each source, iterate through and copy it over
for _, src := range srcs {
fullPath := filepath.Join(c.buildcontext, src)

fi, err := os.Lstat(fullPath)
if err != nil {
return errors.Wrap(err, "could not copy source")
Expand All @@ -79,27 +80,27 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu

destPath, err := util.DestinationFilepath(fullPath, dest, cwd)
if err != nil {
return err
return errors.Wrap(err, "find destination path")
}

// If the destination dir is a symlink we need to resolve the path and use
// that instead of the symlink path
destPath, err = resolveIfSymlink(destPath)
if err != nil {
return err
return errors.Wrap(err, "resolving dest symlink")
}

if fi.IsDir() {
copiedFiles, err := util.CopyDir(fullPath, destPath, c.buildcontext, uid, gid)
if err != nil {
return err
return errors.Wrap(err, "copying dir")
}
c.snapshotFiles = append(c.snapshotFiles, copiedFiles...)
} else if util.IsSymlink(fi) {
// If file is a symlink, we want to copy the target file to destPath
exclude, err := util.CopySymlink(fullPath, destPath, c.buildcontext, uid, gid)
exclude, err := util.CopySymlink(fullPath, destPath, c.buildcontext)
if err != nil {
return err
return errors.Wrap(err, "copying symlink")
}
if exclude {
continue
Expand All @@ -109,7 +110,7 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
// ... Else, we want to copy over a file
exclude, err := util.CopyFile(fullPath, destPath, c.buildcontext, uid, gid)
if err != nil {
return err
return errors.Wrap(err, "copying file")
}
if exclude {
continue
Expand Down
Loading

0 comments on commit c718dc6

Please sign in to comment.