Skip to content

Commit

Permalink
better shdTakeSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Jun 7, 2020
1 parent b746274 commit c85d64c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/base_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ func (b *BaseCommand) ShouldCacheOutput() bool {
return false
}

func (a *BaseCommand) ShouldDetectDeletedFiles() bool {
func (b *BaseCommand) ShouldDetectDeletedFiles() bool {
return false
}
21 changes: 4 additions & 17 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ func (s *stageBuilder) build() error {
files = command.FilesToSnapshot()
timing.DefaultRun.Stop(t)

if !s.shouldTakeSnapshot(index, files, command.ProvidesFilesToSnapshot(),
command.ShouldDetectDeletedFiles()) {
if !s.shouldTakeSnapshot(index, files, command.MetadataOnly()) {
continue
}
if isCacheCommand {
Expand Down Expand Up @@ -433,7 +432,7 @@ func (s *stageBuilder) takeSnapshot(files []string, shdDelete bool) (string, 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

// We only snapshot the very end with single snapshot mode on.
Expand All @@ -446,20 +445,8 @@ func (s *stageBuilder) shouldTakeSnapshot(index int, files []string, provideFile
return true
}

// if command does not provide files, snapshot everything.
if !provideFiles {
return true
}

if detectDeletion {
return true
}
// Don't snapshot an empty list.
if len(files) == 0 {
return false
}

return true
// if command is a metadata command, do not snapshot.
return !isMetadatCmd
}

func (s *stageBuilder) saveSnapshotToImage(createdBy string, tarPath string) error {
Expand Down
20 changes: 10 additions & 10 deletions pkg/executor/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
cmds []commands.DockerCommand
}
type args struct {
index int
files []string
hasFiles bool
index int
files []string
metadataOnly bool
}
tests := []struct {
name string
Expand Down Expand Up @@ -158,9 +158,9 @@ func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
stage: config.KanikoStage{},
},
args: args{
index: 0,
files: []string{},
hasFiles: true,
index: 0,
files: []string{},
metadataOnly: true,
},
want: false,
},
Expand All @@ -172,9 +172,9 @@ func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
},
},
args: args{
index: 0,
files: nil,
hasFiles: false,
index: 0,
files: nil,
metadataOnly: false,
},
want: true,
},
Expand Down Expand Up @@ -204,7 +204,7 @@ func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
opts: tt.fields.opts,
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)
}
})
Expand Down
1 change: 0 additions & 1 deletion pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func (s *Snapshotter) TakeSnapshot(files []string, shdCheckDelete bool) (string,
}
}
}
logrus.Infof("whiteouts %s", filesToWhiteout)
t := util.NewTar(f)
defer t.Close()
if err := writeToTar(t, filesToAdd, filesToWhiteout); err != nil {
Expand Down

0 comments on commit c85d64c

Please sign in to comment.