From dbabcb1f5fc101c2735c9438cf326e8a8a51aac4 Mon Sep 17 00:00:00 2001 From: Don McCasland Date: Thu, 26 Sep 2019 15:32:40 -0700 Subject: [PATCH] Adding CachingCopy command --- pkg/commands/copy.go | 42 ++++++++++++++++++++++++++++++++++++++++-- pkg/commands/run.go | 2 +- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/pkg/commands/copy.go b/pkg/commands/copy.go index 1f62b4311f..0dd4272398 100644 --- a/pkg/commands/copy.go +++ b/pkg/commands/copy.go @@ -21,6 +21,7 @@ import ( "path/filepath" "github.com/moby/buildkit/frontend/dockerfile/instructions" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/GoogleContainerTools/kaniko/pkg/constants" @@ -70,7 +71,6 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu // we need to add '/' to the end to indicate the destination is a directory dest = filepath.Join(cwd, dest) + "/" } - logrus.Debugf("Calling CopyDir fullPath:%s dest:%s", fullPath, dest) copiedFiles, err := util.CopyDir(fullPath, dest, c.buildcontext) if err != nil { return err @@ -88,7 +88,6 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu c.snapshotFiles = append(c.snapshotFiles, destPath) } else { // ... Else, we want to copy over a file - logrus.Debugf("Calling CopyFile fullPath:%s destPath:%s", fullPath, destPath) exclude, err := util.CopyFile(fullPath, destPath, c.buildcontext) if err != nil { return err @@ -140,3 +139,42 @@ func (c *CopyCommand) MetadataOnly() bool { func (c *CopyCommand) RequiresUnpackedFS() bool { return true } + +func (r *CopyCommand) ShouldCacheOutput() bool { + return true +} + +// CacheCommand returns true since this command should be cached +func (r *CopyCommand) CacheCommand(img v1.Image) DockerCommand { + + return &CachingCopyCommand{ + img: img, + cmd: r.cmd, + } +} + +type CachingCopyCommand struct { + BaseCommand + img v1.Image + extractedFiles []string + cmd *instructions.CopyCommand +} + +func (cr *CachingCopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error { + logrus.Infof("Found cached layer, extracting to filesystem") + var err error + cr.extractedFiles, err = util.GetFSFromImage(constants.RootDir, cr.img) + logrus.Infof("extractedFiles: %s", cr.extractedFiles) + if err != nil { + return errors.Wrap(err, "extracting fs from image") + } + return nil +} + +func (cr *CachingCopyCommand) FilesToSnapshot() []string { + return cr.extractedFiles +} + +func (cr *CachingCopyCommand) String() string { + return cr.cmd.String() +} diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 7a9bf03dc2..0122f37d52 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -28,7 +28,7 @@ import ( "github.com/GoogleContainerTools/kaniko/pkg/constants" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/util" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/pkg/errors" "github.com/sirupsen/logrus"