add a new method to indicate if the command provides files to snapshot

This commit is contained in:
Tejal Desai 2020-05-01 15:50:46 -07:00
parent 2e1ca5f19d
commit 32e3336d4c
9 changed files with 61 additions and 10 deletions

View File

@ -116,6 +116,10 @@ func (a *AddCommand) FilesToSnapshot() []string {
return a.snapshotFiles return a.snapshotFiles
} }
func (a *AddCommand) ProvidesFilesToSnapshot() bool {
return true
}
// String returns some information about the command for the image config // String returns some information about the command for the image config
func (a *AddCommand) String() string { func (a *AddCommand) String() string {
return a.cmd.String() return a.cmd.String()

View File

@ -32,6 +32,10 @@ func (b *BaseCommand) FilesToSnapshot() []string {
return []string{} return []string{}
} }
func (b *BaseCommand) ProvidesFilesToSnapshot() bool {
return false
}
func (b *BaseCommand) FilesUsedFromContext(_ *v1.Config, _ *dockerfile.BuildArgs) ([]string, error) { func (b *BaseCommand) FilesUsedFromContext(_ *v1.Config, _ *dockerfile.BuildArgs) ([]string, error) {
return []string{}, nil return []string{}, nil
} }

View File

@ -37,6 +37,10 @@ type DockerCommand interface {
// A list of files to snapshot, empty for metadata commands or nil if we don't know // A list of files to snapshot, empty for metadata commands or nil if we don't know
FilesToSnapshot() []string 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. // Return a cache-aware implementation of this command, if it exists.
CacheCommand(v1.Image) DockerCommand CacheCommand(v1.Image) DockerCommand

View File

@ -138,6 +138,10 @@ func (c *CopyCommand) MetadataOnly() bool {
return false return false
} }
func (c *CopyCommand) ProvidesFilesToSnapshot() bool {
return true
}
func (c *CopyCommand) RequiresUnpackedFS() bool { func (c *CopyCommand) RequiresUnpackedFS() bool {
return true return true
} }
@ -210,6 +214,14 @@ func (cr *CachingCopyCommand) FilesToSnapshot() []string {
return f return f
} }
func (cr *CachingCopyCommand) MetadataOnly() bool {
return false
}
func (cr *CachingCopyCommand) ProvidesFilesToSnapshot() bool {
return true
}
func (cr *CachingCopyCommand) String() string { func (cr *CachingCopyCommand) String() string {
if cr.cmd == nil { if cr.cmd == nil {
return "nil command" return "nil command"

View File

@ -151,6 +151,10 @@ func (r *RunCommand) FilesToSnapshot() []string {
return nil return nil
} }
func (r *RunCommand) ProvidesFilesToSnapshot() bool {
return false
}
// CacheCommand returns true since this command should be cached // CacheCommand returns true since this command should be cached
func (r *RunCommand) CacheCommand(img v1.Image) DockerCommand { func (r *RunCommand) CacheCommand(img v1.Image) DockerCommand {
@ -227,3 +231,11 @@ func (cr *CachingRunCommand) String() string {
} }
return cr.cmd.String() return cr.cmd.String()
} }
func (cr *CachingRunCommand) MetadataOnly() bool {
return false
}
func (cr *CachingRunCommand) RequiresUnpackedFS() bool {
return true
}

View File

@ -75,3 +75,7 @@ func (w *WorkdirCommand) String() string {
func (w *WorkdirCommand) MetadataOnly() bool { func (w *WorkdirCommand) MetadataOnly() bool {
return false return false
} }
func (w *WorkdirCommand) ProvidesFilesToSnapshot() bool {
return true
}

View File

@ -347,15 +347,18 @@ func (s *stageBuilder) build() error {
default: default:
return false return false
} }
} }()
if !isCacheCommand() && !initSnapshotTaken { if !initSnapshotTaken && !isCacheCommand && !command.MetadataOnly() {
// Take initial snapshot if !command.ProvidesFilesToSnapshot() {
t := timing.Start("Initial FS snapshot") // Take initial snapshot if command is not metadata only
if err := s.snapshotter.Init(); err != nil { // and does not return a list of files changed
return err 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 { if err := command.ExecuteCommand(&s.cf.Config, s.args); err != nil {
@ -368,7 +371,7 @@ func (s *stageBuilder) build() error {
continue continue
} }
if isCacheCommand() { if isCacheCommand {
v := command.(commands.Cached) v := command.(commands.Cached)
layer := v.Layer() layer := v.Layer()
if err := s.saveLayerToImage(layer, command.String()); err != nil { if err := s.saveLayerToImage(layer, command.String()); err != nil {

View File

@ -55,6 +55,9 @@ func (m MockDockerCommand) String() string {
func (m MockDockerCommand) FilesToSnapshot() []string { func (m MockDockerCommand) FilesToSnapshot() []string {
return []string{"meow-snapshot-no-cache"} return []string{"meow-snapshot-no-cache"}
} }
func (m MockDockerCommand) ProvidesFilesToSnapshot() bool {
return true
}
func (m MockDockerCommand) CacheCommand(image v1.Image) commands.DockerCommand { func (m MockDockerCommand) CacheCommand(image v1.Image) commands.DockerCommand {
return m.cacheCommand return m.cacheCommand
} }
@ -84,6 +87,9 @@ func (m MockCachedDockerCommand) String() string {
func (m MockCachedDockerCommand) FilesToSnapshot() []string { func (m MockCachedDockerCommand) FilesToSnapshot() []string {
return []string{"meow-snapshot"} return []string{"meow-snapshot"}
} }
func (m MockCachedDockerCommand) ProvidesFilesToSnapshot() bool {
return true
}
func (m MockCachedDockerCommand) CacheCommand(image v1.Image) commands.DockerCommand { func (m MockCachedDockerCommand) CacheCommand(image v1.Image) commands.DockerCommand {
return nil return nil
} }

View File

@ -39,6 +39,7 @@ var snapshotPathPrefix = config.KanikoDir
// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken // Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
type Snapshotter struct { type Snapshotter struct {
i int
l *LayeredMap l *LayeredMap
directory string directory string
whitelist []util.WhitelistEntry whitelist []util.WhitelistEntry
@ -51,7 +52,8 @@ func NewSnapshotter(l *LayeredMap, d string) *Snapshotter {
// Init initializes a new snapshotter // Init initializes a new snapshotter
func (s *Snapshotter) Init() error { func (s *Snapshotter) Init() error {
logrus.Info("Taking initial snapshot") s.i++
logrus.Infof("Taking initial snapshot %d", s.i)
_, _, err := s.scanFullFilesystem() _, _, err := s.scanFullFilesystem()
return err return err
} }