Speed up workdir by always returning an empty filelist (rather than a nil one). (#557)

Nil indicates that a full snapshot is required. Empty indicates that nothing should be snapshotted.
This commit is contained in:
dlorenc 2019-02-13 11:55:08 -06:00 committed by GitHub
parent 877abd30ed
commit 15e70d4142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 19 deletions

View File

@ -34,6 +34,9 @@ type WorkdirCommand struct {
snapshotFiles []string snapshotFiles []string
} }
// For testing
var mkdir = os.MkdirAll
func (w *WorkdirCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error { func (w *WorkdirCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
logrus.Info("cmd: workdir") logrus.Info("cmd: workdir")
workdirPath := w.cmd.Path workdirPath := w.cmd.Path
@ -50,10 +53,11 @@ func (w *WorkdirCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile
logrus.Infof("Changed working directory to %s", config.WorkingDir) logrus.Infof("Changed working directory to %s", config.WorkingDir)
// Only create and snapshot the dir if it didn't exist already // Only create and snapshot the dir if it didn't exist already
w.snapshotFiles = []string{}
if _, err := os.Stat(config.WorkingDir); os.IsNotExist(err) { if _, err := os.Stat(config.WorkingDir); os.IsNotExist(err) {
logrus.Infof("Creating directory %s", config.WorkingDir) 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 mkdir(config.WorkingDir, 0755)
} }
return nil return nil
} }

View File

@ -16,6 +16,7 @@ limitations under the License.
package commands package commands
import ( import (
"os"
"testing" "testing"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
@ -32,39 +33,64 @@ import (
var workdirTests = []struct { var workdirTests = []struct {
path string path string
expectedPath string expectedPath string
snapshotFiles []string
}{ }{
{ {
path: "/a", path: "/a",
expectedPath: "/a", expectedPath: "/a",
snapshotFiles: []string{"/a"},
}, },
{ {
path: "b", path: "b",
expectedPath: "/a/b", expectedPath: "/a/b",
snapshotFiles: []string{"/a/b"},
}, },
{ {
path: "c", path: "c",
expectedPath: "/a/b/c", expectedPath: "/a/b/c",
snapshotFiles: []string{"/a/b/c"},
}, },
{ {
path: "/d", path: "/d",
expectedPath: "/d", expectedPath: "/d",
snapshotFiles: []string{"/d"},
}, },
{ {
path: "$path", path: "$path",
expectedPath: "/d/usr", expectedPath: "/d/usr",
snapshotFiles: []string{"/d/usr"},
}, },
{ {
path: "$home", path: "$home",
expectedPath: "/root", expectedPath: "/root",
snapshotFiles: []string{},
}, },
{ {
path: "$path/$home", path: "/foo/$path/$home",
expectedPath: "/root/usr/root", expectedPath: "/foo/usr/root",
snapshotFiles: []string{"/foo/usr/root"},
},
{
path: "/tmp",
expectedPath: "/tmp",
snapshotFiles: []string{},
}, },
} }
// For testing
func mockDir(p string, fi os.FileMode) error {
return nil
}
func TestWorkdirCommand(t *testing.T) { func TestWorkdirCommand(t *testing.T) {
// Mock out mkdir for testing.
oldMkdir := mkdir
mkdir = mockDir
defer func() {
mkdir = oldMkdir
}()
cfg := &v1.Config{ cfg := &v1.Config{
WorkingDir: "/", WorkingDir: "/",
Env: []string{ Env: []string{
@ -78,10 +104,11 @@ func TestWorkdirCommand(t *testing.T) {
cmd: &instructions.WorkdirCommand{ cmd: &instructions.WorkdirCommand{
Path: test.path, Path: test.path,
}, },
snapshotFiles: []string{}, snapshotFiles: nil,
} }
buildArgs := dockerfile.NewBuildArgs([]string{}) buildArgs := dockerfile.NewBuildArgs([]string{})
cmd.ExecuteCommand(cfg, buildArgs) cmd.ExecuteCommand(cfg, buildArgs)
testutil.CheckErrorAndDeepEqual(t, false, nil, test.expectedPath, cfg.WorkingDir) testutil.CheckErrorAndDeepEqual(t, false, nil, test.expectedPath, cfg.WorkingDir)
testutil.CheckErrorAndDeepEqual(t, false, nil, test.snapshotFiles, cmd.snapshotFiles)
} }
} }