better shdTakeSnapshot

This commit is contained in:
Tejal Desai 2020-06-06 19:49:34 -07:00
parent b7462742c1
commit c85d64c8ae
4 changed files with 15 additions and 29 deletions

View File

@ -52,6 +52,6 @@ func (b *BaseCommand) ShouldCacheOutput() bool {
return false return false
} }
func (a *BaseCommand) ShouldDetectDeletedFiles() bool { func (b *BaseCommand) ShouldDetectDeletedFiles() bool {
return false return false
} }

View File

@ -372,8 +372,7 @@ func (s *stageBuilder) build() error {
files = command.FilesToSnapshot() files = command.FilesToSnapshot()
timing.DefaultRun.Stop(t) timing.DefaultRun.Stop(t)
if !s.shouldTakeSnapshot(index, files, command.ProvidesFilesToSnapshot(), if !s.shouldTakeSnapshot(index, files, command.MetadataOnly()) {
command.ShouldDetectDeletedFiles()) {
continue continue
} }
if isCacheCommand { if isCacheCommand {
@ -433,7 +432,7 @@ func (s *stageBuilder) takeSnapshot(files []string, shdDelete bool) (string, err
return snapshot, err return snapshot, err
} }
func (s *stageBuilder) shouldTakeSnapshot(index int, files []string, provideFiles bool, detectDeletion bool) bool { func (s *stageBuilder) shouldTakeSnapshot(index int, files []string, isMetadatCmd bool) bool {
isLastCommand := index == len(s.cmds)-1 isLastCommand := index == len(s.cmds)-1
// We only snapshot the very end with single snapshot mode on. // We only snapshot the very end with single snapshot mode on.
@ -446,20 +445,8 @@ func (s *stageBuilder) shouldTakeSnapshot(index int, files []string, provideFile
return true return true
} }
// if command does not provide files, snapshot everything. // if command is a metadata command, do not snapshot.
if !provideFiles { return !isMetadatCmd
return true
}
if detectDeletion {
return true
}
// Don't snapshot an empty list.
if len(files) == 0 {
return false
}
return true
} }
func (s *stageBuilder) saveSnapshotToImage(createdBy string, tarPath string) error { func (s *stageBuilder) saveSnapshotToImage(createdBy string, tarPath string) error {

View File

@ -103,9 +103,9 @@ func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
cmds []commands.DockerCommand cmds []commands.DockerCommand
} }
type args struct { type args struct {
index int index int
files []string files []string
hasFiles bool metadataOnly bool
} }
tests := []struct { tests := []struct {
name string name string
@ -158,9 +158,9 @@ func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
stage: config.KanikoStage{}, stage: config.KanikoStage{},
}, },
args: args{ args: args{
index: 0, index: 0,
files: []string{}, files: []string{},
hasFiles: true, metadataOnly: true,
}, },
want: false, want: false,
}, },
@ -172,9 +172,9 @@ func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
}, },
}, },
args: args{ args: args{
index: 0, index: 0,
files: nil, files: nil,
hasFiles: false, metadataOnly: false,
}, },
want: true, want: true,
}, },
@ -204,7 +204,7 @@ func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
opts: tt.fields.opts, opts: tt.fields.opts,
cmds: tt.fields.cmds, cmds: tt.fields.cmds,
} }
if got := s.shouldTakeSnapshot(tt.args.index, tt.args.files, tt.args.hasFiles, false); got != tt.want { if got := s.shouldTakeSnapshot(tt.args.index, tt.args.files, !tt.args.metadataOnly); got != tt.want {
t.Errorf("stageBuilder.shouldTakeSnapshot() = %v, want %v", got, tt.want) t.Errorf("stageBuilder.shouldTakeSnapshot() = %v, want %v", got, tt.want)
} }
}) })

View File

@ -111,7 +111,6 @@ func (s *Snapshotter) TakeSnapshot(files []string, shdCheckDelete bool) (string,
} }
} }
} }
logrus.Infof("whiteouts %s", filesToWhiteout)
t := util.NewTar(f) t := util.NewTar(f)
defer t.Close() defer t.Close()
if err := writeToTar(t, filesToAdd, filesToWhiteout); err != nil { if err := writeToTar(t, filesToAdd, filesToWhiteout); err != nil {