Skip to content

Commit

Permalink
Speed up workdir by always returning an empty filelist (rather than a…
Browse files Browse the repository at this point in the history
… nil one).

Nil indicates that a full snapshot is required. Empty indicates that nothing should be snapshotted.
  • Loading branch information
dlorenc committed Feb 11, 2019
1 parent 114a085 commit c2f19e0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
3 changes: 2 additions & 1 deletion pkg/commands/workdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ func (w *WorkdirCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile
logrus.Infof("Changed working directory to %s", config.WorkingDir)

// Only create and snapshot the dir if it didn't exist already
w.snapshotFiles = []string{}
if _, err := os.Stat(config.WorkingDir); os.IsNotExist(err) {
logrus.Infof("Creating directory %s", config.WorkingDir)
w.snapshotFiles = []string{config.WorkingDir}
w.snapshotFiles = append(w.snapshotFiles, config.WorkingDir)
return os.MkdirAll(config.WorkingDir, 0755)
}
return nil
Expand Down
48 changes: 31 additions & 17 deletions pkg/commands/workdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,49 @@ import (
// This is needed to make sure WorkingDir handles paths correctly
// For example, if WORKDIR specifies a non-absolute path, it should be appended to the current WORKDIR
var workdirTests = []struct {
path string
expectedPath string
path string
expectedPath string
snapshotFiles []string
}{
{
path: "/a",
expectedPath: "/a",
path: "/a",
expectedPath: "/a",
snapshotFiles: []string{"/a"},
},
{
path: "b",
expectedPath: "/a/b",
path: "b",
expectedPath: "/a/b",
snapshotFiles: []string{"/a/b"},
},
{
path: "c",
expectedPath: "/a/b/c",
path: "c",
expectedPath: "/a/b/c",
snapshotFiles: []string{"/a/b/c"},
},
{
path: "/d",
expectedPath: "/d",
path: "/d",
expectedPath: "/d",
snapshotFiles: []string{"/d"},
},
{
path: "$path",
expectedPath: "/d/usr",
path: "$path",
expectedPath: "/d/usr",
snapshotFiles: []string{"/d/usr"},
},
{
path: "$home",
expectedPath: "/root",
path: "$home",
expectedPath: "/root",
snapshotFiles: []string{"/root"},
},
{
path: "$path/$home",
expectedPath: "/root/usr/root",
path: "$path/$home",
expectedPath: "/root/usr/root",
snapshotFiles: []string{"/root/usr/root"},
},
{
path: "/tmp",
expectedPath: "/tmp",
snapshotFiles: []string{},
},
}

Expand All @@ -78,10 +91,11 @@ func TestWorkdirCommand(t *testing.T) {
cmd: &instructions.WorkdirCommand{
Path: test.path,
},
snapshotFiles: []string{},
snapshotFiles: nil,
}
buildArgs := dockerfile.NewBuildArgs([]string{})
cmd.ExecuteCommand(cfg, buildArgs)
testutil.CheckErrorAndDeepEqual(t, false, nil, test.expectedPath, cfg.WorkingDir)
testutil.CheckErrorAndDeepEqual(t, false, nil, test.snapshotFiles, cmd.snapshotFiles)
}
}

0 comments on commit c2f19e0

Please sign in to comment.