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

Changes the builder steps to use the new ncache.Cache. #1047

Merged
merged 42 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
db9222a
Has Cache use last modified time as metadata.
coollog Sep 25, 2018
ebf6fb4
Changes builder to use the new cache mechanism.
coollog Sep 25, 2018
9e9e20c
Finishes
coollog Sep 25, 2018
c6c20eb
Fixes tests.
coollog Sep 25, 2018
2c3e862
Merge branch 'edit-ncache' into use-ncache
coollog Sep 25, 2018
db2de20
Working
coollog Sep 25, 2018
9a7405d
Deletes cache.
coollog Sep 25, 2018
ab522f8
feedback
coollog Sep 26, 2018
b2b7210
Merge branch 'edit-ncache' into use-ncache
coollog Sep 26, 2018
f67e6dd
Fixing
coollog Sep 26, 2018
7868308
Rewords javadoc.
coollog Sep 26, 2018
e5b5755
Merge branch 'edit-ncache' into use-ncache
coollog Sep 26, 2018
5b00b23
Stores temporary cache files under cache directory.
coollog Sep 26, 2018
32a87a8
Merge branch 'fix-ncache-write' into use-ncache
coollog Sep 26, 2018
3a12c18
format
coollog Sep 26, 2018
a54d577
testing - revert this
coollog Sep 26, 2018
742f8c2
hmm
coollog Sep 26, 2018
d8f8a50
Resorts to copying for file systems that don't support renames.
coollog Sep 26, 2018
14c2f7d
Merge branch 'edit-ncache' into use-ncache
coollog Sep 26, 2018
da94d8b
javadoc
coollog Sep 26, 2018
b3001c8
resorts to copy
coollog Sep 26, 2018
8bb6ba7
Merge branch 'fix-ncache-write' into use-ncache
coollog Sep 26, 2018
1d10522
splitting
coollog Sep 26, 2018
346a61c
Splits CacheWrite into CompressedCacheWrite and UncompressedCacheWrite.
coollog Sep 26, 2018
003f215
Merge branch 'split-cachewrite' into use-ncache
coollog Sep 26, 2018
c57e299
Fixes it.
coollog Sep 26, 2018
aa3d311
Merge branch 'split-cachewrite' into use-ncache
coollog Sep 26, 2018
9e62d30
updates javadoc
coollog Sep 26, 2018
ab2ca12
Removes CompressedCacheWrite.
coollog Sep 26, 2018
f8a011e
Merge branch 'split-cachewrite' into use-ncache
coollog Sep 26, 2018
9faf66f
Fixes maven.
coollog Sep 26, 2018
3ad588d
Merge branch 'master' into split-cachewrite
coollog Sep 26, 2018
b7381db
Merge branch 'split-cachewrite' into use-ncache
coollog Sep 26, 2018
4d25141
feedback
coollog Sep 27, 2018
6374195
feedback
coollog Sep 27, 2018
8dbf44b
Merge branch 'split-cachewrite' into use-ncache
coollog Sep 27, 2018
2afc629
fixes
coollog Sep 27, 2018
7d7a628
Merge branch 'master' into use-ncache
coollog Sep 27, 2018
437baa3
Merge branch 'master' into use-ncache
coollog Sep 27, 2018
94bd28a
feedback
coollog Sep 27, 2018
040ed33
Merge branch 'master' into use-ncache
coollog Sep 27, 2018
130f2d7
Merge branch 'master' into use-ncache
coollog Sep 27, 2018
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 @@ -17,11 +17,9 @@
package com.google.cloud.tools.jib.builder;

