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' configuration object to wrap container-specific parameters #421

Merged
merged 11 commits into from
Jun 20, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Blob getContainerConfigurationBlob() {
// Sets the entrypoint.
template.setContainerEntrypoint(image.getEntrypoint());

// Sets the main class arguments.
// Sets the main method arguments.
template.setContainerCmd(image.getJavaArguments());

// Serializes into JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ public void buildDocker() throws InvalidImageReferenceException {

// Asserts required @Input parameters are not null.
Preconditions.checkNotNull(jibExtension);
GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
jibExtension.handleDeprecatedParameters(gradleBuildLogger);

RegistryCredentials knownBaseRegistryCredentials = null;
Authorization fromAuthorization = jibExtension.getFrom().getImageAuthorization();
if (fromAuthorization != null) {
knownBaseRegistryCredentials = new RegistryCredentials("jib.from.auth", fromAuthorization);
}

GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
GradleProjectProperties gradleProjectProperties =
GradleProjectProperties.getForProject(getProject(), gradleBuildLogger);
String mainClass = gradleProjectProperties.getMainClass(jibExtension);
Expand All @@ -101,8 +103,8 @@ public void buildDocker() throws InvalidImageReferenceException {
.setBaseImageCredentialHelperName(jibExtension.getFrom().getCredHelper())
.setKnownBaseRegistryCredentials(knownBaseRegistryCredentials)
.setMainClass(mainClass)
.setJavaArguments(jibExtension.getContainer().getArgs())
.setJvmFlags(jibExtension.getContainer().getJvmFlags())
.setJavaArguments(jibExtension.getArgs())
.setJvmFlags(jibExtension.getJvmFlags())
.build();

// TODO: Instead of disabling logging, have authentication credentials be provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void setTargetImage(String targetImage) {
public void buildImage() throws InvalidImageReferenceException {
// Asserts required @Input parameters are not null.
Preconditions.checkNotNull(jibExtension);
GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
jibExtension.handleDeprecatedParameters(gradleBuildLogger);

if (Strings.isNullOrEmpty(jibExtension.getTargetImage())) {
throw new GradleException(
Expand All @@ -91,7 +93,6 @@ public void buildImage() throws InvalidImageReferenceException {
knownTargetRegistryCredentials = new RegistryCredentials("jib.to.auth", toAuthorization);
}

GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
GradleProjectProperties gradleProjectProperties =
GradleProjectProperties.getForProject(getProject(), gradleBuildLogger);
String mainClass = gradleProjectProperties.getMainClass(jibExtension);
Expand All @@ -105,9 +106,9 @@ public void buildImage() throws InvalidImageReferenceException {
.setTargetImageCredentialHelperName(jibExtension.getTo().getCredHelper())
.setKnownTargetRegistryCredentials(knownTargetRegistryCredentials)
.setMainClass(mainClass)
.setJavaArguments(jibExtension.getContainer().getArgs())
.setJvmFlags(jibExtension.getContainer().getJvmFlags())
.setTargetFormat(jibExtension.getContainer().getFormat())
.setJavaArguments(jibExtension.getArgs())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be left as jibExtension.getContainer().getArgs() since jibExtension.getArgs() (and the like) will be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed jibExtension.getArgs() to return container.getArgs(), and my plan was to tag jibExtension.getArgs() with @Internal instead of removing it in the future. Sort of like how getTargetImage() and getBaseImage() are handled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay that sounds good.

.setJvmFlags(jibExtension.getJvmFlags())
.setTargetFormat(jibExtension.getFormat())
.build();

// TODO: Instead of disabling logging, have authentication credentials be provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* A bean that configures properties of the container run from the image. This is configurable with
* Groovy closures and can be validated when used as a task input.
*/
public class ContainerConfiguration {
public class ContainerParameters {

private List<String> jvmFlags = Collections.emptyList();
@Nullable private String mainClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public void generateDockerContext() {
Preconditions.checkNotNull(jibExtension);

GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
jibExtension.handleDeprecatedParameters(gradleBuildLogger);

GradleProjectProperties gradleProjectProperties =
GradleProjectProperties.getForProject(getProject(), gradleBuildLogger);
String mainClass = gradleProjectProperties.getMainClass(jibExtension);
Expand All @@ -105,9 +107,9 @@ public void generateDockerContext() {
try {
new DockerContextGenerator(gradleProjectProperties.getSourceFilesConfiguration())
.setBaseImage(jibExtension.getBaseImage())
.setJvmFlags(jibExtension.getContainer().getJvmFlags())
.setJvmFlags(jibExtension.getJvmFlags())
.setMainClass(mainClass)
.setJavaArguments(jibExtension.getContainer().getArgs())
.setJavaArguments(jibExtension.getArgs())
.generate(Paths.get(targetDir));

gradleBuildLogger.info("Created Docker context at " + targetDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public String getJarPluginName() {
*/
String getMainClass(JibExtension jibExtension) {
try {
return MainClassFinder.resolveMainClass(jibExtension.getContainer().getMainClass(), this);
return MainClassFinder.resolveMainClass(jibExtension.getMainClass(), this);
} catch (MainClassInferenceException ex) {
throw new GradleException(ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@

package com.google.cloud.tools.jib.gradle;

import com.google.cloud.tools.jib.builder.BuildLogger;
import com.google.cloud.tools.jib.image.ImageFormat;
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
Expand All @@ -42,10 +49,12 @@
* image = ‘gcr.io/gcp-project/my-app:built-with-jib’
* credHelper = ‘ecr-login’
* }
* jvmFlags = [‘-Xms512m’, ‘-Xdebug’]
* mainClass = ‘com.mycompany.myproject.Main’
* args = ['arg1', 'arg2']
* format = OCI
* container {
* jvmFlags = [‘-Xms512m’, ‘-Xdebug’]
* mainClass = ‘com.mycompany.myproject.Main’
* args = ['arg1', 'arg2']
* format = OCI
* }
* }
* }</pre>
*/
Expand All @@ -57,23 +66,75 @@ public class JibExtension {

private final ImageConfiguration from;
private final ImageConfiguration to;
private final ContainerConfiguration container;
private final ContainerParameters container;
private final Property<Boolean> useOnlyProjectCache;

// TODO: Deprecated parameters; remove these 4
private final ListProperty<String> jvmFlags;
private final Property<String> mainClass;
private final ListProperty<String> args;
private final Property<ImageFormat> format;

public JibExtension(Project project) {
ObjectFactory objectFactory = project.getObjects();

from = objectFactory.newInstance(ImageConfiguration.class);
to = objectFactory.newInstance(ImageConfiguration.class);
container = objectFactory.newInstance(ContainerConfiguration.class);
container = objectFactory.newInstance(ContainerParameters.class);

jvmFlags = objectFactory.listProperty(String.class);
mainClass = objectFactory.property(String.class);
args = objectFactory.listProperty(String.class);
format = objectFactory.property(ImageFormat.class);

useOnlyProjectCache = objectFactory.property(Boolean.class);

// Sets defaults.
from.setImage(DEFAULT_FROM_IMAGE);
jvmFlags.set(Collections.emptyList());
args.set(Collections.emptyList());
useOnlyProjectCache.set(DEFAULT_USE_ONLY_PROJECT_CACHE);
}

/**
* Warns about deprecated parameters in use.
*
* @param logger The logger used to print the warnings
*/
void handleDeprecatedParameters(BuildLogger logger) {
StringBuilder deprecatedParams = new StringBuilder();
if (!jvmFlags.get().isEmpty()) {
deprecatedParams.append(" jvmFlags -> container.jvmFlags\n");
if (container.getJvmFlags().isEmpty()) {
container.setJvmFlags(jvmFlags.get());
}
}
if (!Strings.isNullOrEmpty(mainClass.getOrNull())) {
deprecatedParams.append(" mainClass -> container.mainClass\n");
if (Strings.isNullOrEmpty(container.getMainClass())) {
container.setMainClass(mainClass.getOrNull());
}
}
if (!args.get().isEmpty()) {
deprecatedParams.append(" args -> container.args\n");
if (container.getArgs().isEmpty()) {
container.setArgs(args.get());
}
}
if (format.getOrNull() != null) {
deprecatedParams.append(" format -> container.format\n");
container.setFormat(format.get());
}

if (deprecatedParams.length() > 0) {
logger.warn(
"There are deprecated parameters used in the build configuration. Please make the "
+ "following changes to your build.gradle to avoid issues in the future:\n"
+ deprecatedParams
+ "You may also wrap the parameters in a container{} block.");
}
}

public void from(Action<? super ImageConfiguration> action) {
action.execute(from);
}
Expand All @@ -82,10 +143,26 @@ public void to(Action<? super ImageConfiguration> action) {
action.execute(to);
}

public void container(Action<? super ContainerConfiguration> action) {
public void container(Action<? super ContainerParameters> action) {
action.execute(container);
}

public void setJvmFlags(List<String> jvmFlags) {
this.jvmFlags.set(jvmFlags);
}

public void setMainClass(String mainClass) {
this.mainClass.set(mainClass);
}

public void setArgs(List<String> args) {
this.args.set(args);
}

public void setFormat(ImageFormat format) {
this.format.set(format);
}

public void setUseOnlyProjectCache(boolean useOnlyProjectCache) {
this.useOnlyProjectCache.set(useOnlyProjectCache);
}
Expand Down Expand Up @@ -115,10 +192,39 @@ ImageConfiguration getTo() {

@Nested
@Optional
ContainerConfiguration getContainer() {
ContainerParameters getContainer() {
return container;
}

// TODO: Make @Internal (deprecated)
@Input
@Optional
List<String> getJvmFlags() {
return container.getJvmFlags();
}

// TODO: Make @Internal (deprecated)
@Input
@Nullable
@Optional
String getMainClass() {
return container.getMainClass();
}

// TODO: Make @Internal (deprecated)
@Input
@Optional
List<String> getArgs() {
return container.getArgs();
}

// TODO: Make @Internal (deprecated)
@Input
@Optional
Class<? extends BuildableManifestTemplate> getFormat() {
return container.getFormat();
}

@Input
@Optional
boolean getUseOnlyProjectCache() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.tools.jib.gradle;

import com.google.cloud.tools.jib.builder.BuildLogger;
import com.google.cloud.tools.jib.image.ImageFormat;
import com.google.cloud.tools.jib.image.json.OCIManifestTemplate;
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate;
Expand All @@ -26,10 +27,17 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

/** Tests for {@link JibExtension}. */
@RunWith(MockitoJUnitRunner.class)
public class JibExtensionTest {

@Mock private BuildLogger mockLogger;

private JibExtension testJibExtension;

@Before
Expand Down Expand Up @@ -112,4 +120,31 @@ public void testUseOnlyProjectCache() {
testJibExtension.setUseOnlyProjectCache(true);
Assert.assertTrue(testJibExtension.getUseOnlyProjectCache());
}

@Test
public void testHandleDeprecatedParameters() {
testJibExtension.handleDeprecatedParameters(mockLogger);
Mockito.verify(mockLogger, Mockito.never()).warn(Mockito.any());

testJibExtension.setJvmFlags(Arrays.asList("jvmFlag1", "jvmFlag2"));
testJibExtension.setMainClass("mainClass");
testJibExtension.setArgs(Arrays.asList("arg1", "arg2", "arg3"));
testJibExtension.setFormat(ImageFormat.OCI);

testJibExtension.handleDeprecatedParameters(mockLogger);

String expectedOutput =
"There are deprecated parameters used in the build configuration. Please make the "
+ "following changes to your build.gradle to avoid issues in the future:\n"
+ " jvmFlags -> container.jvmFlags\n"
+ " mainClass -> container.mainClass\n"
+ " args -> container.args\n"
+ " format -> container.format\n"
+ "You may also wrap the parameters in a container{} block.";
Mockito.verify(mockLogger).warn(expectedOutput);
Assert.assertEquals(Arrays.asList("jvmFlag1", "jvmFlag2"), testJibExtension.getJvmFlags());
Assert.assertEquals("mainClass", testJibExtension.getMainClass());
Assert.assertEquals(Arrays.asList("arg1", "arg2", "arg3"), testJibExtension.getArgs());
Assert.assertEquals(OCIManifestTemplate.class, testJibExtension.getFormat());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class BuildDockerMojo extends JibPluginConfiguration {

@Override
public void execute() throws MojoExecutionException {
MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog());
handleDeprecatedParameters(mavenBuildLogger);

if (!new DockerClient().isDockerInstalled()) {
throw new MojoExecutionException(HELPFUL_SUGGESTIONS.forDockerNotInstalled());
}
Expand All @@ -68,7 +71,6 @@ public void execute() throws MojoExecutionException {
RegistryCredentials knownBaseRegistryCredentials =
mavenSettingsServerCredentials.retrieve(baseImage.getRegistry());

MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog());
MavenProjectProperties mavenProjectProperties =
MavenProjectProperties.getForProject(getProject(), mavenBuildLogger);
String mainClass = mavenProjectProperties.getMainClass(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class BuildImageMojo extends JibPluginConfiguration {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog());
handleDeprecatedParameters(mavenBuildLogger);

// Validates 'format'.
if (Arrays.stream(ImageFormat.values()).noneMatch(value -> value.name().equals(getFormat()))) {
throw new MojoFailureException(
Expand Down Expand Up @@ -83,7 +86,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
RegistryCredentials knownTargetRegistryCredentials =
mavenSettingsServerCredentials.retrieve(targetImage.getRegistry());

MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog());
MavenProjectProperties mavenProjectProperties =
MavenProjectProperties.getForProject(getProject(), mavenBuildLogger);
String mainClass = mavenProjectProperties.getMainClass(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public class DockerContextMojo extends JibPluginConfiguration {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog());
handleDeprecatedParameters(mavenBuildLogger);

Preconditions.checkNotNull(targetDir);

MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog());
MavenProjectProperties mavenProjectProperties =
MavenProjectProperties.getForProject(getProject(), mavenBuildLogger);
String mainClass = mavenProjectProperties.getMainClass(this);
Expand Down
Loading