Skip to content

Commit

Permalink
Reproducible add with flag
Browse files Browse the repository at this point in the history
  • Loading branch information
MJDSys committed Jul 11, 2019
1 parent baba32c commit 62d0cdf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (s *stageBuilder) build() error {
}
// Take initial snapshot
t := timing.Start("Initial FS snapshot")
if err := s.snapshotter.Init(); err != nil {
if err := s.snapshotter.Init(s.opts.Reproducible); err != nil {
return err
}
timing.DefaultRun.Stop(t)
Expand Down Expand Up @@ -290,7 +290,7 @@ func (s *stageBuilder) takeSnapshot(files []string) (string, error) {
var err error
t := timing.Start("Snapshotting FS")
if files == nil || s.opts.SingleSnapshot {
snapshot, err = s.snapshotter.TakeSnapshotFS()
snapshot, err = s.snapshotter.TakeSnapshotFS(s.opts.Reproducible)
} else {
// Volumes are very weird. They get snapshotted in the next command.
files = append(files, util.Volumes()...)
Expand Down
15 changes: 10 additions & 5 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"syscall"

"github.com/GoogleContainerTools/kaniko/pkg/timing"
Expand Down Expand Up @@ -47,8 +48,8 @@ func NewSnapshotter(l *LayeredMap, d string) *Snapshotter {
}

// Init initializes a new snapshotter
func (s *Snapshotter) Init() error {
_, _, err := s.scanFullFilesystem()
func (s *Snapshotter) Init(reproducible bool) error {
_, _, err := s.scanFullFilesystem(reproducible)
return err
}

Expand Down Expand Up @@ -94,7 +95,7 @@ func (s *Snapshotter) TakeSnapshot(files []string) (string, error) {

// TakeSnapshotFS takes a snapshot of the filesystem, avoiding directories in the whitelist, and creates
// a tarball of the changed files.
func (s *Snapshotter) TakeSnapshotFS() (string, error) {
func (s *Snapshotter) TakeSnapshotFS(reproducible bool) (string, error) {
f, err := ioutil.TempFile(snapshotPathPrefix, "")
if err != nil {
return "", err
Expand All @@ -103,7 +104,7 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
t := util.NewTar(f)
defer t.Close()

filesToAdd, filesToWhiteOut, err := s.scanFullFilesystem()
filesToAdd, filesToWhiteOut, err := s.scanFullFilesystem(reproducible)
if err != nil {
return "", err
}
Expand All @@ -115,7 +116,7 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
return f.Name(), nil
}

func (s *Snapshotter) scanFullFilesystem() ([]string, []string, error) {
func (s *Snapshotter) scanFullFilesystem(reproducible bool) ([]string, []string, error) {
logrus.Info("Taking snapshot of full filesystem...")

// Some of the operations that follow (e.g. hashing) depend on the file system being synced,
Expand Down Expand Up @@ -186,6 +187,10 @@ func (s *Snapshotter) scanFullFilesystem() ([]string, []string, error) {
// Also add parent directories to keep the permission of them correctly.
filesToAdd = filesWithParentDirs(filesToAdd)

if reproducible {
sort.Strings(filesToAdd)
}

// Add files to the layered map
for _, file := range filesToAdd {
if err := s.l.Add(file); err != nil {
Expand Down
47 changes: 43 additions & 4 deletions pkg/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestSnapshotFSFileChange(t *testing.T) {
t.Fatalf("Error setting up fs: %s", err)
}
// Take another snapshot
tarPath, err := snapshotter.TakeSnapshotFS()
tarPath, err := snapshotter.TakeSnapshotFS(false)
if err != nil {
t.Fatalf("Error taking snapshot of fs: %s", err)
}
Expand Down Expand Up @@ -87,6 +87,45 @@ func TestSnapshotFSFileChange(t *testing.T) {
}
}

func TestSnapshotFSIsReproducible(t *testing.T) {
testDir, snapshotter, cleanup, err := setUpTestDir()
defer cleanup()
if err != nil {
t.Fatal(err)
}
// Make some changes to the filesystem
newFiles := map[string]string{
"foo": "newbaz1",
"bar/bat": "baz",
}
if err := testutil.SetupFiles(testDir, newFiles); err != nil {
t.Fatalf("Error setting up fs: %s", err)
}
// Take another snapshot
tarPath, err := snapshotter.TakeSnapshotFS(true)
if err != nil {
t.Fatalf("Error taking snapshot of fs: %s", err)
}

f, err := os.Open(tarPath)
if err != nil {
t.Fatal(err)
}
// Check contents of the snapshot, make sure contents are sorted by name
tr := tar.NewReader(f)
var filesInTar []string
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
filesInTar = append(filesInTar, hdr.Name)
}
if !sort.StringsAreSorted(filesInTar) {
t.Fatalf("Expected the file in the tar archive were sorted, actual list was not sorted: %v", filesInTar)
}
}

func TestSnapshotFSChangePermissions(t *testing.T) {
testDir, snapshotter, cleanup, err := setUpTestDir()
defer cleanup()
Expand All @@ -99,7 +138,7 @@ func TestSnapshotFSChangePermissions(t *testing.T) {
t.Fatalf("Error changing permissions on %s: %v", batPath, err)
}
// Take another snapshot
tarPath, err := snapshotter.TakeSnapshotFS()
tarPath, err := snapshotter.TakeSnapshotFS(false)
if err != nil {
t.Fatalf("Error taking snapshot of fs: %s", err)
}
Expand Down Expand Up @@ -192,7 +231,7 @@ func TestEmptySnapshotFS(t *testing.T) {
defer cleanup()

// Take snapshot with no changes
tarPath, err := snapshotter.TakeSnapshotFS()
tarPath, err := snapshotter.TakeSnapshotFS(false)
if err != nil {
t.Fatalf("Error taking snapshot of fs: %s", err)
}
Expand Down Expand Up @@ -234,7 +273,7 @@ func setUpTestDir() (string, *Snapshotter, func(), error) {
// Take the initial snapshot
l := NewLayeredMap(util.Hasher(), util.CacheHasher())
snapshotter := NewSnapshotter(l, testDir)
if err := snapshotter.Init(); err != nil {
if err := snapshotter.Init(false); err != nil {
return "", nil, nil, errors.Wrap(err, "initializing snapshotter")
}

Expand Down

0 comments on commit 62d0cdf

Please sign in to comment.