@@ -437,31 +437,39 @@ public void onEvent(
437
437
438
438
List <ContainerStatus > terminatedContainers = PodUtils .getTerminatedContainers (pod );
439
439
if (!terminatedContainers .isEmpty ()) {
440
- String ns = pod .getMetadata ().getNamespace ();
441
- String name = pod .getMetadata ().getName ();
442
- TaskListener runListener = node .getRunListener ();
443
440
List <String > containers = new ArrayList <>();
444
441
terminatedContainers .forEach (c -> {
445
442
ContainerStateTerminated t = c .getState ().getTerminated ();
446
443
String containerName = c .getName ();
447
444
containers .add (containerName );
448
- LOGGER .info (() -> ns + "/" + name + " Container " + containerName
449
- + " was just terminated, so removing the corresponding Jenkins agent" );
450
445
String reason = t .getReason ();
451
- runListener
452
- .getLogger ()
453
- .printf (
454
- "%s/%s Container %s was terminated (Exit Code: %d, Reason: %s)%n" ,
455
- ns , name , containerName , t .getExitCode (), reason );
456
446
if (reason != null ) {
457
447
terminationReasons .add (reason );
458
448
}
459
449
});
460
-
461
- logLastLinesThenTerminateNode (node , pod , runListener );
462
- PodUtils .cancelQueueItemFor (pod , "ContainerError" );
463
- disconnectComputer (
450
+ String reason = pod .getStatus ().getReason ();
451
+ String message = pod .getStatus ().getMessage ();
452
+ var sb = new StringBuilder ()
453
+ .append (pod .getMetadata ().getNamespace ())
454
+ .append ("/" )
455
+ .append (pod .getMetadata ().getName ());
456
+ if (containers .size () > 1 ) {
457
+ sb .append (" Containers " )
458
+ .append (String .join ("," , containers ))
459
+ .append (" were terminated." );
460
+ } else {
461
+ sb .append (" Container " )
462
+ .append (String .join ("," , containers ))
463
+ .append (" was terminated." );
464
+ }
465
+ logAndCleanUp (
464
466
node ,
467
+ pod ,
468
+ terminationReasons ,
469
+ reason ,
470
+ message ,
471
+ sb ,
472
+ node .getRunListener (),
465
473
new PodOfflineCause (Messages ._PodOfflineCause_ContainerFailed ("ContainerError" , containers )));
466
474
}
467
475
}
@@ -481,27 +489,60 @@ public void onEvent(
481
489
}
482
490
483
491
if ("Failed" .equals (pod .getStatus ().getPhase ())) {
484
- String ns = pod .getMetadata ().getNamespace ();
485
- String name = pod .getMetadata ().getName ();
486
- TaskListener runListener = node .getRunListener ();
487
492
String reason = pod .getStatus ().getReason ();
488
493
String message = pod .getStatus ().getMessage ();
489
- LOGGER .info (
490
- () -> ns + "/" + name + " Pod just failed. Removing the corresponding Jenkins agent. Reason: "
491
- + reason + ", Message: " + message );
492
- runListener
493
- .getLogger ()
494
- .printf ("%s/%s Pod just failed (Reason: %s, Message: %s)%n" , ns , name , reason , message );
495
- if (reason != null ) {
496
- terminationReasons .add (reason );
497
- }
498
-
499
- logLastLinesThenTerminateNode (node , pod , runListener );
500
- disconnectComputer (node , new PodOfflineCause (Messages ._PodOfflineCause_PodFailed (reason , message )));
494
+ logAndCleanUp (
495
+ node ,
496
+ pod ,
497
+ terminationReasons ,
498
+ reason ,
499
+ message ,
500
+ new StringBuilder ()
501
+ .append (pod .getMetadata ().getNamespace ())
502
+ .append ("/" )
503
+ .append (pod .getMetadata ().getName ())
504
+ .append (" Pod just failed." ),
505
+ node .getRunListener (),
506
+ new PodOfflineCause (Messages ._PodOfflineCause_PodFailed (reason , message )));
501
507
}
502
508
}
503
509
}
504
510
511
+ private static void logAndCleanUp (
512
+ KubernetesSlave node ,
513
+ Pod pod ,
514
+ Set <String > terminationReasons ,
515
+ String reason ,
516
+ String message ,
517
+ StringBuilder sb ,
518
+ TaskListener runListener ,
519
+ PodOfflineCause cause )
520
+ throws IOException , InterruptedException {
521
+ List <String > details = new ArrayList <>();
522
+ if (reason != null ) {
523
+ details .add ("Reason: " + reason );
524
+ terminationReasons .add (reason );
525
+ }
526
+ if (message != null ) {
527
+ details .add ("Message: " + message );
528
+ }
529
+ if (!details .isEmpty ()) {
530
+ sb .append (" " ).append (String .join (", " , details )).append ("." );
531
+ }
532
+ var evictionCondition = pod .getStatus ().getConditions ().stream ()
533
+ .filter (c -> "EvictionByEvictionAPI" .equals (c .getReason ()))
534
+ .findFirst ();
535
+ if (evictionCondition .isPresent ()) {
536
+ sb .append (" Pod was evicted by the Kubernetes Eviction API." );
537
+ terminationReasons .add (evictionCondition .get ().getReason ());
538
+ }
539
+ LOGGER .info (() -> sb + " Removing corresponding node " + node .getNodeName () + " from Jenkins." );
540
+ runListener .getLogger ().println (sb );
541
+ logLastLinesThenTerminateNode (node , pod , runListener );
542
+ PodUtils .cancelQueueItemFor (pod , "PodFailure" );
543
+ disconnectComputer (node , cause );
544
+ }
545
+
505
546
private static void logLastLinesThenTerminateNode (KubernetesSlave node , Pod pod , TaskListener runListener )
506
547
throws IOException , InterruptedException {
507
548
try {
0 commit comments