Skip to content

Commit

Permalink
Use Instant.EPOCH when adding docker layers
Browse files Browse the repository at this point in the history
Use Instant.EPOCH when adding docker layers so if the content of the files do not change the layer digest doesn't change either
  • Loading branch information
fbotis authored and gsmet committed Jul 18, 2022
1 parent 7519aec commit 4d3fd0b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,19 @@ public class JibConfig {
*/
@ConfigItem(defaultValue = "true")
public boolean useCurrentTimestamp;

/**
* Whether to set the modification time (last modified time) of the files put by Jib in the image to the actual
* build time. Otherwise, the modification time will be set to the Unix epoch (00:00:00, January 1st, 1970 in UTC).
*
* If the modification time is constant (flag is set to false so Unix epoch is used) across two consecutive builds,
* the docker layer sha256 digest will be different only if the actual files added by Jib to the docker layer were
* changed. More exactly, having 2 consecutive builds will generate different docker layers only if the actual
* content of the files within the docker layer was changed.
*
* If the current timestamp is used the sha256 digest of the docker layer will always be different even if the
* content of the files didn't change.
*/
@ConfigItem(defaultValue = "true")
public boolean useCurrentTimestampFileModification;
}
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,15 @@ private JibContainerBuilder createContainerBuilderFromFastJar(String baseJvmImag

try {
Instant now = Instant.now();
Instant modificationTime = jibConfig.useCurrentTimestampFileModification ? now : Instant.EPOCH;

JibContainerBuilder jibContainerBuilder = Jib
.from(toRegistryImage(ImageReference.parse(baseJvmImage), jibConfig.baseRegistryUsername,
jibConfig.baseRegistryPassword));
if (fastChangingLibPaths.isEmpty()) {
// just create a layer with the entire lib structure intact
addLayer(jibContainerBuilder, Collections.singletonList(componentsPath.resolve(JarResultBuildStep.LIB)),
workDirInContainer, "fast-jar-lib", isMutableJar, now);
workDirInContainer, "fast-jar-lib", isMutableJar, modificationTime);
} else {
// we need to manually create each layer
// the idea here is that the fast changing libraries are created in a later layer, thus when they do change,
Expand Down Expand Up @@ -486,15 +487,15 @@ private JibContainerBuilder createContainerBuilderFromFastJar(String baseJvmImag
.resolve(JarResultBuildStep.DEPLOYMENT_LIB);
addLayer(jibContainerBuilder, Collections.singletonList(deploymentPath),
workDirInContainer.resolve(JarResultBuildStep.LIB),
"fast-jar-deployment-libs", true, now);
"fast-jar-deployment-libs", true, modificationTime);
}

AbsoluteUnixPath libsMainPath = workDirInContainer.resolve(JarResultBuildStep.LIB)
.resolve(JarResultBuildStep.MAIN);
addLayer(jibContainerBuilder, nonFastChangingLibPaths, libsMainPath, "fast-jar-normal-libs",
isMutableJar, now);
isMutableJar, modificationTime);
addLayer(jibContainerBuilder, new ArrayList<>(fastChangingLibPaths), libsMainPath, "fast-jar-changing-libs",
isMutableJar, now);
isMutableJar, modificationTime);
}

if (appCDSResult.isPresent()) {
Expand All @@ -512,15 +513,15 @@ private JibContainerBuilder createContainerBuilderFromFastJar(String baseJvmImag
componentsPath.resolve(JarResultBuildStep.QUARKUS_RUN_JAR),
workDirInContainer.resolve(JarResultBuildStep.QUARKUS_RUN_JAR),
isMutableJar ? REMOTE_DEV_FILE_PERMISSIONS : DEFAULT_FILE_PERMISSIONS,
now,
modificationTime,
isMutableJar ? DEFAULT_BASE_IMAGE_USER : "")
.build());
}

addLayer(jibContainerBuilder, Collections.singletonList(componentsPath.resolve(JarResultBuildStep.APP)),
workDirInContainer, "fast-jar-quarkus-app", isMutableJar, now);
workDirInContainer, "fast-jar-quarkus-app", isMutableJar, modificationTime);
addLayer(jibContainerBuilder, Collections.singletonList(componentsPath.resolve(JarResultBuildStep.QUARKUS)),
workDirInContainer, "fast-jar-quarkus", isMutableJar, now);
workDirInContainer, "fast-jar-quarkus", isMutableJar, modificationTime);
if (JibConfig.DEFAULT_WORKING_DIR.equals(jibConfig.workingDirectory)) {
// this layer ensures that the working directory is writeable
// see https://github.com/GoogleContainerTools/jib/issues/1270
Expand All @@ -530,7 +531,7 @@ private JibContainerBuilder createContainerBuilderFromFastJar(String baseJvmImag
Files.createTempDirectory("jib"),
AbsoluteUnixPath.get(jibConfig.workingDirectory),
FilePermissions.DEFAULT_FOLDER_PERMISSIONS,
now, DEFAULT_BASE_IMAGE_USER))
modificationTime, DEFAULT_BASE_IMAGE_USER))
.build());
}
if (isMutableJar) {
Expand All @@ -541,13 +542,13 @@ private JibContainerBuilder createContainerBuilderFromFastJar(String baseJvmImag
Files.createTempDirectory("jib"),
workDirInContainer.resolve("dev"),
REMOTE_DEV_FOLDER_PERMISSIONS,
now, DEFAULT_BASE_IMAGE_USER))
modificationTime, DEFAULT_BASE_IMAGE_USER))
.addEntry(
new FileEntry(
componentsPath.resolve(JarResultBuildStep.QUARKUS_APP_DEPS),
workDirInContainer.resolve(JarResultBuildStep.QUARKUS_APP_DEPS),
REMOTE_DEV_FOLDER_PERMISSIONS,
now, DEFAULT_BASE_IMAGE_USER))
modificationTime, DEFAULT_BASE_IMAGE_USER))
.build());
}

Expand Down

0 comments on commit 4d3fd0b

Please sign in to comment.