Skip to content

Commit

Permalink
Use Gradle Property and Provider to enable lazy evaluation for jib.co…
Browse files Browse the repository at this point in the history
…ntainer.environment (GoogleContainerTools#4067)
  • Loading branch information
Paul van der Bles committed Oct 4, 2024
1 parent a3c0c5b commit 570150b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Contributions to this project must be accompanied by a Contributor License
Agreement. You (or your employer) retain the copyright to your contribution;
this simply gives us permission to use and redistribute your contributions as
part of the project. Head over to <https://cla.developers.google.com/> to see
part of the project. Head over to <https://cla.developers.google.com/> to see
your current agreements on file or to sign a new one.

You generally only need to submit a CLA once, so if you've already submitted one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class ContainerParameters {

private final ListProperty<String> jvmFlags;
private Map<String, String> environment = Collections.emptyMap();
private MapProperty<String, String> environment;
private ListProperty<String> entrypoint;
private List<String> extraClasspath = Collections.emptyList();
private boolean expandClasspathDependencies;
Expand All @@ -64,6 +64,7 @@ public ContainerParameters(ObjectFactory objectFactory) {
mainClass = objectFactory.property(String.class);
jvmFlags = objectFactory.listProperty(String.class);
entrypoint = objectFactory.listProperty(String.class);
environment = objectFactory.mapProperty(String.class, String.class).empty();
}

@Input
Expand Down Expand Up @@ -109,18 +110,18 @@ public void setJvmFlags(Provider<List<String>> jvmFlags) {

@Input
@Optional
public Map<String, String> getEnvironment() {
if (System.getProperty(PropertyNames.CONTAINER_ENVIRONMENT) != null) {
return ConfigurationPropertyValidator.parseMapProperty(
System.getProperty(PropertyNames.CONTAINER_ENVIRONMENT));
public MapProperty<String, String> getEnvironment() {
String environmentProperty = System.getProperty(PropertyNames.CONTAINER_ENVIRONMENT);
if (environmentProperty != null) {
Map<String, String> parsedLabels =
ConfigurationPropertyValidator.parseMapProperty(environmentProperty);
if (!parsedLabels.equals(environment.get())) {
environment.set(parsedLabels);
}
}
return environment;
}

public void setEnvironment(Map<String, String> environment) {
this.environment = environment;
}

@Input
@Optional
public List<String> getExtraClasspath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public String getAppRoot() {

@Override
public Map<String, String> getEnvironment() {
return jibExtension.getContainer().getEnvironment();
return jibExtension.getContainer().getEnvironment().get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class GradleRawConfigurationTest {

@Mock private MapProperty<String, String> labels;

@Mock private MapProperty<String, String> environment;

@Test
public void testGetters() {
JibExtension jibExtension = Mockito.mock(JibExtension.class);
Expand Down Expand Up @@ -85,8 +87,7 @@ public void testGetters() {
Mockito.when(containerParameters.getAppRoot()).thenReturn("/app/root");
Mockito.when(containerParameters.getArgs()).thenReturn(Arrays.asList("--log", "info"));
Mockito.when(containerParameters.getEntrypoint()).thenReturn(Arrays.asList("java", "Main"));
Mockito.when(containerParameters.getEnvironment())
.thenReturn(new HashMap<>(ImmutableMap.of("currency", "dollar")));
Mockito.when(environment.get()).thenReturn(Collections.singletonMap("currency", "dollar"));
Mockito.when(containerParameters.getJvmFlags()).thenReturn(Arrays.asList("-cp", "."));
Mockito.when(labels.get()).thenReturn(Collections.singletonMap("unit", "cm"));
Mockito.when(containerParameters.getLabels()).thenReturn(labels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void testToTags_containsEmptyTag() {
@Test
public void testContainer() {
assertThat(testJibExtension.getContainer().getJvmFlags()).isEmpty();
assertThat(testJibExtension.getContainer().getEnvironment()).isEmpty();
assertThat(testJibExtension.getContainer().getEnvironment().get()).isEmpty();
assertThat(testJibExtension.getContainer().getExtraClasspath()).isEmpty();
assertThat(testJibExtension.getContainer().getExpandClasspathDependencies()).isFalse();
assertThat(testJibExtension.getContainer().getMainClass()).isNull();
Expand All @@ -192,7 +192,6 @@ public void testContainer() {
testJibExtension.container(
container -> {
container.setJvmFlags(Arrays.asList("jvmFlag1", "jvmFlag2"));
container.setEnvironment(ImmutableMap.of("var1", "value1", "var2", "value2"));
container.setEntrypoint(Arrays.asList("foo", "bar", "baz"));
container.setExtraClasspath(Arrays.asList("/d1", "/d2", "/d3"));
container.setExpandClasspathDependencies(true);
Expand All @@ -207,9 +206,6 @@ public void testContainer() {
ContainerParameters container = testJibExtension.getContainer();
assertThat(container.getEntrypoint()).containsExactly("foo", "bar", "baz").inOrder();
assertThat(container.getJvmFlags()).containsExactly("jvmFlag1", "jvmFlag2").inOrder();
assertThat(container.getEnvironment())
.containsExactly("var1", "value1", "var2", "value2")
.inOrder();
assertThat(container.getExtraClasspath()).containsExactly("/d1", "/d2", "/d3").inOrder();
assertThat(testJibExtension.getContainer().getExpandClasspathDependencies()).isTrue();
assertThat(testJibExtension.getContainer().getMainClass()).isEqualTo("mainClass");
Expand Down Expand Up @@ -465,7 +461,7 @@ public void testProperties() {
.containsExactly("entry1", "entry2", "entry3")
.inOrder();
System.setProperty("jib.container.environment", "env1=val1,env2=val2");
assertThat(testJibExtension.getContainer().getEnvironment())
assertThat(testJibExtension.getContainer().getEnvironment().get())
.containsExactly("env1", "val1", "env2", "val2")
.inOrder();
System.setProperty("jib.container.extraClasspath", "/d1,/d2,/d3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,14 @@ public void testLazyEvalForLabels() {
"labels contain values [firstkey:updated-first-label, secondKey:updated-second-label]");
}

@Test
public void testLazyEvalForEnvironment() {
BuildResult showLabels = testProject.build("showenvironment", "-Djib.console=plain");
assertThat(showLabels.getOutput())
.contains(
"environment contains values [var1:val1updated, var2:val2updated]");
}

@Test
public void testLazyEvalForEntryPoint() {
BuildResult showEntrypoint = testProject.build("showentrypoint", "-Djib.console=plain");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ dependencies {
project.ext.value = 'original'
project.ext.jibCreationTime = '1970-01-23T00:23:42Z'
project.ext.jibFilesModificationTime = '1970-01-23T01:23:42Z'
project.ext.jibEnvrionment = [
"var1": "val1",
"var2": "val2"
]

project.afterEvaluate {
project.ext.value = 'updated'
project.ext.getCustomPermissions = { -> return ['/updated': '755'] }
project.ext.jibCreationTime = '2022-07-19T10:23:42Z'
project.ext.jibFilesModificationTime = '2022-07-19T11:23:42Z'
project.ext.jibEnvrionment = [
"var1": "val1updated",
"var2": "val2updated"
]
}

jib {
Expand All @@ -38,6 +46,7 @@ jib {
]
}
entrypoint = project.provider { [project.ext.value] }
environment = project.provider { project.ext.jibEnvrionment }
creationTime = project.provider { project.ext.jibCreationTime }
filesModificationTime = project.provider { project.ext.jibFilesModificationTime }
mainClass = project.provider { project.ext.value }
Expand Down Expand Up @@ -82,3 +91,8 @@ tasks.register('showJvmFlags') {
List<String> prop = project.extensions.jib.container.jvmFlags
println('jvmFlags value ' + prop)
}

tasks.register('showenvironment') {
Map<String, String> prop = project.extensions.getByName('jib')['container']['environment'].get()
println('environment contains values ' + prop)
}

0 comments on commit 570150b

Please sign in to comment.