Fix build timeout for OpenShift build service
Handle intermittent Socket timeout build issues happening on
OpenShift build service.
@@ -33,6 +33,7 @@ import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.dsl.LogWatch;
+import io.fabric8.maven.core.access.ClusterAccess;
import io.fabric8.maven.core.config.OpenShiftBuildStrategy;
import io.fabric8.maven.core.service.BuildService;
import io.fabric8.maven.core.service.Fabric8ServiceException;
@@ -70,6 +71,13 @@ public class OpenshiftBuildService implements BuildService {
private final Logger log;
private ServiceHub dockerServiceHub;
private BuildServiceConfig config;
+ private ClusterAccess clusterAccess;
+
+ /*
+ * Retry parameter
+ */
+ private static final int RESOURCE_CREATION_RETRIES = 5;
+ private static final int RESOURCE_CREATION_RETRY_TIMEOUT_IN_MILLIS = 1000;
public OpenshiftBuildService(OpenShiftClient client, Logger log, ServiceHub dockerServiceHub, BuildServiceConfig config) {
Objects.requireNonNull(client, "client");
@@ -81,6 +89,7 @@ public class OpenshiftBuildService implements BuildService {
this.log = log;
this.dockerServiceHub = dockerServiceHub;
this.config = config;
+ this.clusterAccess = new ClusterAccess(null);
}
@Override
@@ -299,14 +308,44 @@ public class OpenshiftBuildService implements BuildService {
}
private void applyResourceObjects(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder) throws Exception {
+ // Adding a workaround to handle intermittent Socket closed errors while
+ // building on OpenShift. See https://github.com/fabric8io/fabric8-maven-plugin/issues/1133
+ // for more details.
+ int nTries = 0;
+ boolean bResourcesCreated = false;
+ Exception buildException = null;
+ do {
+ try {
if (config.getEnricherTask() != null) {
config.getEnricherTask().execute(builder);
}
-
if (builder.hasItems()) {
KubernetesList k8sList = builder.build();
client.lists().create(k8sList);
}
+ // If we are here, it means resources got created successfully.
+ bResourcesCreated = true;
+ } catch (Exception aException) {
+ // Handling the exception like this because we get a generic Exception from the above block.
+ // Retry only when Exception is of socket closed message.
+ if(aException.getMessage() != null && aException.getMessage().contains("Socket closed")) {
+ log.warn("Problem encountered while applying resource objects, retrying..");
+ buildException = aException;
+ nTries++;
+ Thread.sleep(RESOURCE_CREATION_RETRY_TIMEOUT_IN_MILLIS);
+ // Make a connection to cluster again.
+ client = clusterAccess.createDefaultClient(log);
+ }
+ else {
+ // The exception caught is not related to closed socket, so let's break
+ // and simply throw as it is.
+ throw new MojoExecutionException(aException.getMessage());
+ }
+ }
+ } while (nTries < RESOURCE_CREATION_RETRIES && !bResourcesCreated);
+
+ if (!bResourcesCreated)
+ throw new MojoExecutionException(buildException.getMessage());
}
private Build startBuild(OpenShiftClient client, File dockerTar, String buildName) {
