Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add container creation timestamp configuration #1888

Merged
merged 17 commits into from
Aug 9, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
Expand Down Expand Up @@ -172,6 +173,14 @@ public void buildDocker()
+ ex.getInvalidFilesModificationTime(),
ex);

} catch (InvalidCreationTimeException ex) {
throw new GradleException(
"container.creationTime should be an ISO 8601 date-time (see "
+ "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH_PLUS_SECOND\", "
+ "\"USE_CURRENT_TIMESTAMP\"): "
+ ex.getInvalidCreationTime(),
ex);

} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new GradleException(
HelpfulSuggestions.forIncompatibleBaseImageJavaVesionForGradle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
Expand Down Expand Up @@ -152,6 +153,14 @@ public void buildImage()
+ ex.getInvalidFilesModificationTime(),
ex);

} catch (InvalidCreationTimeException ex) {
throw new GradleException(
"container.creationTime should be an ISO 8601 date-time (see "
+ "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH_PLUS_SECOND\", "
+ "\"USE_CURRENT_TIMESTAMP\"): "
+ ex.getInvalidCreationTime(),
ex);

} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new GradleException(
HelpfulSuggestions.forIncompatibleBaseImageJavaVesionForGradle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
Expand Down Expand Up @@ -170,6 +171,14 @@ public void buildTar()
+ ex.getInvalidFilesModificationTime(),
ex);

} catch (InvalidCreationTimeException ex) {
throw new GradleException(
"container.creationTime should be an ISO 8601 date-time (see "
+ "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH_PLUS_SECOND\", "
+ "\"USE_CURRENT_TIMESTAMP\"): "
+ ex.getInvalidCreationTime(),
ex);

} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new GradleException(
HelpfulSuggestions.forIncompatibleBaseImageJavaVesionForGradle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ContainerParameters {
@Nullable private String user;
@Nullable private String workingDirectory;
private String filesModificationTime = "EPOCH_PLUS_SECOND";
private String creationTime = "EPOCH_PLUS_SECOND";

@Input
@Optional
Expand Down Expand Up @@ -261,4 +262,17 @@ public String getFilesModificationTime() {
public void setFilesModificationTime(String filesModificationTime) {
this.filesModificationTime = filesModificationTime;
}

@Input
@Optional
public String getCreationTime() {
if (System.getProperty(PropertyNames.CONTAINER_CREATION_TIME) != null) {
return System.getProperty(PropertyNames.CONTAINER_CREATION_TIME);
}
return creationTime;
}

public void setCreationTime(String creationTime) {
this.creationTime = creationTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public String getFilesModificationTime() {
return jibExtension.getContainer().getFilesModificationTime();
}

@Override
public String getCreationTime() {
return jibExtension.getContainer().getCreationTime();
}

@Override
public List<Path> getExtraDirectories() {
return jibExtension.getExtraDirectories().getPaths();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ static void checkDeprecatedUsage(JibExtension jibExtension, Logger logger) {
"You cannot configure both 'jib.extraDirectory.path' and 'jib.extraDirectories.paths'");
}
}

if (jibExtension.getContainer().getUseCurrentTimestamp()) {
if (!jibExtension.getContainer().getCreationTime().equals("EPOCH_PLUS_SECOND")) {
throw new IllegalArgumentException(
"You cannot configure both 'jib.container.useCurrentTimestamp' and "
+ "'jib.container.creationTime'");
}
logger.warn(
"'jib.container.useCurrentTimestamp' is deprecated; use 'jib.container.creationTime' "
+ "with the value 'USE_CURRENT_TIMESTAMP' instead");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
public class TaskCommonTest {

@Mock private JibExtension jibExtension;
@Mock private ContainerParameters containerParameters;
@Mock private Logger logger;

@Before
Expand All @@ -46,6 +47,7 @@ public void setUp() {
Assert.assertNull(System.getProperty("jib.extraDirectory.permissions"));
Assert.assertNull(System.getProperty("jib.extraDirectories.paths"));
Assert.assertNull(System.getProperty("jib.extraDirectories.permissions"));
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
Mockito.when(jibExtension.getContainer()).thenReturn(containerParameters);
}

@After
Expand Down Expand Up @@ -131,6 +133,32 @@ public void testCheckDeprecatedUsage_extraDirectoryAndExtraDirectoriesConfigured
}
}

@Test
public void testCheckDeprecatedUsage_useCurrentTimestampConfigured() {
Mockito.when(containerParameters.getUseCurrentTimestamp()).thenReturn(true);
Mockito.when(containerParameters.getCreationTime()).thenReturn("EPOCH_PLUS_SECOND");
TaskCommon.checkDeprecatedUsage(jibExtension, logger);
Mockito.verify(logger)
.warn(
"'jib.container.useCurrentTimestamp' is deprecated; use 'jib.container.creationTime' "
+ "with the value 'USE_CURRENT_TIMESTAMP' instead");
}

@Test
public void testCheckDeprecatedUsage_useCurrentTimestampAndCreationTimeConfigured() {
Mockito.when(containerParameters.getUseCurrentTimestamp()).thenReturn(true);
Mockito.when(containerParameters.getCreationTime()).thenReturn("USE_CURRENT_TIMESTAMP");
try {
TaskCommon.checkDeprecatedUsage(jibExtension, logger);
Assert.fail();
} catch (IllegalArgumentException ex) {
Assert.assertEquals(
"You cannot configure both 'jib.container.useCurrentTimestamp' and "
+ "'jib.container.creationTime'",
ex.getMessage());
}
}

@Test
public void testGetWarTask_normalJavaProject() {
Project project = ProjectBuilder.builder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
Expand Down Expand Up @@ -97,6 +98,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
HelpfulSuggestions.forDockerNotInstalled(HELPFUL_SUGGESTIONS_PREFIX));
}

MojoCommon.checkUseCurrentTimestampDeprecation(this);

try {
RawConfiguration mavenRawConfiguration = new MavenRawConfiguration(this);
MavenProjectProperties projectProperties =
Expand Down Expand Up @@ -170,6 +173,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
+ ex.getInvalidFilesModificationTime(),
ex);

} catch (InvalidCreationTimeException ex) {
throw new MojoExecutionException(
"<container><creationTime> should be an ISO 8601 date-time (see "
+ "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH_PLUS_SECOND\", "
+ "\"USE_CURRENT_TIMESTAMP\"): "
+ ex.getInvalidCreationTime(),
ex);

} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new MojoExecutionException(
HelpfulSuggestions.forIncompatibleBaseImageJavaVesionForMaven(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
Expand Down Expand Up @@ -73,6 +74,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

MojoCommon.checkUseCurrentTimestampDeprecation(this);

// Validates 'format'.
if (Arrays.stream(ImageFormat.values()).noneMatch(value -> value.name().equals(getFormat()))) {
throw new MojoFailureException(
Expand Down Expand Up @@ -164,6 +167,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
+ ex.getInvalidFilesModificationTime(),
ex);

} catch (InvalidCreationTimeException ex) {
throw new MojoExecutionException(
"<container><creationTime> should be an ISO 8601 date-time (see "
+ "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH_PLUS_SECOND\", "
+ "\"USE_CURRENT_TIMESTAMP\"): "
+ ex.getInvalidCreationTime(),
ex);

} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new MojoExecutionException(
HelpfulSuggestions.forIncompatibleBaseImageJavaVesionForMaven(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.JibBuildRunner;
Expand Down Expand Up @@ -71,6 +72,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

MojoCommon.checkUseCurrentTimestampDeprecation(this);

try {
RawConfiguration mavenRawConfiguration = new MavenRawConfiguration(this);
MavenProjectProperties projectProperties =
Expand Down Expand Up @@ -142,6 +145,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
+ ex.getInvalidFilesModificationTime(),
ex);

} catch (InvalidCreationTimeException ex) {
throw new MojoExecutionException(
"<container><creationTime> should be an ISO 8601 date-time (see "
+ "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH_PLUS_SECOND\", "
+ "\"USE_CURRENT_TIMESTAMP\"): "
+ ex.getInvalidCreationTime(),
ex);

} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new MojoExecutionException(
HelpfulSuggestions.forIncompatibleBaseImageJavaVesionForMaven(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ public static class ContainerParameters {
@Nullable @Parameter private String workingDirectory;

@Parameter private String filesModificationTime = "EPOCH_PLUS_SECOND";

@Parameter private String creationTime = "EPOCH_PLUS_SECOND";
}

/** Configuration for the {@code extraDirectories} parameter. */
Expand Down Expand Up @@ -363,6 +365,7 @@ AuthConfiguration getTargetImageAuth() {
*
* @return {@code true} if the build should use the current timestamp, {@code false} if not
*/
@Deprecated
boolean getUseCurrentTimestamp() {
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
String property = getProperty(PropertyNames.CONTAINER_USE_CURRENT_TIMESTAMP);
if (property != null) {
Expand Down Expand Up @@ -558,6 +561,19 @@ String getFilesModificationTime() {
return container.filesModificationTime;
}

/**
* Gets the configured container creation time value.
*
* @return the configured container creation time value
*/
String getCreationTime() {
String property = getProperty(PropertyNames.CONTAINER_CREATION_TIME);
if (property != null) {
return property;
}
return container.creationTime;
}

/**
* Gets the list of configured extra directory paths.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public String getFilesModificationTime() {
return jibPluginConfiguration.getFilesModificationTime();
}

@Override
public String getCreationTime() {
return jibPluginConfiguration.getCreationTime();
}

@Override
public List<Path> getExtraDirectories() {
return MojoCommon.getExtraDirectories(jibPluginConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public class MojoCommon {
@VisibleForTesting
public static final String REQUIRED_VERSION_PROPERTY_NAME = "jib.requiredVersion";

@Deprecated
static void checkUseCurrentTimestampDeprecation(JibPluginConfiguration jibPluginConfiguration) {
if (jibPluginConfiguration.getUseCurrentTimestamp()) {
if (!jibPluginConfiguration.getCreationTime().equals("EPOCH_PLUS_SECOND")) {
throw new IllegalArgumentException(
"You cannot configure both <container><useCurrentTimestamp> and "
+ "<container><creationTime>");
}
jibPluginConfiguration
.getLog()
.warn(
"<container><useCurrentTimestamp> is deprecated; use <container><creationTime> with "
+ "the value CURRENT_TIME instead");
TadCordle marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Gets the list of extra directory paths from a {@link JibPluginConfiguration}. Returns {@code
* (project dir)/src/main/jib} by default if not configured.
Expand Down
Loading