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

Convert Gradle config parameters jib.container.environment to use lazy evaluation #4067 #4317

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> parsedEnvironment =
ConfigurationPropertyValidator.parseMapProperty(environmentProperty);
if (!parsedEnvironment.equals(environment.get())) {
environment.set(parsedEnvironment);
}
}
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,8 @@ 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.getEnvironment()).thenReturn(environment);
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 showEnvironment = testProject.build("showenvironment", "-Djib.console=plain");
assertThat(showEnvironment.getOutput())
.contains(
"environment contains values [var1:updated-first-environment-var, var2:updated-second-environment-var]");
}

@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,6 +17,10 @@ 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'
Expand All @@ -38,6 +42,12 @@ jib {
]
}
entrypoint = project.provider { [project.ext.value] }
environment = project.provider {
[
var1 : project.ext.value + '-first-environment-var',
var2: project.ext.value + '-second-environment-var'
]
}
creationTime = project.provider { project.ext.jibCreationTime }
filesModificationTime = project.provider { project.ext.jibFilesModificationTime }
mainClass = project.provider { project.ext.value }
Expand Down Expand Up @@ -82,3 +92,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)
}