import com.google.cloud.tools.jib.Command;
import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException;
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.BuildConfiguration;
import com.google.cloud.tools.jib.configuration.CacheConfiguration;
import com.google.cloud.tools.jib.configuration.CacheDirectoryCreationException;
import com.google.cloud.tools.jib.configuration.ContainerConfiguration;
import com.google.cloud.tools.jib.configuration.ImageConfiguration;
import com.google.cloud.tools.jib.configuration.LayerConfiguration;
Expand Down Expand Up @@ -123,10 +121,10 @@ public void setUp() throws IOException, URISyntaxException {

@Test
public void testSteps_forBuildToDockerRegistry()
throws IOException, InterruptedException, CacheMetadataCorruptedException, ExecutionException,
CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
throws IOException, InterruptedException, ExecutionException,
CacheDirectoryCreationException {
BuildSteps buildImageSteps =
getBuildSteps(
BuildSteps.forBuildToDockerRegistry(
getBuildConfigurationBuilder(
ImageReference.of("gcr.io", "distroless/java", "latest"),
ImageReference.of("localhost:5000", "testimage", "testtag"))
Expand All @@ -148,10 +146,10 @@ public void testSteps_forBuildToDockerRegistry()

@Test
public void testSteps_forBuildToDockerRegistry_multipleTags()
throws IOException, InterruptedException, CacheMetadataCorruptedException, ExecutionException,
CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
throws IOException, InterruptedException, ExecutionException,
CacheDirectoryCreationException {
BuildSteps buildImageSteps =
getBuildSteps(
BuildSteps.forBuildToDockerRegistry(
getBuildConfigurationBuilder(
ImageReference.of("gcr.io", "distroless/java", "latest"),
ImageReference.of("localhost:5000", "testimage", "testtag"))
Expand Down Expand Up @@ -188,10 +186,9 @@ public void testSteps_forBuildToDockerRegistry_multipleTags()

@Test
public void testSteps_forBuildToDockerRegistry_dockerHubBaseImage()
throws InvalidImageReferenceException, IOException, InterruptedException, ExecutionException,
CacheDirectoryCreationException, CacheMetadataCorruptedException,
CacheDirectoryNotOwnedException {
getBuildSteps(
throws InvalidImageReferenceException, IOException, InterruptedException,
CacheDirectoryCreationException, ExecutionException {
BuildSteps.forBuildToDockerRegistry(
getBuildConfigurationBuilder(
ImageReference.parse("openjdk:8-jre-alpine"),
ImageReference.of("localhost:5000", "testimage", "testtag"))
Expand All @@ -206,19 +203,15 @@ public void testSteps_forBuildToDockerRegistry_dockerHubBaseImage()

@Test
public void testSteps_forBuildToDockerDaemon()
throws IOException, InterruptedException, CacheMetadataCorruptedException, ExecutionException,
CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
throws IOException, InterruptedException, ExecutionException,
CacheDirectoryCreationException {
String imageReference = "testdocker";
BuildConfiguration buildConfiguration =
getBuildConfigurationBuilder(
ImageReference.of("gcr.io", "distroless/java", "latest"),
ImageReference.of(null, imageReference, null))
.build();
Path cacheDirectory = temporaryFolder.newFolder().toPath();
BuildSteps.forBuildToDockerDaemon(
buildConfiguration,
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false))
.run();
BuildSteps.forBuildToDockerDaemon(buildConfiguration).run();

assertDockerInspect(imageReference);
Assert.assertEquals(
Expand All @@ -227,20 +220,16 @@ public void testSteps_forBuildToDockerDaemon()

@Test
public void testSteps_forBuildToDockerDaemon_multipleTags()
throws IOException, InterruptedException, CacheMetadataCorruptedException, ExecutionException,
CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
throws IOException, InterruptedException, ExecutionException,
CacheDirectoryCreationException {
String imageReference = "testdocker";
BuildConfiguration buildConfiguration =
getBuildConfigurationBuilder(
ImageReference.of("gcr.io", "distroless/java", "latest"),
ImageReference.of(null, imageReference, null))
.setAdditionalTargetImageTags(ImmutableSet.of("testtag2", "testtag3"))
.build();
Path cacheDirectory = temporaryFolder.newFolder().toPath();
BuildSteps.forBuildToDockerDaemon(
buildConfiguration,
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false))
.run();
BuildSteps.forBuildToDockerDaemon(buildConfiguration).run();

assertDockerInspect(imageReference);
Assert.assertEquals(
Expand All @@ -257,34 +246,23 @@ public void testSteps_forBuildToDockerDaemon_multipleTags()

@Test
public void testSteps_forBuildToTarball()
throws IOException, InterruptedException, CacheMetadataCorruptedException, ExecutionException,
CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
throws IOException, InterruptedException, ExecutionException,
CacheDirectoryCreationException {
BuildConfiguration buildConfiguration =
getBuildConfigurationBuilder(
ImageReference.of("gcr.io", "distroless/java", "latest"),
ImageReference.of(null, "testtar", null))
.build();
Path outputPath = temporaryFolder.newFolder().toPath().resolve("test.tar");
Path cacheDirectory = temporaryFolder.newFolder().toPath();
BuildSteps.forBuildToTar(
outputPath,
buildConfiguration,
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false))
.run();
BuildSteps.forBuildToTar(outputPath, buildConfiguration).run();

new Command("docker", "load", "--input", outputPath.toString()).run();
Assert.assertEquals(
"Hello, world. An argument.\n", new Command("docker", "run", "--rm", "testtar").run());
}

private BuildSteps getBuildSteps(BuildConfiguration buildConfiguration) throws IOException {
Path cacheDirectory = temporaryFolder.newFolder().toPath();
return BuildSteps.forBuildToDockerRegistry(
buildConfiguration, new Caches.Initializer(cacheDirectory, false, cacheDirectory, false));
}

private BuildConfiguration.Builder getBuildConfigurationBuilder(
ImageReference baseImage, ImageReference targetImage) {
ImageReference baseImage, ImageReference targetImage) throws IOException {
ImageConfiguration baseImageConfiguration = ImageConfiguration.builder(baseImage).build();
ImageConfiguration targetImageConfiguration = ImageConfiguration.builder(targetImage).build();
ContainerConfiguration containerConfiguration =
Expand All @@ -298,10 +276,13 @@ private BuildConfiguration.Builder getBuildConfigurationBuilder(
ExposedPortsParser.parse(Arrays.asList("1000", "2000-2002/tcp", "3000/udp")))
.setLabels(ImmutableMap.of("key1", "value1", "key2", "value2"))
.build();
Path cacheDirectory = temporaryFolder.newFolder().toPath();
return BuildConfiguration.builder()
.setBaseImageConfiguration(baseImageConfiguration)
.setTargetImageConfiguration(targetImageConfiguration)
.setContainerConfiguration(containerConfiguration)
.setBaseImageLayersCacheConfiguration(CacheConfiguration.forPath(cacheDirectory))
.setApplicationLayersCacheConfiguration(CacheConfiguration.forPath(cacheDirectory))
.setAllowInsecureRegistries(true)
.setLayerConfigurations(fakeLayerConfigurations)
.setToolName("jib-integration-test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import com.google.cloud.tools.jib.image.json.V21ManifestTemplate;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.OutputStream;
import java.security.DigestException;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;

/** Integration tests for {@link BlobPuller}. */
public class BlobPullerIntegrationTest {
Expand All @@ -56,14 +54,13 @@ public void testPull() throws IOException, RegistryException, InterruptedExcepti
// Pulls a layer BLOB of the busybox image.
CountingDigestOutputStream layerOutputStream =
new CountingDigestOutputStream(ByteStreams.nullOutputStream());
registryClient.pullBlob(realDigest, layerOutputStream);
registryClient.pullBlob(realDigest).writeTo(layerOutputStream);

Assert.assertEquals(realDigest, layerOutputStream.toBlobDescriptor().getDigest());
}

@Test
public void testPull_unknownBlob()
throws RegistryException, IOException, DigestException, InterruptedException {
public void testPull_unknownBlob() throws IOException, DigestException, InterruptedException {
localRegistry.pullAndPushToLocal("busybox", "busybox");
DescriptorDigest nonexistentDigest =
DescriptorDigest.fromHash(
Expand All @@ -74,10 +71,13 @@ public void testPull_unknownBlob()
RegistryClient.factory(EVENT_EMITTER, "localhost:5000", "busybox")
.setAllowInsecureRegistries(true)
.newRegistryClient();
registryClient.pullBlob(nonexistentDigest, Mockito.mock(OutputStream.class));
registryClient.pullBlob(nonexistentDigest).writeTo(ByteStreams.nullOutputStream());
Assert.fail("Trying to pull nonexistent blob should have errored");

} catch (RegistryErrorException ex) {
} catch (IOException ex) {
if (!(ex.getCause() instanceof RegistryErrorException)) {
throw ex;
coollog marked this conversation as resolved.
Show resolved Hide resolved
}
Assert.assertThat(
ex.getMessage(),
CoreMatchers.containsString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// TODO: Move to com.google.cloud.tools.jib once that package is cleaned up.

import com.google.cloud.tools.jib.configuration.BuildConfiguration;
import com.google.cloud.tools.jib.configuration.CacheDirectoryCreationException;
import com.google.cloud.tools.jib.configuration.ContainerConfiguration;
import com.google.cloud.tools.jib.configuration.LayerConfiguration;
import com.google.cloud.tools.jib.configuration.Port;
Expand Down Expand Up @@ -302,7 +303,8 @@ public JibContainerBuilder addLabel(String key, String value) {
}

@VisibleForTesting
BuildConfiguration toBuildConfiguration(TargetImage targetImage) {
BuildConfiguration toBuildConfiguration(TargetImage targetImage)
throws IOException, CacheDirectoryCreationException {
BuildConfiguration.Builder buildConfigurationBuilder = BuildConfiguration.builder();

buildConfigurationBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@
package com.google.cloud.tools.jib.builder;

import com.google.cloud.tools.jib.builder.steps.StepsRunner;
import com.google.cloud.tools.jib.cache.Cache;
import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException;
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.BuildConfiguration;
import com.google.cloud.tools.jib.event.events.LogEvent;
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.ExecutionException;

Expand All @@ -46,15 +40,12 @@ private interface StepsRunnerConsumer {
* All the steps to build an image to a Docker registry.
*
* @param buildConfiguration the configuration parameters for the build
* @param cachesInitializer the {@link Caches.Initializer} used to setup the cache
* @return a new {@link BuildSteps} for building to a registry
*/
public static BuildSteps forBuildToDockerRegistry(
BuildConfiguration buildConfiguration, Caches.Initializer cachesInitializer) {
public static BuildSteps forBuildToDockerRegistry(BuildConfiguration buildConfiguration) {
return new BuildSteps(
DESCRIPTION_FOR_DOCKER_REGISTRY,
buildConfiguration,
cachesInitializer,
stepsRunner ->
stepsRunner
.runRetrieveTargetRegistryCredentialsStep()
Expand All @@ -75,15 +66,12 @@ public static BuildSteps forBuildToDockerRegistry(
* All the steps to build to Docker daemon
*
* @param buildConfiguration the configuration parameters for the build
* @param cachesInitializer the {@link Caches.Initializer} used to setup the cache
* @return a new {@link BuildSteps} for building to a Docker daemon
*/
public static BuildSteps forBuildToDockerDaemon(
BuildConfiguration buildConfiguration, Caches.Initializer cachesInitializer) {
public static BuildSteps forBuildToDockerDaemon(BuildConfiguration buildConfiguration) {
return new BuildSteps(
DESCRIPTION_FOR_DOCKER_DAEMON,
buildConfiguration,
cachesInitializer,
stepsRunner ->
stepsRunner
.runPullBaseImageStep()
Expand All @@ -100,17 +88,12 @@ public static BuildSteps forBuildToDockerDaemon(
*
* @param outputPath the path to output the tarball to
* @param buildConfiguration the configuration parameters for the build
* @param cachesInitializer the {@link Caches.Initializer} used to setup the cache
* @return a new {@link BuildSteps} for building a tarball
*/
public static BuildSteps forBuildToTar(
Path outputPath,
BuildConfiguration buildConfiguration,
Caches.Initializer cachesInitializer) {
public static BuildSteps forBuildToTar(Path outputPath, BuildConfiguration buildConfiguration) {
return new BuildSteps(
DESCRIPTION_FOR_TARBALL,
buildConfiguration,
cachesInitializer,
stepsRunner ->
stepsRunner
.runPullBaseImageStep()
Expand All @@ -124,48 +107,32 @@ public static BuildSteps forBuildToTar(

private final String description;
private final BuildConfiguration buildConfiguration;
private final Caches.Initializer cachesInitializer;
private final StepsRunnerConsumer stepsRunnerConsumer;

/**
* @param description a description of what the steps do
* @param buildConfiguration the configuration parameters for the build
* @param stepsRunnerConsumer accepts a {@link StepsRunner} by running the necessary steps
*/
private BuildSteps(
String description,
BuildConfiguration buildConfiguration,
Caches.Initializer cachesInitializer,
StepsRunnerConsumer stepsRunnerConsumer) {
this.description = description;
this.buildConfiguration = buildConfiguration;
this.cachesInitializer = cachesInitializer;
this.stepsRunnerConsumer = stepsRunnerConsumer;
}

public BuildConfiguration getBuildConfiguration() {
return buildConfiguration;
}

public void run()
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException,
CacheDirectoryNotOwnedException, CacheDirectoryCreationException {
public void run() throws InterruptedException, ExecutionException {
buildConfiguration.getEventEmitter().emit(LogEvent.lifecycle(""));

try (TimerEventEmitter ignored =
new TimerEventEmitter(buildConfiguration.getEventEmitter(), description)) {
try (Caches caches = cachesInitializer.init()) {
Cache baseImageLayersCache = caches.getBaseCache();
Cache applicationLayersCache = caches.getApplicationCache();

StepsRunner stepsRunner =
new StepsRunner(buildConfiguration, baseImageLayersCache, applicationLayersCache);
stepsRunnerConsumer.accept(stepsRunner);

// Writes the cached layers to the cache metadata.
baseImageLayersCache.addCachedLayersToMetadata(stepsRunner.getCachedBaseImageLayers());
applicationLayersCache.addCachedLayersWithMetadataToMetadata(
stepsRunner.getCachedApplicationLayers());
}
stepsRunnerConsumer.accept(new StepsRunner(buildConfiguration));
}

if (buildConfiguration.getContainerConfiguration() != null) {
Expand Down
Loading