From 8fb17f60d9fda35d9b460f928447c58110b55e08 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Thu, 30 Apr 2020 13:25:22 -0700 Subject: [PATCH] Defer initial snapshot. Remove ReadSuccess() --- pkg/commands/cache.go | 6 ------ pkg/commands/copy.go | 2 -- pkg/commands/run.go | 1 - pkg/constants/constants.go | 2 +- pkg/executor/build.go | 38 +++++++++++++++++++++----------------- pkg/snapshot/snapshot.go | 1 + 6 files changed, 23 insertions(+), 27 deletions(-) diff --git a/pkg/commands/cache.go b/pkg/commands/cache.go index 1d37e6251..63970734f 100644 --- a/pkg/commands/cache.go +++ b/pkg/commands/cache.go @@ -20,18 +20,12 @@ import v1 "github.com/google/go-containerregistry/pkg/v1" type Cached interface { Layer() v1.Layer - ReadSuccess() bool } type caching struct { layer v1.Layer - readSuccess bool } func (c caching) Layer() v1.Layer { return c.layer } - -func (c caching) ReadSuccess() bool { - return c.readSuccess -} diff --git a/pkg/commands/copy.go b/pkg/commands/copy.go index e31efe2f3..472218fd6 100644 --- a/pkg/commands/copy.go +++ b/pkg/commands/copy.go @@ -189,8 +189,6 @@ func (cr *CachingCopyCommand) ExecuteCommand(config *v1.Config, buildArgs *docke } cr.layer = layers[0] - cr.readSuccess = true - cr.extractedFiles, err = util.GetFSFromLayers(kConfig.RootDir, layers, util.ExtractFunc(cr.extractFn), util.IncludeWhiteout()) logrus.Debugf("extractedFiles: %s", cr.extractedFiles) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 23b7f5fd7..d4a8003bf 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -200,7 +200,6 @@ func (cr *CachingRunCommand) ExecuteCommand(config *v1.Config, buildArgs *docker } cr.layer = layers[0] - cr.readSuccess = true cr.extractedFiles, err = util.GetFSFromLayers( kConfig.RootDir, diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 19c46cef3..8dd5893ea 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -18,7 +18,7 @@ package constants const ( // RootDir is the path to the root directory - RootDir = "/root" + RootDir = "/" // WorkspaceDir is the path to the workspace directory WorkspaceDir = "/workspace" diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 015466bf7..7090edc2f 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -317,13 +317,7 @@ func (s *stageBuilder) build() error { return errors.Wrap(err, "failed to check filesystem whitelist") } - // Take initial snapshot - t := timing.Start("Initial FS snapshot") - if err := s.snapshotter.Init(); err != nil { - return err - } - - timing.DefaultRun.Stop(t) + initSnapshotTaken := false cacheGroup := errgroup.Group{} for index, command := range s.cmds { @@ -346,6 +340,24 @@ func (s *stageBuilder) build() error { logrus.Info(command.String()) + isCacheCommand := func() bool { + switch command.(type) { + case commands.Cached: + return true + default: + return false + } + } + if !isCacheCommand() && !initSnapshotTaken { + // Take initial snapshot + t := timing.Start("Initial FS snapshot") + if err := s.snapshotter.Init(); err != nil { + return err + } + timing.DefaultRun.Stop(t) + initSnapshotTaken = true + } + if err := command.ExecuteCommand(&s.cf.Config, s.args); err != nil { return errors.Wrap(err, "failed to execute command") } @@ -356,16 +368,7 @@ func (s *stageBuilder) build() error { continue } - fn := func() bool { - switch v := command.(type) { - case commands.Cached: - return v.ReadSuccess() - default: - return false - } - } - - if fn() { + if isCacheCommand() { v := command.(commands.Cached) layer := v.Layer() if err := s.saveLayerToImage(layer, command.String()); err != nil { @@ -388,6 +391,7 @@ func (s *stageBuilder) build() error { // Push layer to cache (in parallel) now along with new config file if s.opts.Cache && command.ShouldCacheOutput() { cacheGroup.Go(func() error { + logrus.Infof("%s is saved at %s", ck, tarPath) return s.pushLayerToCache(s.opts, ck, tarPath, command.String()) }) } diff --git a/pkg/snapshot/snapshot.go b/pkg/snapshot/snapshot.go index 7b3df5661..d750601a4 100644 --- a/pkg/snapshot/snapshot.go +++ b/pkg/snapshot/snapshot.go @@ -51,6 +51,7 @@ func NewSnapshotter(l *LayeredMap, d string) *Snapshotter { // Init initializes a new snapshotter func (s *Snapshotter) Init() error { + logrus.Info("Taking initial snapshot") _, _, err := s.scanFullFilesystem() return err }