Skip to content

Commit 01e2b4b

Browse files
authored
Moves BuildImageSteps exception handling to jib-core. (GoogleContainerTools#256)
1 parent f5fc5a6 commit 01e2b4b

13 files changed

Lines changed: 329 additions & 195 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ public Authorization call()
5757
try (Timer ignored =
5858
new Timer(
5959
buildConfiguration.getBuildLogger(),
60-
String.format(DESCRIPTION, buildConfiguration.getTargetRegistry()))) {
60+
String.format(DESCRIPTION, buildConfiguration.getTargetImageRegistry()))) {
6161
Authorization registryCredentials = NonBlockingFutures.get(registryCredentialsFuture);
6262
RegistryAuthenticator registryAuthenticator =
6363
RegistryAuthenticators.forOther(
64-
buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository());
64+
buildConfiguration.getTargetImageRegistry(),
65+
buildConfiguration.getTargetImageRepository());
6566
if (registryAuthenticator == null) {
6667
return registryCredentials;
6768
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,15 @@ public RegistryCredentials getKnownBaseRegistryCredentials() {
244244
return knownBaseRegistryCredentials;
245245
}
246246

247-
public String getTargetRegistry() {
247+
public String getTargetImageRegistry() {
248248
return targetImageReference.getRegistry();
249249
}
250250

251-
public String getTargetRepository() {
251+
public String getTargetImageRepository() {
252252
return targetImageReference.getRepository();
253253
}
254254

255-
public String getTargetTag() {
255+
public String getTargetImageTag() {
256256
return targetImageReference.getTag();
257257
}
258258

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void run()
8080
listeningExecutorService.submit(
8181
new RetrieveRegistryCredentialsStep(
8282
buildConfiguration,
83-
buildConfiguration.getTargetRegistry(),
83+
buildConfiguration.getTargetImageRegistry(),
8484
buildConfiguration.getTargetImageCredentialHelperName(),
8585
buildConfiguration.getKnownTargetRegistryCredentials()));
8686
ListenableFuture<Authorization> retrieveBaseImageRegistryCredentialsFuture =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public Void call()
5656
RegistryClient registryClient =
5757
new RegistryClient(
5858
pushAuthorizationFuture.get(),
59-
buildConfiguration.getTargetRegistry(),
60-
buildConfiguration.getTargetRepository())
59+
buildConfiguration.getTargetImageRegistry(),
60+
buildConfiguration.getTargetImageRepository())
6161
.setTimer(timer);
6262

6363
if (registryClient.checkBlob(layerDigest) != null) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ private BlobDescriptor afterBuildConfigurationFutureFuture()
7676
RegistryClient registryClient =
7777
new RegistryClient(
7878
NonBlockingFutures.get(pushAuthorizationFuture),
79-
buildConfiguration.getTargetRegistry(),
80-
buildConfiguration.getTargetRepository())
79+
buildConfiguration.getTargetImageRegistry(),
80+
buildConfiguration.getTargetImageRepository())
8181
.setTimer(timer);
8282

8383
CountingDigestOutputStream digestOutputStream =

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ private Void afterPushBaseImageLayerFuturesFuture()
103103
RegistryClient registryClient =
104104
new RegistryClient(
105105
NonBlockingFutures.get(pushAuthorizationFuture),
106-
buildConfiguration.getTargetRegistry(),
107-
buildConfiguration.getTargetRepository());
106+
buildConfiguration.getTargetImageRegistry(),
107+
buildConfiguration.getTargetImageRepository());
108108

109109
// TODO: Consolidate with BuildAndPushContainerConfigurationStep.
110110
// Constructs the image.
@@ -124,7 +124,7 @@ private Void afterPushBaseImageLayerFuturesFuture()
124124
buildConfiguration.getTargetFormat(),
125125
NonBlockingFutures.get(
126126
NonBlockingFutures.get(containerConfigurationBlobDescriptorFutureFuture)));
127-
registryClient.pushManifest(manifestTemplate, buildConfiguration.getTargetTag());
127+
registryClient.pushManifest(manifestTemplate, buildConfiguration.getTargetImageTag());
128128
}
129129

