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
}
// For testing
var mkdir = os.MkdirAll
func (w *WorkdirCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
logrus.Info("cmd: workdir")
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)
// 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}
return os.MkdirAll(config.WorkingDir, 0755)
w.snapshotFiles = append(w.snapshotFiles, config.WorkingDir)
return mkdir(config.WorkingDir, 0755)
}
return nil
}

View File

@ -16,6 +16,7 @@ limitations under the License.
package commands
import (
"os"
"testing"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
@ -30,41 +31,66 @@ 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{},
},
{
path: "$path/$home",
expectedPath: "/root/usr/root",
path: "/foo/$path/$home",
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) {
// Mock out mkdir for testing.
oldMkdir := mkdir
mkdir = mockDir
defer func() {
mkdir = oldMkdir
}()
cfg := &v1.Config{
WorkingDir: "/",
Env: []string{
@ -78,10 +104,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)
}
}