Skip to content

Commit

Permalink
Add support for defining an entrypoint (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
briandealwis authored Aug 30, 2018
1 parent 223c031 commit 84a5002
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ private static void addIfNotEmpty(
private final ImmutableList<CopyDirective> copyDirectives;

@Nullable private String baseImage;
private List<String> jvmFlags = Collections.emptyList();
private String mainClass = "";
private List<String> entrypoint = Collections.emptyList();
private List<String> javaArguments = Collections.emptyList();
private List<String> exposedPorts = Collections.emptyList();
private Map<String, String> labels = Collections.emptyMap();
Expand Down Expand Up @@ -150,24 +149,13 @@ public JavaDockerContextGenerator setBaseImage(String baseImage) {
}

/**
* Sets the JVM flags used in the {@code ENTRYPOINT}.
* Sets the entrypoint to be used as the {@code ENTRYPOINT}.
*
* @param jvmFlags the jvm flags.
* @param entrypoint the entrypoint.
* @return this
*/
public JavaDockerContextGenerator setJvmFlags(List<String> jvmFlags) {
this.jvmFlags = jvmFlags;
return this;
}

/**
* Sets the main class used in the {@code ENTRYPOINT}.
*
* @param mainClass the name of the main class.
* @return this
*/
public JavaDockerContextGenerator setMainClass(String mainClass) {
this.mainClass = mainClass;
public JavaDockerContextGenerator setEntrypoint(List<String> entrypoint) {
this.entrypoint = entrypoint;
return this;
}

Expand Down Expand Up @@ -291,9 +279,7 @@ String makeDockerfile() throws JsonProcessingException {

dockerfile
.append("\nENTRYPOINT ")
.append(
objectMapper.writeValueAsString(
JavaEntrypointConstructor.makeDefaultEntrypoint(jvmFlags, mainClass)))
.append(objectMapper.writeValueAsString(entrypoint))
.append("\nCMD ")
.append(objectMapper.writeValueAsString(javaArguments));
return dockerfile.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ public void testMakeDockerfile() throws IOException {
String dockerfile =
new JavaDockerContextGenerator(mockJavaLayerConfigurations)
.setBaseImage(expectedBaseImage)
.setJvmFlags(expectedJvmFlags)
.setMainClass(expectedMainClass)
.setEntrypoint(
JavaEntrypointConstructor.makeDefaultEntrypoint(
expectedJvmFlags, expectedMainClass))
.setJavaArguments(expectedJavaArguments)
.setExposedPorts(exposedPorts)
.setLabels(expectedLabels)
Expand Down
1 change: 1 addition & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
### Added

- `container.labels` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751))
- `container.entrypoint` configuration parameter to set the entrypoint ([#579](https://github.com/GoogleContainerTools/jib/issues/579))
- `history` to layer metadata ([#875](https://github.com/GoogleContainerTools/jib/issues/875))
- Propagates working directory from the base image ([#902](https://github.com/GoogleContainerTools/jib/pull/902))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ContainerParameters {

private boolean useCurrentTimestamp = false;
private List<String> jvmFlags = Collections.emptyList();
private List<String> entrypoint = Collections.emptyList();
@Nullable private String mainClass;
private List<String> args = Collections.emptyList();
private ImageFormat format = ImageFormat.Docker;
Expand All @@ -50,6 +51,16 @@ public void setUseCurrentTimestamp(boolean useCurrentTimestamp) {
this.useCurrentTimestamp = useCurrentTimestamp;
}

@Input
@Optional
public List<String> getEntrypoint() {
return entrypoint;
}

public void setEntrypoint(List<String> entrypoint) {
this.entrypoint = entrypoint;
}

@Input
@Optional
public List<String> getJvmFlags() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

import com.google.cloud.tools.jib.frontend.ExposedPortsParser;
import com.google.cloud.tools.jib.frontend.JavaDockerContextGenerator;
import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor;
import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator;
import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions;
import com.google.common.base.Preconditions;
import com.google.common.io.InsecureRecursiveDeleteException;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import javax.annotation.Nullable;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
Expand Down Expand Up @@ -107,18 +109,25 @@ public void generateDockerContext() {
GradleProjectProperties gradleProjectProperties =
GradleProjectProperties.getForProject(
getProject(), gradleJibLogger, jibExtension.getExtraDirectoryPath());
String mainClass = gradleProjectProperties.getMainClass(jibExtension);
String targetDir = getTargetDir();

List<String> entrypoint = jibExtension.getContainer().getEntrypoint();
if (entrypoint.isEmpty()) {
String mainClass = gradleProjectProperties.getMainClass(jibExtension);
entrypoint =
JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass);
} else if (jibExtension.getMainClass() != null || !jibExtension.getJvmFlags().isEmpty()) {
gradleJibLogger.warn("mainClass and jvmFlags are ignored when entrypoint is specified");
}

try {
// Validate port input, but don't save the output because we don't want the ranges expanded
// here.
ExposedPortsParser.parse(jibExtension.getExposedPorts());

new JavaDockerContextGenerator(gradleProjectProperties.getJavaLayerConfigurations())
.setBaseImage(jibExtension.getBaseImage())
.setJvmFlags(jibExtension.getJvmFlags())
.setMainClass(mainClass)
.setEntrypoint(entrypoint)
.setJavaArguments(jibExtension.getArgs())
.setExposedPorts(jibExtension.getExposedPorts())
.setLabels(jibExtension.getLabels())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers;
import com.google.cloud.tools.jib.registry.RegistryClient;
import java.time.Instant;
import java.util.List;
import javax.annotation.Nullable;
import org.gradle.api.GradleException;

Expand Down Expand Up @@ -85,12 +86,17 @@ static PluginConfigurationProcessor processCommonConfiguration(
ImageConfiguration.builder(baseImage)
.setCredentialRetrievers(defaultCredentialRetrievers.asList());

String mainClass = projectProperties.getMainClass(jibExtension);
List<String> entrypoint = jibExtension.getContainer().getEntrypoint();
if (entrypoint.isEmpty()) {
String mainClass = projectProperties.getMainClass(jibExtension);
entrypoint =
JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass);
} else if (jibExtension.getMainClass() != null || !jibExtension.getJvmFlags().isEmpty()) {
logger.warn("mainClass and jvmFlags are ignored when entrypoint is specified");
}
ContainerConfiguration.Builder containerConfigurationBuilder =
ContainerConfiguration.builder()
.setEntrypoint(
JavaEntrypointConstructor.makeDefaultEntrypoint(
jibExtension.getJvmFlags(), mainClass))
.setEntrypoint(entrypoint)
.setProgramArguments(jibExtension.getArgs())
.setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts()))
.setLabels(jibExtension.getLabels());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,15 @@ public void testContainer() {
testJibExtension.container(
container -> {
container.setJvmFlags(Arrays.asList("jvmFlag1", "jvmFlag2"));
container.setEntrypoint(Arrays.asList("foo", "bar", "baz"));
container.setMainClass("mainClass");
container.setArgs(Arrays.asList("arg1", "arg2", "arg3"));
container.setPorts(Arrays.asList("1000", "2000-2010", "3000"));
container.setLabels(ImmutableMap.of("label1", "value1", "label2", "value2"));
container.setFormat(ImageFormat.OCI);
});
Assert.assertEquals(
Arrays.asList("foo", "bar", "baz"), testJibExtension.getContainer().getEntrypoint());
Assert.assertEquals(
Arrays.asList("jvmFlag1", "jvmFlag2"), testJibExtension.getContainer().getJvmFlags());
Assert.assertEquals("mainClass", testJibExtension.getContainer().getMainClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import com.google.cloud.tools.jib.configuration.ContainerConfiguration;
import com.google.cloud.tools.jib.frontend.JavaLayerConfigurations;
import com.google.cloud.tools.jib.image.InvalidImageReferenceException;
import java.util.Arrays;
import java.util.Collections;
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 PluginConfigurationProcessor}. */
@RunWith(MockitoJUnitRunner.class)
public class PluginConfigurationProcessorTest {

@Mock GradleJibLogger mockGradleJibLogger;
@Mock JibExtension mockJibExtension;
@Mock ImageParameters mockImageParameters;
@Mock ContainerParameters mockContainerParameters;
@Mock GradleProjectProperties mockProjectProperties;

@Before
public void setUp() throws Exception {
Mockito.doReturn("gcr.io/distroless/java").when(mockJibExtension).getBaseImage();
Mockito.doReturn(mockImageParameters).when(mockJibExtension).getFrom();
Mockito.doReturn(new AuthParameters("mock")).when(mockImageParameters).getAuth();
Mockito.doReturn(mockContainerParameters).when(mockJibExtension).getContainer();
Mockito.doReturn(Collections.emptyList()).when(mockContainerParameters).getEntrypoint();

Mockito.doReturn(JavaLayerConfigurations.builder().build())
.when(mockProjectProperties)
.getJavaLayerConfigurations();
Mockito.doReturn("java.lang.Object").when(mockProjectProperties).getMainClass(mockJibExtension);
}

/** Test with our default mocks, which try to mimic the bare Gradle configuration. */
@Test
public void testPluginConfigurationProcessor_defaults() throws InvalidImageReferenceException {
PluginConfigurationProcessor processor =
PluginConfigurationProcessor.processCommonConfiguration(
mockGradleJibLogger, mockJibExtension, mockProjectProperties);
ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build();
Assert.assertEquals(
Arrays.asList(
"java", "-cp", "/app/resources/:/app/classes/:/app/libs/*", "java.lang.Object"),
configuration.getEntrypoint());
Mockito.verifyZeroInteractions(mockGradleJibLogger);
}

@Test
public void testEntrypoint() throws InvalidImageReferenceException {
Mockito.doReturn(Arrays.asList("custom", "entrypoint"))
.when(mockContainerParameters)
.getEntrypoint();

PluginConfigurationProcessor processor =
PluginConfigurationProcessor.processCommonConfiguration(
mockGradleJibLogger, mockJibExtension, mockProjectProperties);
ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build();

Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint());
Mockito.verifyZeroInteractions(mockGradleJibLogger);
}

@Test
public void testEntrypoint_warningOnJvmFlags() throws InvalidImageReferenceException {
Mockito.doReturn(Arrays.asList("custom", "entrypoint"))
.when(mockContainerParameters)
.getEntrypoint();
Mockito.doReturn(Arrays.asList("jvmFlag")).when(mockJibExtension).getJvmFlags();

PluginConfigurationProcessor processor =
PluginConfigurationProcessor.processCommonConfiguration(
mockGradleJibLogger, mockJibExtension, mockProjectProperties);
ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build();

Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint());
Mockito.verify(mockGradleJibLogger)
.warn("mainClass and jvmFlags are ignored when entrypoint is specified");
}

@Test
public void testEntrypoint_warningOnMainclass() throws InvalidImageReferenceException {
Mockito.doReturn(Arrays.asList("custom", "entrypoint"))
.when(mockContainerParameters)
.getEntrypoint();
Mockito.doReturn("java.util.Object").when(mockJibExtension).getMainClass();

PluginConfigurationProcessor processor =
PluginConfigurationProcessor.processCommonConfiguration(
mockGradleJibLogger, mockJibExtension, mockProjectProperties);
ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build();

Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint());
Mockito.verify(mockGradleJibLogger)
.warn("mainClass and jvmFlags are ignored when entrypoint is specified");
}

// TODO should test other behaviours
}
1 change: 1 addition & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
### Added

- `<container><labels>` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751))
- `<container><entrypoint>` configuration parameter to set the entrypoint ([#579](https://github.com/GoogleContainerTools/jib/issues/579))
- `history` to layer metadata ([#875](https://github.com/GoogleContainerTools/jib/issues/875))
- Propagates working directory from the base image ([#902](https://github.com/GoogleContainerTools/jib/pull/902))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

import com.google.cloud.tools.jib.frontend.ExposedPortsParser;
import com.google.cloud.tools.jib.frontend.JavaDockerContextGenerator;
import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor;
import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator;
import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.io.InsecureRecursiveDeleteException;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import javax.annotation.Nullable;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -61,7 +63,14 @@ public void execute() throws MojoExecutionException {

MavenProjectProperties mavenProjectProperties =
MavenProjectProperties.getForProject(getProject(), mavenJibLogger, getExtraDirectory());
String mainClass = mavenProjectProperties.getMainClass(this);

List<String> entrypoint = getEntrypoint();
if (entrypoint.isEmpty()) {
String mainClass = mavenProjectProperties.getMainClass(this);
entrypoint = JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass);
} else if (getMainClass() != null || !getJvmFlags().isEmpty()) {
mavenJibLogger.warn("<mainClass> and <jvmFlags> are ignored when <entrypoint> is specified");
}

try {
// Validate port input, but don't save the output because we don't want the ranges expanded
Expand All @@ -70,8 +79,7 @@ public void execute() throws MojoExecutionException {

new JavaDockerContextGenerator(mavenProjectProperties.getJavaLayerConfigurations())
.setBaseImage(getBaseImage())
.setJvmFlags(getJvmFlags())
.setMainClass(mainClass)
.setEntrypoint(entrypoint)
.setJavaArguments(getArgs())
.setExposedPorts(getExposedPorts())
.setLabels(getLabels())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public static class ContainerParameters {

@Parameter private boolean useCurrentTimestamp = false;

@Parameter private List<String> entrypoint = Collections.emptyList();

@Parameter private List<String> jvmFlags = Collections.emptyList();

@Nullable @Parameter private String mainClass;
Expand Down Expand Up @@ -253,6 +255,10 @@ boolean getUseCurrentTimestamp() {
return container.useCurrentTimestamp;
}

List<String> getEntrypoint() {
return container.entrypoint;
}

List<String> getJvmFlags() {
return container.jvmFlags;
}
Expand Down
Loading

0 comments on commit 84a5002

Please sign in to comment.