33
33
import io .fabric8 .kubernetes .client .Watch ;
34
34
import io .fabric8 .kubernetes .client .Watcher ;
35
35
import io .fabric8 .kubernetes .client .dsl .LogWatch ;
36
+ import io .fabric8 .maven .core .access .ClusterAccess ;
36
37
import io .fabric8 .maven .core .config .OpenShiftBuildStrategy ;
37
38
import io .fabric8 .maven .core .service .BuildService ;
38
39
import io .fabric8 .maven .core .service .Fabric8ServiceException ;
@@ -70,6 +71,13 @@ public class OpenshiftBuildService implements BuildService {
70
71
private final Logger log ;
71
72
private ServiceHub dockerServiceHub ;
72
73
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 ;
73
81
74
82
public OpenshiftBuildService (OpenShiftClient client , Logger log , ServiceHub dockerServiceHub , BuildServiceConfig config ) {
75
83
Objects .requireNonNull (client , "client" );
@@ -81,6 +89,7 @@ public OpenshiftBuildService(OpenShiftClient client, Logger log, ServiceHub dock
81
89
this .log = log ;
82
90
this .dockerServiceHub = dockerServiceHub ;
83
91
this .config = config ;
92
+ this .clusterAccess = new ClusterAccess (null );
84
93
}
85
94
86
95
@ Override
@@ -299,14 +308,44 @@ private void checkOrCreateImageStream(BuildServiceConfig config, OpenShiftClient
299
308
}
300
309
301
310
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 );
305
346
306
- if (builder .hasItems ()) {
307
- KubernetesList k8sList = builder .build ();
308
- client .lists ().create (k8sList );
309
- }
347
+ if (!bResourcesCreated )
348
+ throw new MojoExecutionException (buildException .getMessage ());
310
349
}
311
350
312
351
private Build startBuild (OpenShiftClient client , File dockerTar , String buildName ) {
0 commit comments