Skip to content

Commit 9dd6ab5

Browse files
authored
Adds useOnlyProjectCache jib-maven-plugin configuration. (GoogleContainerTools#134)
1 parent ca95542 commit 9dd6ab5

5 files changed

Lines changed: 101 additions & 36 deletions

File tree

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildImageStepsIntegrationTest.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package com.google.cloud.tools.jib.builder;
1818

1919
import com.google.cloud.tools.jib.Command;
20+
import com.google.cloud.tools.jib.cache.Caches;
2021
import com.google.cloud.tools.jib.image.ImageReference;
2122
import com.google.cloud.tools.jib.registry.LocalRegistry;
23+
import java.nio.file.Path;
2224
import org.junit.Assert;
2325
import org.junit.ClassRule;
2426
import org.junit.Rule;
@@ -44,11 +46,12 @@ public void testSteps() throws Exception {
4446
.setMainClass("HelloWorld")
4547
.build();
4648

49+
Path cacheDirectory = temporaryCacheDirectory.newFolder().toPath();
4750
BuildImageSteps buildImageSteps =
4851
new BuildImageSteps(
4952
buildConfiguration,
5053
sourceFilesConfiguration,
51-
temporaryCacheDirectory.getRoot().toPath());
54+
Caches.newInitializer(cacheDirectory).setBaseCacheDirectory(cacheDirectory));
5255

5356
long lastTime = System.nanoTime();
5457
buildImageSteps.run();

‎jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildImageSteps.java‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919
import com.google.cloud.tools.jib.Timer;
2020
import com.google.cloud.tools.jib.blob.BlobDescriptor;
2121
import com.google.cloud.tools.jib.cache.Cache;
22+
import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException;
2223
import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException;
2324
import com.google.cloud.tools.jib.cache.CachedLayer;
25+
import com.google.cloud.tools.jib.cache.Caches;
2426
import com.google.cloud.tools.jib.http.Authorization;
2527
import com.google.cloud.tools.jib.image.Image;
2628
import com.google.common.util.concurrent.Futures;
2729
import com.google.common.util.concurrent.ListenableFuture;
2830
import com.google.common.util.concurrent.ListeningExecutorService;
2931
import com.google.common.util.concurrent.MoreExecutors;
3032
import java.io.IOException;
31-
import java.nio.file.Path;
3233
import java.util.List;
3334
import java.util.concurrent.ExecutionException;
3435
import java.util.concurrent.Executors;
@@ -40,24 +41,24 @@ public class BuildImageSteps {
4041

4142
private final BuildConfiguration buildConfiguration;
4243
private final SourceFilesConfiguration sourceFilesConfiguration;
43-
private final Path cacheDirectory;
44+
private final Caches.Initializer cachesInitializer;
4445

4546
public BuildImageSteps(
4647
BuildConfiguration buildConfiguration,
4748
SourceFilesConfiguration sourceFilesConfiguration,
48-
Path cacheDirectory) {
49+
Caches.Initializer cachesInitializer) {
4950
this.buildConfiguration = buildConfiguration;
5051
this.sourceFilesConfiguration = sourceFilesConfiguration;
51-
this.cacheDirectory = cacheDirectory;
52+
this.cachesInitializer = cachesInitializer;
5253
}
5354

5455
public BuildConfiguration getBuildConfiguration() {
5556
return buildConfiguration;
5657
}
5758

5859
public void run()
59-
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
60-
IOException {
60+
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException,
61+
CacheDirectoryNotOwnedException {
6162
List<String> entrypoint =
6263
EntrypointBuilder.makeEntrypoint(
6364
sourceFilesConfiguration,
@@ -69,7 +70,10 @@ public void run()
6970
ListeningExecutorService listeningExecutorService =
7071
MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
7172

72-
try (Cache cache = Cache.init(cacheDirectory)) {
73+
try (Caches caches = cachesInitializer.init()) {
74+
Cache baseLayersCache = caches.getBaseCache();
75+
Cache applicationLayersCache = caches.getApplicationCache();
76+
7377
timer2.lap("Setting up credential retrieval");
7478
ListenableFuture<Authorization> retrieveTargetRegistryCredentialsFuture =
7579
listeningExecutorService.submit(
@@ -112,7 +116,7 @@ public void run()
112116
.call(
113117
new PullAndCacheBaseImageLayersStep(
114118
buildConfiguration,
115-
cache,
119+
baseLayersCache,
116120
listeningExecutorService,
117121
authenticatePullFuture,
118122
pullBaseImageFuture),
@@ -134,7 +138,10 @@ public void run()
134138
// Builds the application layers.
135139
List<ListenableFuture<CachedLayer>> buildAndCacheApplicationLayerFutures =
136140
new BuildAndCacheApplicationLayersStep(
137-
buildConfiguration, sourceFilesConfiguration, cache, listeningExecutorService)
141+
buildConfiguration,
142+
sourceFilesConfiguration,
143+
applicationLayersCache,
144+
listeningExecutorService)
138145
.call();
139146

140147
timer2.lap("Setting up container configuration push");

‎jib-core/src/main/java/com/google/cloud/tools/jib/cache/Caches.java‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.Closeable;
2222
import java.io.IOException;
2323
import java.nio.file.Files;
24-
import java.nio.file.NotDirectoryException;
2524
import java.nio.file.Path;
2625

2726
/**
@@ -47,13 +46,13 @@ public static class Initializer {
4746
/** A file to store in the default base image layers cache to check ownership by Jib. */
4847
private static final String OWNERSHIP_FILE_NAME = ".jib";
4948

50-
@VisibleForTesting
5149
/**
5250
* Ensures ownership of {@code cacheDirectory} by checking for the existence of {@link
5351
* #OWNERSHIP_FILE_NAME}.
5452
*
5553
* <p>This is a safety check to make sure we are not writing to a directory not created by Jib.
5654
*/
55+
@VisibleForTesting
5756
static void ensureOwnership(Path cacheDirectory)
5857
throws CacheDirectoryNotOwnedException, IOException {
5958
Path ownershipFile = cacheDirectory.resolve(OWNERSHIP_FILE_NAME);
@@ -107,22 +106,31 @@ public static Initializer newInitializer(Path applicationCacheDirectory) {
107106

108107
/** Instantiate with {@link Initializer#init}. */
109108
private Caches(Path baseCacheDirectory, Path applicationCacheDirectory)
110-
throws CacheMetadataCorruptedException, NotDirectoryException {
111-
baseCache = Cache.init(baseCacheDirectory);
109+
throws CacheMetadataCorruptedException, IOException {
112110
applicationCache = Cache.init(applicationCacheDirectory);
113-
}
114111

115-
@Override
116-
public void close() throws IOException {
117-
baseCache.close();
118-
applicationCache.close();
112+
// Ensures that only one Cache is initialized if using the same directory.
113+
if (Files.isSameFile(baseCacheDirectory, applicationCacheDirectory)) {
114+
baseCache = applicationCache;
115+
} else {
116+
baseCache = Cache.init(baseCacheDirectory);
117+
}
119118
}
120119

121-
Cache getBaseCache() {
120+
public Cache getBaseCache() {
122121
return baseCache;
123122
}
124123

125-
Cache getApplicationCache() {
124+
public Cache getApplicationCache() {
126125
return applicationCache;
127126
}
127+
128+
@Override
129+
public void close() throws IOException {
130+
applicationCache.close();
131+
132+
if (baseCache != applicationCache) {
133+
baseCache.close();
134+
}
135+
}
128136
}

‎jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import com.google.cloud.tools.jib.builder.BuildConfiguration;
2222
import com.google.cloud.tools.jib.builder.BuildImageSteps;
2323
import com.google.cloud.tools.jib.builder.SourceFilesConfiguration;
24+
import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException;
2425
import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException;
26+
import com.google.cloud.tools.jib.cache.Caches;
2527
import com.google.cloud.tools.jib.http.Authorization;
2628
import com.google.cloud.tools.jib.http.Authorizations;
2729
import com.google.cloud.tools.jib.image.ImageReference;
@@ -111,6 +113,9 @@ private Class<? extends BuildableManifestTemplate> getManifestTemplateClass() {
111113
@Parameter(defaultValue = "Docker", required = true)
112114
private ImageFormat imageFormat;
113115

116+
@Parameter(defaultValue = "false", required = true)
117+
private boolean useOnlyProjectCache;
118+
114119
@Override
115120
public void execute() throws MojoExecutionException, MojoFailureException {
116121
validateParameters();
@@ -173,6 +178,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
173178
throw new MojoExecutionException("Could not create cache directory: " + cacheDirectory, ex);
174179
}
175180
}
181+
Caches.Initializer cachesInitializer = Caches.newInitializer(cacheDirectory);
182+
if (useOnlyProjectCache) {
183+
cachesInitializer.setBaseCacheDirectory(cacheDirectory);
184+
}
176185

177186
getLog().info("");
178187
getLog().info("Pushing image as " + targetImageReference);
@@ -186,7 +195,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
186195

187196
RegistryClient.setUserAgentSuffix(USER_AGENT_SUFFIX);
188197

189-
buildImage(new BuildImageSteps(buildConfiguration, sourceFilesConfiguration, cacheDirectory));
198+
buildImage(
199+
new BuildImageSteps(buildConfiguration, sourceFilesConfiguration, cachesInitializer));
190200

191201
getLog().info("");
192202
getLog().info("Built and pushed image as " + targetImageReference);
@@ -232,6 +242,14 @@ void buildImage(BuildImageSteps buildImageSteps) throws MojoExecutionException {
232242
getLog().error(ex);
233243
// TODO: Add more suggestions for various build failures.
234244
throwMojoExecutionExceptionWithHelpMessage(ex, null);
245+
246+
} catch (CacheDirectoryNotOwnedException ex) {
247+
throwMojoExecutionExceptionWithHelpMessage(
248+
ex,
249+
"check that '"
250+
+ ex.getCacheDirectory()
251+
+ "' is not used by another application or set the `useOnlyProjectCache` "
252+
+ "configuration");
235253
}
236254
}
237255

‎jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoTest.java‎

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
import com.google.api.client.http.HttpStatusCodes;
2121
import com.google.cloud.tools.jib.builder.BuildConfiguration;
2222
import com.google.cloud.tools.jib.builder.BuildImageSteps;
23+
import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException;
2324
import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException;
2425
import com.google.cloud.tools.jib.registry.RegistryUnauthorizedException;
2526
import java.io.IOException;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
2629
import java.util.Collections;
2730
import java.util.concurrent.ExecutionException;
2831
import org.apache.http.conn.HttpHostConnectException;
@@ -60,8 +63,8 @@ public void testBuildImage_pass() throws MojoExecutionException {
6063

6164
@Test
6265
public void testBuildImage_cacheMetadataCorruptedException()
63-
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
64-
IOException {
66+
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException,
67+
CacheDirectoryNotOwnedException {
6568
CacheMetadataCorruptedException mockCacheMetadataCorruptedException =
6669
Mockito.mock(CacheMetadataCorruptedException.class);
6770
Mockito.doThrow(mockCacheMetadataCorruptedException).when(mockBuildImageSteps).run();
@@ -80,8 +83,8 @@ public void testBuildImage_cacheMetadataCorruptedException()
8083

8184
@Test
8285
public void testBuildImage_executionException_httpHostConnectException()
83-
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
84-
IOException {
86+
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException,
87+
CacheDirectoryNotOwnedException {
8588
HttpHostConnectException mockHttpHostConnectException =
8689
Mockito.mock(HttpHostConnectException.class);
8790
Mockito.when(mockExecutionException.getCause()).thenReturn(mockHttpHostConnectException);
@@ -101,8 +104,8 @@ public void testBuildImage_executionException_httpHostConnectException()
101104

102105
@Test
103106
public void testBuildImage_executionException_registryUnauthorizedException_statusCodeForbidden()
104-
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
105-
IOException {
107+
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException,
108+
CacheDirectoryNotOwnedException {
106109
Mockito.when(mockRegistryUnauthorizedException.getHttpResponseException())
107110
.thenReturn(mockHttpResponseException);
108111
Mockito.when(mockRegistryUnauthorizedException.getImageReference())
@@ -127,8 +130,8 @@ public void testBuildImage_executionException_registryUnauthorizedException_stat
127130

128131
@Test
129132
public void testBuildImage_executionException_registryUnauthorizedException_noCredentials()
130-
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
131-
IOException {
133+
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException,
134+
CacheDirectoryNotOwnedException {
132135
Mockito.when(mockRegistryUnauthorizedException.getHttpResponseException())
133136
.thenReturn(mockHttpResponseException);
134137
Mockito.when(mockRegistryUnauthorizedException.getRegistry()).thenReturn("someregistry");
@@ -151,8 +154,8 @@ public void testBuildImage_executionException_registryUnauthorizedException_noCr
151154

152155
@Test
153156
public void testBuildImage_executionException_registryUnauthorizedException_other()
154-
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
155-
IOException {
157+
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException,
158+
CacheDirectoryNotOwnedException {
156159
Mockito.when(mockRegistryUnauthorizedException.getHttpResponseException())
157160
.thenReturn(mockHttpResponseException);
158161
Mockito.when(mockRegistryUnauthorizedException.getRegistry()).thenReturn("someregistry");
@@ -178,8 +181,8 @@ public void testBuildImage_executionException_registryUnauthorizedException_othe
178181

179182
@Test
180183
public void testBuildImage_executionException_other()
181-
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
182-
IOException {
184+
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException,
185+
CacheDirectoryNotOwnedException {
183186
Throwable throwable = new Throwable();
184187
Mockito.when(mockExecutionException.getCause()).thenReturn(throwable);
185188
Mockito.doThrow(mockExecutionException).when(mockBuildImageSteps).run();
@@ -196,8 +199,8 @@ public void testBuildImage_executionException_other()
196199

197200
@Test
198201
public void testBuildImage_otherException()
199-
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
200-
IOException {
202+
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException,
203+
CacheDirectoryNotOwnedException {
201204
IOException ioException = new IOException();
202205
Mockito.doThrow(ioException).when(mockBuildImageSteps).run();
203206

@@ -215,4 +218,30 @@ public void testBuildImage_otherException()
215218
Mockito.verify(mockLog).error(ioException);
216219
}
217220
}
221+
222+
@Test
223+
public void testBuildImage_cacheDirectoryNotOwnedException()
224+
throws InterruptedException, ExecutionException, CacheDirectoryNotOwnedException,
225+
CacheMetadataCorruptedException, IOException {
226+
Path expectedCacheDirectory = Paths.get("some/path");
227+
228+
CacheDirectoryNotOwnedException mockCacheDirectoryNotOwnedException =
229+
Mockito.mock(CacheDirectoryNotOwnedException.class);
230+
Mockito.when(mockCacheDirectoryNotOwnedException.getCacheDirectory())
231+
.thenReturn(expectedCacheDirectory);
232+
Mockito.doThrow(mockCacheDirectoryNotOwnedException).when(mockBuildImageSteps).run();
233+
234+
try {
235+
testBuildImageMojo.buildImage(mockBuildImageSteps);
236+
Assert.fail("buildImage should have thrown an exception");
237+
238+
} catch (MojoExecutionException ex) {
239+
Assert.assertEquals(
240+
"Build image failed, perhaps you should check that '"
241+
+ expectedCacheDirectory
242+
+ "' is not used by another application or set the `useOnlyProjectCache` configuration",
243+
ex.getMessage());
244+
Assert.assertEquals(mockCacheDirectoryNotOwnedException, ex.getCause());
245+
}
246+
}
218247
}

0 commit comments

Comments
 (0)