130130
return null;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.frontend;
18+
19+
import com.google.cloud.tools.jib.builder.BuildImageSteps;
20+
21+
/** Wraps an exception that happens during {@link BuildImageSteps#run}. */
22+
public class BuildImageStepsExecutionException extends Exception {
23+
24+
BuildImageStepsExecutionException(String message, Throwable cause) {
25+
super(message, cause);
26+
}
27+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.frontend;
18+
19+
import com.google.api.client.http.HttpResponseException;
20+
import com.google.api.client.http.HttpStatusCodes;
21+
import com.google.cloud.tools.jib.builder.BuildConfiguration;
22+
import com.google.cloud.tools.jib.builder.BuildImageSteps;
23+
import com.google.cloud.tools.jib.builder.SourceFilesConfiguration;
24+
import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException;
25+
import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException;
26+
import com.google.cloud.tools.jib.cache.Caches;
27+
import com.google.cloud.tools.jib.registry.RegistryAuthenticationFailedException;
28+
import com.google.cloud.tools.jib.registry.RegistryUnauthorizedException;
29+
import com.google.common.annotations.VisibleForTesting;
30+
import java.io.IOException;
31+
import java.net.UnknownHostException;
32+
import java.nio.file.Files;
33+
import java.nio.file.Path;
34+
import java.util.concurrent.ExecutionException;
35+
import java.util.function.Supplier;
36+
import org.apache.http.conn.HttpHostConnectException;
37+
38+
/** Runs {@link BuildImageSteps} and builds helpful error messages. */
39+
public class BuildImageStepsRunner {
40+
41+
private static final HelpfulMessageBuilder helpfulMessageBuilder =
42+
new HelpfulMessageBuilder("Build image failed");
43+
44+
/**
45+
* Sets up a new {@link BuildImageStepsRunner}. Creates the directory for the cache, if needed.
46+
*
47+
* @param useOnlyProjectCache if {@code true}, sets the base layers cache directory to be the same
48+
* as the application layers cache directory
49+
* @throws CacheDirectoryCreationException if the {@code cacheDirectory} could not be created
50+
*/
51+
public static BuildImageStepsRunner newRunner(
52+
BuildConfiguration buildConfiguration,
53+
SourceFilesConfiguration sourceFilesConfiguration,
54+
Path cacheDirectory,
55+
boolean useOnlyProjectCache)
56+
throws CacheDirectoryCreationException {
57+
if (!Files.exists(cacheDirectory)) {
58+
try {
59+
Files.createDirectory(cacheDirectory);
60+
61+
} catch (IOException ex) {
62+
throw new CacheDirectoryCreationException(cacheDirectory, ex);
63+
}
64+
}
65+
Caches.Initializer cachesInitializer = Caches.newInitializer(cacheDirectory);
66+
if (useOnlyProjectCache) {
67+
cachesInitializer.setBaseCacheDirectory(cacheDirectory);
68+
}
69+
70+
return new BuildImageStepsRunner(
71+
buildConfiguration, sourceFilesConfiguration, cachesInitializer);
72+
}
73+
74+
private final Supplier<BuildImageSteps> buildImageStepsSupplier;
75+
76+
@VisibleForTesting
77+
BuildImageStepsRunner(Supplier<BuildImageSteps> buildImageStepsSupplier) {
78+
this.buildImageStepsSupplier = buildImageStepsSupplier;
79+
}
80+
81+
private BuildImageStepsRunner(
82+
BuildConfiguration buildConfiguration,
83+
SourceFilesConfiguration sourceFilesConfiguration,
84+
Caches.Initializer cachesInitializer) {
85+
buildImageStepsSupplier =
86+
() -> new BuildImageSteps(buildConfiguration, sourceFilesConfiguration, cachesInitializer);
87+
}
88+
89+
public void buildImage() throws BuildImageStepsExecutionException {
90+
BuildImageSteps buildImageSteps = buildImageStepsSupplier.get();
91+
92+
try {
93+
buildImageSteps.run();
94+
95+
} catch (CacheMetadataCorruptedException cacheMetadataCorruptedException) {
96+
// TODO: Have this be different for Maven and Gradle.
97+
throw new BuildImageStepsExecutionException(
98+
helpfulMessageBuilder.withSuggestion("run 'mvn clean' to clear the cache"),
99+
cacheMetadataCorruptedException);
100+
101+
} catch (ExecutionException executionException) {
102+
BuildConfiguration buildConfiguration = buildImageSteps.getBuildConfiguration();
103+
104+
if (executionException.getCause() instanceof HttpHostConnectException) {
105+
// Failed to connect to registry.
106+
throw new BuildImageStepsExecutionException(
107+
helpfulMessageBuilder.withSuggestion(
108+
"make sure your Internet is up and that the registry you are pushing to exists"),
109+
executionException.getCause());
110+
111+
} else if (executionException.getCause() instanceof RegistryUnauthorizedException) {
112+
handleRegistryUnauthorizedException(
113+
(RegistryUnauthorizedException) executionException.getCause(), buildConfiguration);
114+
115+
} else if (executionException.getCause() instanceof RegistryAuthenticationFailedException
116+
&& executionException.getCause().getCause() instanceof HttpResponseException) {
117+
handleRegistryUnauthorizedException(
118+
new RegistryUnauthorizedException(
119+
buildConfiguration.getTargetImageRegistry(),
120+
buildConfiguration.getTargetImageRepository(),
121+
(HttpResponseException) executionException.getCause().getCause()),
122+
buildConfiguration);
123+
124+
} else if (executionException.getCause() instanceof UnknownHostException) {
125+
throw new BuildImageStepsExecutionException(
126+
helpfulMessageBuilder.withSuggestion(
127+
"make sure that the registry you configured exists/is spelled properly"),
128+
executionException.getCause());
129+
130+
} else {
131+
throw new BuildImageStepsExecutionException(
132+
helpfulMessageBuilder.withNoHelp(), executionException.getCause());
133+
}
134+
135+
} catch (InterruptedException | IOException ex) {
136+
// TODO: Add more suggestions for various build failures.
137+
throw new BuildImageStepsExecutionException(helpfulMessageBuilder.withNoHelp(), ex);
138+
139+
} catch (CacheDirectoryNotOwnedException ex) {
140+
throw new BuildImageStepsExecutionException(
141+
helpfulMessageBuilder.withSuggestion(
142+
"check that '"
143+
+ ex.getCacheDirectory()
144+
+ "' is not used by another application or set the `useOnlyProjectCache` "
145+
+ "configuration"),
146+
ex);
147+
}
148+
}
149+
150+
private void handleRegistryUnauthorizedException(
151+
RegistryUnauthorizedException registryUnauthorizedException,
152+
BuildConfiguration buildConfiguration)
153+
throws BuildImageStepsExecutionException {
154+
if (registryUnauthorizedException.getHttpResponseException().getStatusCode()
155+
== HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
156+
// No permissions for registry/repository.
157+
throw new BuildImageStepsExecutionException(
158+
helpfulMessageBuilder.withSuggestion(
159+
"make sure you have permissions for "
160+
+ registryUnauthorizedException.getImageReference()),
161+
registryUnauthorizedException);
162+
163+
} else {
164+
boolean isRegistryForBase =
165+
registryUnauthorizedException
166+
.getRegistry()
167+
.equals(buildConfiguration.getBaseImageRegistry());
168+
boolean isRegistryForTarget =
169+
registryUnauthorizedException
170+
.getRegistry()
171+
.equals(buildConfiguration.getTargetImageRegistry());
172+
boolean areBaseImageCredentialsConfigured =
173+
buildConfiguration.getBaseImageCredentialHelperName() != null
174+
|| buildConfiguration.getKnownBaseRegistryCredentials() != null;
175+
boolean areTargetImageCredentialsConfigured =
176+
buildConfiguration.getTargetImageCredentialHelperName() != null
177+
|| buildConfiguration.getKnownTargetRegistryCredentials() != null;
178+
179+
if ((isRegistryForBase && !areBaseImageCredentialsConfigured)
180+
|| (isRegistryForTarget && !areTargetImageCredentialsConfigured)) {
181+
// No credential helpers defined.
182+
throw new BuildImageStepsExecutionException(
183+
helpfulMessageBuilder.withSuggestion(
184+
"set a credential helper name with the configuration 'credHelpers' or "
185+
+ "set credentials for '"
186+
+ registryUnauthorizedException.getRegistry()
187+
+ "' in your Maven settings"),
188+
registryUnauthorizedException);
189+
}
190+
191+
// Credential helper probably was not configured correctly or did not have the necessary
192+
// credentials.
193+
throw new BuildImageStepsExecutionException(
194+
helpfulMessageBuilder.withSuggestion(
195+
"make sure your credentials for '"
196+
+ registryUnauthorizedException.getRegistry()
197+
+ "' are set up correctly"),
198+
registryUnauthorizedException);
199+
}
200+
}
201+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.frontend;
18+
19+
import com.google.cloud.tools.jib.cache.Cache;
20+
import java.nio.file.Path;
21+
22+
/** Thrown when a directory could not be created to be used as the {@link Cache}. */
23+
public class CacheDirectoryCreationException extends Exception {
24+
25+
CacheDirectoryCreationException(Path cacheDirectory, Throwable cause) {
26+
super("Could not create cache directory: " + cacheDirectory, cause);
27+
}
28+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public void testBuilder() {
7575
Assert.assertEquals(
7676
expectedBaseImageCredentialHelperName,
7777
buildConfiguration.getBaseImageCredentialHelperName());
78-
Assert.assertEquals(expectedTargetServerUrl, buildConfiguration.getTargetRegistry());
79-
Assert.assertEquals(expectedTargetImageName, buildConfiguration.getTargetRepository());
80-
Assert.assertEquals(expectedTargetTag, buildConfiguration.getTargetTag());
78+
Assert.assertEquals(expectedTargetServerUrl, buildConfiguration.getTargetImageRegistry());
79+
Assert.assertEquals(expectedTargetImageName, buildConfiguration.getTargetImageRepository());
80+
Assert.assertEquals(expectedTargetTag, buildConfiguration.getTargetImageTag());
8181
Assert.assertEquals(
8282
expectedTargetImageCredentialHelperName,
8383
buildConfiguration.getTargetImageCredentialHelperName());

0 commit comments

Comments
 (0)