-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Removes SourceFilesConfiguration in favor of list of LayerConfiguration in BuildConfiguration. #516
Changes from 18 commits
b468528
69e6b57
84f5ef6
fd6daaf
780332a
e3d835d
fd46ee2
4736f4d
00176fe
36791ac
fcde23c
06e5b46
0bc2536
7162e85
7c28ffe
e871c14
77339ff
614dbc7
5a8247a
bc46b0b
72abd98
0ee1e86
3596206
d7da59d
69764a0
19db093
bbe18c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,16 +21,23 @@ | |
import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException; | ||
import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; | ||
import com.google.cloud.tools.jib.cache.Caches; | ||
import com.google.cloud.tools.jib.configuration.LayerConfiguration; | ||
import com.google.cloud.tools.jib.frontend.ExposedPortsParser; | ||
import com.google.cloud.tools.jib.frontend.JavaEntrypointBuilder; | ||
import com.google.cloud.tools.jib.image.ImageReference; | ||
import com.google.cloud.tools.jib.image.InvalidImageReferenceException; | ||
import com.google.cloud.tools.jib.registry.LocalRegistry; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.io.Resources; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.stream.Stream; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
|
@@ -42,25 +49,36 @@ | |
/** Integration tests for {@link BuildSteps}. */ | ||
public class BuildStepsIntegrationTest { | ||
|
||
/** Lists the files in the {@code resourcePath} resources directory. */ | ||
private static ImmutableList<Path> getFilesList(String resourcePath) | ||
throws URISyntaxException, IOException { | ||
try (Stream<Path> fileStream = | ||
Files.list(Paths.get(Resources.getResource(resourcePath).toURI()))) { | ||
return fileStream.collect(ImmutableList.toImmutableList()); | ||
} | ||
} | ||
|
||
@ClassRule public static LocalRegistry localRegistry = new LocalRegistry(5000); | ||
|
||
private static final TestBuildLogger logger = new TestBuildLogger(); | ||
|
||
@Rule public TemporaryFolder temporaryCacheDirectory = new TemporaryFolder(); | ||
|
||
@Rule public TemporaryFolder temporaryTarOutput = new TemporaryFolder(); | ||
@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's harmless to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should definitely have |
||
|
||
private SourceFilesConfiguration sourceFilesConfiguration; | ||
private ImmutableList<LayerConfiguration> fakeLayerConfigurations; | ||
|
||
@Before | ||
public void setUp() throws IOException, URISyntaxException { | ||
sourceFilesConfiguration = | ||
TestSourceFilesConfiguration.builder() | ||
.withClasses() | ||
.withDependencies() | ||
.withSnapshotDependencies() | ||
.withResources() | ||
.build(); | ||
fakeLayerConfigurations = | ||
ImmutableList.of( | ||
LayerConfiguration.builder() | ||
.addEntry(getFilesList("application/dependencies"), "/app/libs/") | ||
.build(), | ||
LayerConfiguration.builder() | ||
.addEntry(getFilesList("application/resources"), "/app/resources/") | ||
.build(), | ||
LayerConfiguration.builder() | ||
.addEntry(getFilesList("application/classes"), "/app/classes/") | ||
.build()); | ||
} | ||
|
||
@Test | ||
|
@@ -72,12 +90,16 @@ public void testSteps_forBuildToDockerRegistry() | |
BuildConfiguration.builder(logger) | ||
.setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest")) | ||
.setTargetImage(ImageReference.of("localhost:5000", "testimage", "testtag")) | ||
.setMainClass("HelloWorld") | ||
.setJavaArguments(Collections.singletonList("An argument.")) | ||
.setExposedPorts( | ||
ExposedPortsParser.parse(Arrays.asList("1000", "2000-2002/tcp", "3000/udp"))) | ||
.setAllowHttp(true) | ||
.setLayerConfigurations(fakeLayerConfigurations) | ||
.setEntrypoint( | ||
JavaEntrypointBuilder.makeDefaultEntrypoint( | ||
Collections.emptyList(), "HelloWorld")) | ||
.build()); | ||
buildImageSteps.run(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we running this 3 times now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, removing. |
||
|
||
long lastTime = System.nanoTime(); | ||
buildImageSteps.run(); | ||
|
@@ -110,9 +132,12 @@ public void testSteps_forBuildToDockerRegistry_dockerHubBaseImage() | |
BuildConfiguration.builder(logger) | ||
.setBaseImage(ImageReference.parse("openjdk:8-jre-alpine")) | ||
.setTargetImage(ImageReference.of("localhost:5000", "testimage", "testtag")) | ||
.setMainClass("HelloWorld") | ||
.setJavaArguments(Collections.singletonList("An argument.")) | ||
.setAllowHttp(true) | ||
.setLayerConfigurations(fakeLayerConfigurations) | ||
.setEntrypoint( | ||
JavaEntrypointBuilder.makeDefaultEntrypoint( | ||
Collections.emptyList(), "HelloWorld")) | ||
.build()) | ||
.run(); | ||
|
||
|
@@ -130,16 +155,17 @@ public void testSteps_forBuildToDockerDaemon() | |
BuildConfiguration.builder(logger) | ||
.setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest")) | ||
.setTargetImage(ImageReference.of(null, "testdocker", null)) | ||
.setMainClass("HelloWorld") | ||
.setJavaArguments(Collections.singletonList("An argument.")) | ||
.setExposedPorts( | ||
ExposedPortsParser.parse(Arrays.asList("1000", "2000-2002/tcp", "3000/udp"))) | ||
.setLayerConfigurations(fakeLayerConfigurations) | ||
.setEntrypoint( | ||
JavaEntrypointBuilder.makeDefaultEntrypoint(Collections.emptyList(), "HelloWorld")) | ||
.build(); | ||
|
||
Path cacheDirectory = temporaryCacheDirectory.newFolder().toPath(); | ||
Path cacheDirectory = temporaryFolder.newFolder().toPath(); | ||
BuildSteps.forBuildToDockerDaemon( | ||
buildConfiguration, | ||
sourceFilesConfiguration, | ||
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false)) | ||
.run(); | ||
|
||
|
@@ -164,16 +190,17 @@ public void testSteps_forBuildToTarball() | |
BuildConfiguration.builder(logger) | ||
.setBaseImage(ImageReference.of("gcr.io", "distroless/java", "latest")) | ||
.setTargetImage(ImageReference.of(null, "testtar", null)) | ||
.setMainClass("HelloWorld") | ||
.setJavaArguments(Collections.singletonList("An argument.")) | ||
.setLayerConfigurations(fakeLayerConfigurations) | ||
.setEntrypoint( | ||
JavaEntrypointBuilder.makeDefaultEntrypoint(Collections.emptyList(), "HelloWorld")) | ||
.build(); | ||
|
||
Path outputPath = temporaryTarOutput.newFolder().toPath().resolve("test.tar"); | ||
Path cacheDirectory = temporaryCacheDirectory.newFolder().toPath(); | ||
Path outputPath = temporaryFolder.newFolder().toPath().resolve("test.tar"); | ||
Path cacheDirectory = temporaryFolder.newFolder().toPath(); | ||
BuildSteps.forBuildToTar( | ||
outputPath, | ||
buildConfiguration, | ||
sourceFilesConfiguration, | ||
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false)) | ||
.run(); | ||
|
||
|
@@ -183,10 +210,8 @@ public void testSteps_forBuildToTarball() | |
} | ||
|
||
private BuildSteps getBuildSteps(BuildConfiguration buildConfiguration) throws IOException { | ||
Path cacheDirectory = temporaryCacheDirectory.newFolder().toPath(); | ||
Path cacheDirectory = temporaryFolder.newFolder().toPath(); | ||
return BuildSteps.forBuildToDockerRegistry( | ||
buildConfiguration, | ||
sourceFilesConfiguration, | ||
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false)); | ||
buildConfiguration, new Caches.Initializer(cacheDirectory, false, cacheDirectory, false)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,16 +49,15 @@ public static class Builder { | |
@Nullable private ImageReference targetImageReference; | ||
@Nullable private String targetImageCredentialHelperName; | ||
@Nullable private RegistryCredentials knownTargetRegistryCredentials; | ||
@Nullable private String mainClass; | ||
private ImmutableList<String> javaArguments = ImmutableList.of(); | ||
private ImmutableList<String> jvmFlags = ImmutableList.of(); | ||
private ImmutableMap<String, String> environmentMap = ImmutableMap.of(); | ||
private ImmutableList<Port> exposedPorts = ImmutableList.of(); | ||
private Class<? extends BuildableManifestTemplate> targetFormat = V22ManifestTemplate.class; | ||
@Nullable private CacheConfiguration applicationLayersCacheConfiguration; | ||
@Nullable private CacheConfiguration baseImageLayersCacheConfiguration; | ||
private boolean allowHttp = false; | ||
@Nullable private LayerConfiguration extraFilesLayerConfiguration; | ||
private ImmutableList<LayerConfiguration> layerConfigurations = ImmutableList.of(); | ||
private ImmutableList<String> entrypoint = ImmutableList.of(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very much a nit, so feel free to ignore, but for organization it'd be nice to put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (This same ordering happens in a bunch of other places too, but I didn't want to overload the comments.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, maybe we can leave that to another PR that can set up a nice ordering pattern for all of these parameters. |
||
|
||
private BuildLogger buildLogger; | ||
|
||
|
@@ -98,11 +97,6 @@ public Builder setKnownTargetRegistryCredentials( | |
return this; | ||
} | ||
|
||
public Builder setMainClass(@Nullable String mainClass) { | ||
this.mainClass = mainClass; | ||
return this; | ||
} | ||
|
||
public Builder setJavaArguments(@Nullable List<String> javaArguments) { | ||
if (javaArguments != null) { | ||
Preconditions.checkArgument(!javaArguments.contains(null)); | ||
|
@@ -111,14 +105,6 @@ public Builder setJavaArguments(@Nullable List<String> javaArguments) { | |
return this; | ||
} | ||
|
||
public Builder setJvmFlags(@Nullable List<String> jvmFlags) { | ||
if (jvmFlags != null) { | ||
Preconditions.checkArgument(!jvmFlags.contains(null)); | ||
this.jvmFlags = ImmutableList.copyOf(jvmFlags); | ||
} | ||
return this; | ||
} | ||
|
||
public Builder setEnvironment(@Nullable Map<String, String> environmentMap) { | ||
if (environmentMap != null) { | ||
Preconditions.checkArgument( | ||
|
@@ -178,14 +164,13 @@ public Builder setAllowHttp(boolean allowHttp) { | |
} | ||
|
||
/** | ||
* Sets the {@link LayerConfiguration} for an extra layer. | ||
* Sets the layers to build. | ||
* | ||
* @param extraFilesLayerConfiguration the layer configuration for the extra layer | ||
* @param layerConfigurations the configurations for the layers | ||
* @return this | ||
*/ | ||
public Builder setExtraFilesLayerConfiguration( | ||
@Nullable LayerConfiguration extraFilesLayerConfiguration) { | ||
this.extraFilesLayerConfiguration = extraFilesLayerConfiguration; | ||
public Builder setLayerConfigurations(List<LayerConfiguration> layerConfigurations) { | ||
this.layerConfigurations = ImmutableList.copyOf(layerConfigurations); | ||
return this; | ||
} | ||
|
||
|
@@ -200,6 +185,20 @@ public Builder setCreationTime(Instant creationTime) { | |
return this; | ||
} | ||
|
||
/** | ||
* Sets the container entrypoint. | ||
* | ||
* @param entrypoint the tokenized command to run when the container starts | ||
* @return this | ||
*/ | ||
public Builder setEntrypoint(@Nullable List<String> entrypoint) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the point of allowing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of allowing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If so, why not just clear the entrypoint if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes a lot of sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or, actually - I think we should probably have the semantics be like:
|
||
if (entrypoint != null) { | ||
Preconditions.checkArgument(!entrypoint.contains(null)); | ||
this.entrypoint = ImmutableList.copyOf(entrypoint); | ||
} | ||
return this; | ||
} | ||
|
||
/** @return the corresponding build configuration */ | ||
public BuildConfiguration build() { | ||
// Validates the parameters. | ||
|
@@ -210,13 +209,10 @@ public BuildConfiguration build() { | |
if (targetImageReference == null) { | ||
errorMessages.add("target image is required but not set"); | ||
} | ||
if (mainClass == null) { | ||
errorMessages.add("main class is required but not set"); | ||
} | ||
|
||
switch (errorMessages.size()) { | ||
case 0: // No errors | ||
if (baseImageReference == null || targetImageReference == null || mainClass == null) { | ||
if (baseImageReference == null || targetImageReference == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we error if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm intending it to be possibly be empty to allow for propagation of entrypoint from base images in the future (when this is used as a library). |
||
throw new IllegalStateException("Required fields should not be null"); | ||
} | ||
if (baseImageReference.usesDefaultTag()) { | ||
|
@@ -234,16 +230,15 @@ public BuildConfiguration build() { | |
targetImageReference, | ||
targetImageCredentialHelperName, | ||
knownTargetRegistryCredentials, | ||
mainClass, | ||
javaArguments, | ||
jvmFlags, | ||
environmentMap, | ||
exposedPorts, | ||
targetFormat, | ||
applicationLayersCacheConfiguration, | ||
baseImageLayersCacheConfiguration, | ||
allowHttp, | ||
extraFilesLayerConfiguration); | ||
layerConfigurations, | ||
entrypoint); | ||
|
||
case 1: | ||
throw new IllegalStateException(errorMessages.get(0)); | ||
|
@@ -294,16 +289,15 @@ public static Builder builder(BuildLogger buildLogger) { | |
private final ImageReference targetImageReference; | ||
@Nullable private final String targetImageCredentialHelperName; | ||
@Nullable private final RegistryCredentials knownTargetRegistryCredentials; | ||
private final String mainClass; | ||
private final ImmutableList<String> javaArguments; | ||
private final ImmutableList<String> jvmFlags; | ||
private final ImmutableMap<String, String> environmentMap; | ||
private final ImmutableList<Port> exposedPorts; | ||
private final Class<? extends BuildableManifestTemplate> targetFormat; | ||
@Nullable private final CacheConfiguration applicationLayersCacheConfiguration; | ||
@Nullable private final CacheConfiguration baseImageLayersCacheConfiguration; | ||
private final boolean allowHttp; | ||
@Nullable private final LayerConfiguration extraFilesLayerConfiguration; | ||
private final ImmutableList<LayerConfiguration> layerConfigurations; | ||
private final ImmutableList<String> entrypoint; | ||
|
||
/** Instantiate with {@link Builder#build}. */ | ||
private BuildConfiguration( | ||
|
@@ -315,16 +309,15 @@ private BuildConfiguration( | |
ImageReference targetImageReference, | ||
@Nullable String targetImageCredentialHelperName, | ||
@Nullable RegistryCredentials knownTargetRegistryCredentials, | ||
String mainClass, | ||
ImmutableList<String> javaArguments, | ||
ImmutableList<String> jvmFlags, | ||
ImmutableMap<String, String> environmentMap, | ||
ImmutableList<Port> exposedPorts, | ||
Class<? extends BuildableManifestTemplate> targetFormat, | ||
@Nullable CacheConfiguration applicationLayersCacheConfiguration, | ||
@Nullable CacheConfiguration baseImageLayersCacheConfiguration, | ||
boolean allowHttp, | ||
@Nullable LayerConfiguration extraFilesLayerConfiguration) { | ||
ImmutableList<LayerConfiguration> layerConfigurations, | ||
ImmutableList<String> entrypoint) { | ||
this.buildLogger = buildLogger; | ||
this.creationTime = creationTime; | ||
this.baseImageReference = baseImageReference; | ||
|
@@ -333,16 +326,15 @@ private BuildConfiguration( | |
this.targetImageReference = targetImageReference; | ||
this.targetImageCredentialHelperName = targetImageCredentialHelperName; | ||
this.knownTargetRegistryCredentials = knownTargetRegistryCredentials; | ||
this.mainClass = mainClass; | ||
this.javaArguments = javaArguments; | ||
this.jvmFlags = jvmFlags; | ||
this.environmentMap = environmentMap; | ||
this.exposedPorts = exposedPorts; | ||
this.targetFormat = targetFormat; | ||
this.applicationLayersCacheConfiguration = applicationLayersCacheConfiguration; | ||
this.baseImageLayersCacheConfiguration = baseImageLayersCacheConfiguration; | ||
this.allowHttp = allowHttp; | ||
this.extraFilesLayerConfiguration = extraFilesLayerConfiguration; | ||
this.layerConfigurations = layerConfigurations; | ||
this.entrypoint = entrypoint; | ||
} | ||
|
||
public BuildLogger getBuildLogger() { | ||
|
@@ -405,18 +397,10 @@ public RegistryCredentials getKnownTargetRegistryCredentials() { | |
return knownTargetRegistryCredentials; | ||
} | ||
|
||
public String getMainClass() { | ||
return mainClass; | ||
} | ||
|
||
public ImmutableList<String> getJavaArguments() { | ||
return javaArguments; | ||
} | ||
|
||
public ImmutableList<String> getJvmFlags() { | ||
return jvmFlags; | ||
} | ||
|
||
public ImmutableMap<String, String> getEnvironment() { | ||
return environmentMap; | ||
} | ||
|
@@ -459,12 +443,20 @@ public boolean getAllowHttp() { | |
} | ||
|
||
/** | ||
* Gets the {@link LayerConfiguration} for an extra layer. | ||
* Gets the configurations for building the layers. | ||
* | ||
* @return the layer configuration | ||
* @return the list of layer configurations | ||
*/ | ||
@Nullable | ||
public LayerConfiguration getExtraFilesLayerConfiguration() { | ||
return extraFilesLayerConfiguration; | ||
public ImmutableList<LayerConfiguration> getLayerConfigurations() { | ||
return layerConfigurations; | ||
} | ||
|
||
/** | ||
* Gets the container entrypoint. | ||
* | ||
* @return the list of entrypoint tokens | ||
*/ | ||
public ImmutableList<String> getEntrypoint() { | ||
return entrypoint; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is specifically for resources maybe it should be renamed to
getResourceFilesList
.