@@ -240,7 +240,7 @@ public void onEvent(@NonNull Action action, @NonNull KubernetesSlave node, @NonN
240240 LOGGER .info (() -> ns + "/" + name + " Container " + c .getName () + " was just terminated, so removing the corresponding Jenkins agent" );
241241 runListener .getLogger ().printf ("%s/%s Container %s was terminated (Exit Code: %d, Reason: %s)%n" , ns , name , c .getName (), t .getExitCode (), t .getReason ());
242242 });
243- node . terminate ( );
243+ logLastLinesThenTerminateNode ( node , pod , runListener );
244244 }
245245 }
246246 }
@@ -258,17 +258,21 @@ public void onEvent(@NonNull Action action, @NonNull KubernetesSlave node, @NonN
258258 TaskListener runListener = node .getTemplate ().getListener ();
259259 LOGGER .info (() -> ns + "/" + name + " Pod just failed. Removing the corresponding Jenkins agent. Reason: " + pod .getStatus ().getReason () + ", Message: " + pod .getStatus ().getMessage ());
260260 runListener .getLogger ().printf ("%s/%s Pod just failed (Reason: %s, Message: %s)%n" , ns , name , pod .getStatus ().getReason (), pod .getStatus ().getMessage ());
261- try {
262- String lines = PodUtils . logLastLines ( pod , node . getKubernetesCloud (). connect ());
263- if ( lines != null ) {
264- runListener . getLogger (). print ( lines );
265- }
266- } catch ( KubernetesAuthException e ) {
267- LOGGER . log ( Level . FINE , e , () -> "Unable to get logs after pod failed event" );
268- } finally {
269- node . terminate ();
270- }
261+ logLastLinesThenTerminateNode ( node , pod , runListener );
262+ }
263+ }
264+ }
265+
266+ private static void logLastLinesThenTerminateNode ( KubernetesSlave node , Pod pod , TaskListener runListener ) throws IOException , InterruptedException {
267+ try {
268+ String lines = PodUtils . logLastLines ( pod , node . getKubernetesCloud (). connect ());
269+ if ( lines != null ) {
270+ runListener . getLogger (). print ( lines );
271271 }
272+ } catch (KubernetesAuthException e ) {
273+ LOGGER .log (Level .FINE , e , () -> "Unable to get logs after pod failed event" );
274+ } finally {
275+ node .terminate ();
272276 }
273277 }
274278
0 commit comments