Skip to content

Commit

Permalink
better error wrapping and add more tests for copy
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Mar 7, 2020
1 parent b4b70d0 commit 6c14d20
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 83 deletions.
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
34 changes: 20 additions & 14 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,23 @@ 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)
srcPath, err := resolveIfSymlink(fullPath)
if err != nil {
return errors.Wrap(err, "resolving src symlink")
}

fi, err := os.Lstat(srcPath)
if err != nil {
return errors.Wrap(err, "could not copy source")
}
Expand All @@ -79,26 +84,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 errors.Wrap(err, "resolving dest symlink")
}

if fi.IsDir() {
// 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
}
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, uid, gid, false)
if err != nil {
return err
return errors.Wrap(err, "copying symlink")
}
if exclude {
continue
Expand All @@ -108,7 +114,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 6c14d20

Please sign in to comment.