Skip to content

Commit

Permalink
[jib-cli-jar] Add option to specify creation time of container (#3050)
Browse files Browse the repository at this point in the history
* add option to specify creation time of container
  • Loading branch information
mpeddada1 authored Feb 12, 2021
1 parent adac824 commit 73a6395
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* the License.
*/

package com.google.cloud.tools.jib.cli.buildfile;
package com.google.cloud.tools.jib.cli;

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;

/** Helper class to convert various strings in a buildfile to Instants. */
class Instants {
public class Instants {
/**
* Parses a time string into Instant. The string must be time in milliseconds since unix epoch or
* an iso8601 datetime.
Expand All @@ -31,7 +31,7 @@ class Instants {
* @param fieldName name of field being parsed (for error messaging)
* @return Instant value of parsed time
*/
static Instant fromMillisOrIso8601(String time, String fieldName) {
public static Instant fromMillisOrIso8601(String time, String fieldName) {
try {
return Instant.ofEpochMilli(Long.parseLong(time));
} catch (NumberFormatException nfe) {
Expand Down
21 changes: 21 additions & 0 deletions jib-cli/src/main/java/com/google/cloud/tools/jib/cli/Jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -150,6 +151,14 @@ public class Jar implements Callable<Integer> {
"Entrypoint for container. Overrides the default entrypoint, example: --entrypoint='custom entrypoint'")
private List<String> entrypoint = Collections.emptyList();

@CommandLine.Option(
names = "--creation-time",
paramLabel = "<creation-time>",
description =
"The creation time of the container in milliseconds since epoch or iso8601 format. Overrides the default (1970-01-01T00:00:00Z)")
@SuppressWarnings("NullAway.Init") // initialized by picocli
private String creationTime;

@Override
public Integer call() {
try {
Expand Down Expand Up @@ -263,4 +272,16 @@ public List<String> getProgramArguments() {
public List<String> getEntrypoint() {
return entrypoint;
}

/**
* Returns {@link Instant} representing creation time of container.
*
* @return an optional creation time
*/
public Optional<Instant> getCreationTime() {
if (creationTime != null) {
return Optional.of(Instants.fromMillisOrIso8601(creationTime, "creationTime"));
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath;
import com.google.cloud.tools.jib.api.buildplan.ImageFormat;
import com.google.cloud.tools.jib.api.buildplan.Port;
import com.google.cloud.tools.jib.cli.Instants;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.cloud.tools.jib.api.buildplan.FilePermissions;
import com.google.cloud.tools.jib.cli.Instants;
import java.time.Instant;
import java.util.Optional;
import javax.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static JibContainerBuilder toJibContainerBuilder(
.setProgramArguments(jarOptions.getProgramArguments());
jarOptions.getUser().ifPresent(containerBuilder::setUser);
jarOptions.getFormat().ifPresent(containerBuilder::setFormat);
jarOptions.getCreationTime().ifPresent(containerBuilder::setCreationTime);

return containerBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* the License.
*/

package com.google.cloud.tools.jib.cli.buildfile;
package com.google.cloud.tools.jib.cli;

import java.time.Instant;
import org.junit.Assert;
Expand Down
32 changes: 32 additions & 0 deletions jib-cli/src/test/java/com/google/cloud/tools/jib/cli/JarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.nio.file.Paths;
import java.time.Instant;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.apache.commons.lang3.ArrayUtils;
Expand Down Expand Up @@ -64,6 +65,7 @@ public void testParse_missingRequiredParams_jarfile() {
public void testParse_defaults() {
Jar jarCommand = CommandLine.populateCommand(new Jar(), "-t", "test-image-ref", "my-app.jar");
CommonCliOptions commonCliOptions = jarCommand.commonCliOptions;

assertThat(commonCliOptions.getTargetImage()).isEqualTo("test-image-ref");
assertThat(commonCliOptions.getUsernamePassword()).isEmpty();
assertThat(commonCliOptions.getToUsernamePassword()).isEmpty();
Expand All @@ -80,6 +82,17 @@ public void testParse_defaults() {
assertThat(commonCliOptions.isStacktrace()).isFalse();
assertThat(commonCliOptions.isHttpTrace()).isFalse();
assertThat(commonCliOptions.isSerialize()).isFalse();
assertThat(jarCommand.getFrom()).isEmpty();
assertThat(jarCommand.getJvmFlags()).isEmpty();
assertThat(jarCommand.getExposedPorts()).isEmpty();
assertThat(jarCommand.getVolumes()).isEmpty();
assertThat(jarCommand.getEnvironment()).isEmpty();
assertThat(jarCommand.getLabels()).isEmpty();
assertThat(jarCommand.getUser()).isEmpty();
assertThat(jarCommand.getFormat()).hasValue(ImageFormat.Docker);
assertThat(jarCommand.getProgramArguments()).isEmpty();
assertThat(jarCommand.getEntrypoint()).isEmpty();
assertThat(jarCommand.getCreationTime()).isEmpty();
}

@Test
Expand Down Expand Up @@ -518,6 +531,25 @@ public void testParse_entrypoint() {
assertThat(jarCommand.getEntrypoint()).isEqualTo(ImmutableList.of("java", "-cp", "myClass"));
}

@Test
public void testParse_creationTime_milliseconds() {
Jar jarCommand =
CommandLine.populateCommand(
new Jar(), "--target=test-image-ref", "--creation-time=23", "my-app.jar");
assertThat(jarCommand.getCreationTime()).hasValue(Instant.ofEpochMilli(23));
}

@Test
public void testParse_creationTime_iso8601() {
Jar jarCommand =
CommandLine.populateCommand(
new Jar(),
"--target=test-image-ref",
"--creation-time=2011-12-03T22:42:05Z",
"my-app.jar");
assertThat(jarCommand.getCreationTime()).hasValue(Instant.parse("2011-12-03T22:42:05Z"));
}

@Test
public void testValidate_nameMissingFail() {
Jar jarCommand =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ public void testToJibContainerBuilder_optionalParameters()
Mockito.when(mockJarCommand.getProgramArguments()).thenReturn(ImmutableList.of("arg1"));
Mockito.when(mockJarCommand.getEntrypoint())
.thenReturn(ImmutableList.of("custom", "entrypoint"));
Mockito.when(mockJarCommand.getCreationTime())
.thenReturn(Optional.of(Instant.ofEpochSecond(5)));

JibContainerBuilder containerBuilder =
JarFiles.toJibContainerBuilder(
Expand All @@ -272,5 +274,6 @@ public void testToJibContainerBuilder_optionalParameters()
assertThat(buildPlan.getFormat()).isEqualTo(ImageFormat.OCI);
assertThat(buildPlan.getCmd()).isEqualTo(ImmutableList.of("arg1"));
assertThat(buildPlan.getEntrypoint()).isEqualTo(ImmutableList.of("custom", "entrypoint"));
assertThat(buildPlan.getCreationTime()).isEqualTo(Instant.ofEpochSecond(5));
}
}

0 comments on commit 73a6395

Please sign in to comment.