Skip to content

Commit

Permalink
add a new method to indicate if the command provides files to snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed May 1, 2020
1 parent 2e1ca5f commit 32e3336
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 10 deletions.
4 changes: 4 additions & 0 deletions pkg/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func (a *AddCommand) FilesToSnapshot() []string {
return a.snapshotFiles
}

func (a *AddCommand) ProvidesFilesToSnapshot() bool {
return true
}

// String returns some information about the command for the image config
func (a *AddCommand) String() string {
return a.cmd.String()
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/base_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (b *BaseCommand) FilesToSnapshot() []string {
return []string{}
}

func (b *BaseCommand) ProvidesFilesToSnapshot() bool {
return false
}

func (b *BaseCommand) FilesUsedFromContext(_ *v1.Config, _ *dockerfile.BuildArgs) ([]string, error) {
return []string{}, nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type DockerCommand interface {
// A list of files to snapshot, empty for metadata commands or nil if we don't know
FilesToSnapshot() []string

// ProvidesFileToSnapshot is true for all metadata commands and commands which know
// list of files changed. False for Run command.
ProvidesFilesToSnapshot() bool

// Return a cache-aware implementation of this command, if it exists.
CacheCommand(v1.Image) DockerCommand

Expand Down
12 changes: 12 additions & 0 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func (c *CopyCommand) MetadataOnly() bool {
return false
}

func (c *CopyCommand) ProvidesFilesToSnapshot() bool {
return true
}

func (c *CopyCommand) RequiresUnpackedFS() bool {
return true
}
Expand Down Expand Up @@ -210,6 +214,14 @@ func (cr *CachingCopyCommand) FilesToSnapshot() []string {
return f
}

func (cr *CachingCopyCommand) MetadataOnly() bool {
return false
}

func (cr *CachingCopyCommand) ProvidesFilesToSnapshot() bool {
return true
}

func (cr *CachingCopyCommand) String() string {
if cr.cmd == nil {
return "nil command"
Expand Down
12 changes: 12 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (r *RunCommand) FilesToSnapshot() []string {
return nil
}

func (r *RunCommand) ProvidesFilesToSnapshot() bool {
return false
}

// CacheCommand returns true since this command should be cached
func (r *RunCommand) CacheCommand(img v1.Image) DockerCommand {

Expand Down Expand Up @@ -227,3 +231,11 @@ func (cr *CachingRunCommand) String() string {
}
return cr.cmd.String()
}

func (cr *CachingRunCommand) MetadataOnly() bool {
return false
}

func (cr *CachingRunCommand) RequiresUnpackedFS() bool {
return true
}
4 changes: 4 additions & 0 deletions pkg/commands/workdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ func (w *WorkdirCommand) String() string {
func (w *WorkdirCommand) MetadataOnly() bool {
return false
}

func (w *WorkdirCommand) ProvidesFilesToSnapshot() bool {
return true
}
21 changes: 12 additions & 9 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,18 @@ func (s *stageBuilder) build() error {
default:
return false
}
}
if !isCacheCommand() && !initSnapshotTaken {
// Take initial snapshot
t := timing.Start("Initial FS snapshot")
if err := s.snapshotter.Init(); err != nil {
return err
}()
if !initSnapshotTaken && !isCacheCommand && !command.MetadataOnly() {
if !command.ProvidesFilesToSnapshot() {
// Take initial snapshot if command is not metadata only
// and does not return a list of files changed
t := timing.Start("Initial FS snapshot")
if err := s.snapshotter.Init(); err != nil {
return err
}
timing.DefaultRun.Stop(t)
initSnapshotTaken = true
}
timing.DefaultRun.Stop(t)
initSnapshotTaken = true
}

if err := command.ExecuteCommand(&s.cf.Config, s.args); err != nil {
Expand All @@ -368,7 +371,7 @@ func (s *stageBuilder) build() error {
continue
}

if isCacheCommand() {
if isCacheCommand {
v := command.(commands.Cached)
layer := v.Layer()
if err := s.saveLayerToImage(layer, command.String()); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/executor/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (m MockDockerCommand) String() string {
func (m MockDockerCommand) FilesToSnapshot() []string {
return []string{"meow-snapshot-no-cache"}
}
func (m MockDockerCommand) ProvidesFilesToSnapshot() bool {
return true
}
func (m MockDockerCommand) CacheCommand(image v1.Image) commands.DockerCommand {
return m.cacheCommand
}
Expand Down Expand Up @@ -84,6 +87,9 @@ func (m MockCachedDockerCommand) String() string {
func (m MockCachedDockerCommand) FilesToSnapshot() []string {
return []string{"meow-snapshot"}
}
func (m MockCachedDockerCommand) ProvidesFilesToSnapshot() bool {
return true
}
func (m MockCachedDockerCommand) CacheCommand(image v1.Image) commands.DockerCommand {
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var snapshotPathPrefix = config.KanikoDir

// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
type Snapshotter struct {
i int
l *LayeredMap
directory string
whitelist []util.WhitelistEntry
Expand All @@ -51,7 +52,8 @@ func NewSnapshotter(l *LayeredMap, d string) *Snapshotter {

// Init initializes a new snapshotter
func (s *Snapshotter) Init() error {
logrus.Info("Taking initial snapshot")
s.i++
logrus.Infof("Taking initial snapshot %d", s.i)
_, _, err := s.scanFullFilesystem()
return err
}
Expand Down

0 comments on commit 32e3336

Please sign in to comment.