Add hash function which only considers mtime when snapshotting

This commit is contained in:
Priya Wadhwa 2018-03-27 17:43:35 -07:00
parent 01df106246
commit e6eb5d1abf
No known key found for this signature in database
GPG Key ID: 0D0DAFD8F7AA73AE
3 changed files with 31 additions and 3 deletions

View File

@ -33,6 +33,7 @@ var (
dockerfilePath string dockerfilePath string
destination string destination string
srcContext string srcContext string
mtime bool
logLevel string logLevel string
) )
@ -40,6 +41,7 @@ func init() {
RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "/workspace/Dockerfile", "Path to the dockerfile to be built.") RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "/workspace/Dockerfile", "Path to the dockerfile to be built.")
RootCmd.PersistentFlags().StringVarP(&srcContext, "context", "c", "", "Path to the dockerfile build context.") RootCmd.PersistentFlags().StringVarP(&srcContext, "context", "c", "", "Path to the dockerfile build context.")
RootCmd.PersistentFlags().StringVarP(&destination, "destination", "d", "", "Registry the final image should be pushed to (ex: gcr.io/test/example:latest)") RootCmd.PersistentFlags().StringVarP(&destination, "destination", "d", "", "Registry the final image should be pushed to (ex: gcr.io/test/example:latest)")
RootCmd.PersistentFlags().BoolVarP(&mtime, "mtime", "", false, "Only look at mtime of a file when snapshotting")
RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic") RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic")
} }
@ -74,8 +76,12 @@ func execute() error {
if err := util.ExtractFileSystemFromImage(baseImage); err != nil { if err := util.ExtractFileSystemFromImage(baseImage); err != nil {
return err return err
} }
hasher := util.Hasher()
l := snapshot.NewLayeredMap(util.Hasher()) if mtime {
logrus.Info("Only file modification times will be considered when snapshotting.")
hasher = util.MtimeHasher()
}
l := snapshot.NewLayeredMap(hasher)
snapshotter := snapshot.NewSnapshotter(l, constants.RootDir) snapshotter := snapshot.NewSnapshotter(l, constants.RootDir)
// Take initial snapshot // Take initial snapshot

View File

@ -28,6 +28,7 @@ var fileTests = []struct {
configPath string configPath string
context string context string
repo string repo string
mtime bool
}{ }{
{ {
description: "test extract filesystem", description: "test extract filesystem",
@ -35,6 +36,7 @@ var fileTests = []struct {
configPath: "/workspace/integration_tests/dockerfiles/config_test_extract_fs.json", configPath: "/workspace/integration_tests/dockerfiles/config_test_extract_fs.json",
context: "integration_tests/dockerfiles/", context: "integration_tests/dockerfiles/",
repo: "extract-filesystem", repo: "extract-filesystem",
mtime: true,
}, },
{ {
description: "test run", description: "test run",
@ -49,6 +51,7 @@ var fileTests = []struct {
configPath: "/workspace/integration_tests/dockerfiles/config_test_run_2.json", configPath: "/workspace/integration_tests/dockerfiles/config_test_run_2.json",
context: "integration_tests/dockerfiles/", context: "integration_tests/dockerfiles/",
repo: "test-run-2", repo: "test-run-2",
mtime: true,
}, },
{ {
description: "test copy", description: "test copy",
@ -56,6 +59,7 @@ var fileTests = []struct {
configPath: "/workspace/integration_tests/dockerfiles/config_test_copy.json", configPath: "/workspace/integration_tests/dockerfiles/config_test_copy.json",
context: "/workspace/integration_tests/", context: "/workspace/integration_tests/",
repo: "test-copy", repo: "test-copy",
mtime: true,
}, },
} }
@ -139,9 +143,13 @@ func main() {
// Then, buld the image with kbuild // Then, buld the image with kbuild
kbuildImage := testRepo + kbuildPrefix + test.repo kbuildImage := testRepo + kbuildPrefix + test.repo
mtime := ""
if test.mtime {
mtime = "--mtime"
}
kbuild := step{ kbuild := step{
Name: executorImage, Name: executorImage,
Args: []string{executorCommand, "--destination", kbuildImage, "--dockerfile", test.dockerfilePath, "--context", test.context}, Args: []string{executorCommand, "--destination", kbuildImage, "--dockerfile", test.dockerfilePath, "--context", test.context, mtime},
} }
// Pull the kbuild image // Pull the kbuild image

View File

@ -61,3 +61,17 @@ func Hasher() func(string) (string, error) {
} }
return hasher return hasher
} }
// Hasher returns a hash function, which only looks at mtime to determine if a file has changed
func MtimeHasher() func(string) (string, error) {
hasher := func(p string) (string, error) {
h := md5.New()
fi, err := os.Lstat(p)
if err != nil {
return "", err
}
h.Write([]byte(fi.ModTime().String()))
return hex.EncodeToString(h.Sum(nil)), nil
}
return hasher
}