Skip to content
This repository was archived by the owner on Jun 19, 2024. It is now read-only.

Commit e685fa3

Browse files
author
hrishin
authored
Merge pull request #1163 from rohanKanojia/issue1133
Fix #1133 build timeout for OpenShift build service
2 parents 3f49051 + b5f6b0a commit e685fa3

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

core/src/main/java/io/fabric8/maven/core/service/openshift/OpenshiftBuildService.java

+46-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.fabric8.kubernetes.client.Watch;
3434
import io.fabric8.kubernetes.client.Watcher;
3535
import io.fabric8.kubernetes.client.dsl.LogWatch;
36+
import io.fabric8.maven.core.access.ClusterAccess;
3637
import io.fabric8.maven.core.config.OpenShiftBuildStrategy;
3738
import io.fabric8.maven.core.service.BuildService;
3839
import io.fabric8.maven.core.service.Fabric8ServiceException;
@@ -70,6 +71,13 @@ public class OpenshiftBuildService implements BuildService {
7071
private final Logger log;
7172
private ServiceHub dockerServiceHub;
7273
private BuildServiceConfig config;
74+
private ClusterAccess clusterAccess;
75+
76+
/*
77+
* Retry parameter
78+
*/
79+
private static final int RESOURCE_CREATION_RETRIES = 5;
80+
private static final int RESOURCE_CREATION_RETRY_TIMEOUT_IN_MILLIS = 1000;
7381

7482
public OpenshiftBuildService(OpenShiftClient client, Logger log, ServiceHub dockerServiceHub, BuildServiceConfig config) {
7583
Objects.requireNonNull(client, "client");
@@ -81,6 +89,7 @@ public OpenshiftBuildService(OpenShiftClient client, Logger log, ServiceHub dock
8189
this.log = log;
8290
this.dockerServiceHub = dockerServiceHub;
8391
this.config = config;
92+
this.clusterAccess = new ClusterAccess(null);
8493
}
8594

8695
@Override
@@ -299,14 +308,44 @@ private void checkOrCreateImageStream(BuildServiceConfig config, OpenShiftClient
299308
}
300309

301310
private void applyResourceObjects(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder) throws Exception {
302-
if (config.getEnricherTask() != null) {
303-
config.getEnricherTask().execute(builder);
304-
}
311+
// Adding a workaround to handle intermittent Socket closed errors while
312+
// building on OpenShift. See https://github.com/fabric8io/fabric8-maven-plugin/issues/1133
313+
// for more details.
314+
int nTries = 0;
315+
boolean bResourcesCreated = false;
316+
Exception buildException = null;
317+
do {
318+
try {
319+
if (config.getEnricherTask() != null) {
320+
config.getEnricherTask().execute(builder);
321+
}
322+
if (builder.hasItems()) {
323+
KubernetesList k8sList = builder.build();
324+
client.lists().create(k8sList);
325+
}
326+
// If we are here, it means resources got created successfully.
327+
bResourcesCreated = true;
328+
} catch (Exception aException) {
329+
// Handling the exception like this because we get a generic Exception from the above block.
330+
// Retry only when Exception is of socket closed message.
331+
if(aException.getMessage() != null && aException.getMessage().contains("Socket closed")) {
332+
log.warn("Problem encountered while applying resource objects, retrying..");
333+
buildException = aException;
334+
nTries++;
335+
Thread.sleep(RESOURCE_CREATION_RETRY_TIMEOUT_IN_MILLIS);
336+
// Make a connection to cluster again.
337+
client = clusterAccess.createDefaultClient(log);
338+
}
339+
else {
340+
// The exception caught is not related to closed socket, so let's break
341+
// and simply throw as it is.
342+
throw new MojoExecutionException(aException.getMessage());
343+
}
344+
}
345+
} while (nTries < RESOURCE_CREATION_RETRIES && !bResourcesCreated);
305346

306-
if (builder.hasItems()) {
307-
KubernetesList k8sList = builder.build();
308-
client.lists().create(k8sList);
309-
}
347+
if (!bResourcesCreated)
348+
throw new MojoExecutionException(buildException.getMessage());
310349
}
311350

312351
private Build startBuild(OpenShiftClient client, File dockerTar, String buildName) {

0 commit comments

Comments
 (0)