@@ -92,7 +92,13 @@ public static Reaper getInstance() {
9292
9393 @ Override
9494 public void onOnline (Computer c , TaskListener listener ) throws IOException , InterruptedException {
95- if (c instanceof KubernetesComputer && activated .compareAndSet (false , true )) {
95+ if (c instanceof KubernetesComputer ) {
96+ maybeActivate ();
97+ }
98+ }
99+
100+ public void maybeActivate () {
101+ if (activated .compareAndSet (false , true )) {
96102 activate ();
97103 }
98104 }
@@ -107,6 +113,10 @@ private void activate() {
107113 continue ;
108114 }
109115 KubernetesSlave ks = (KubernetesSlave ) n ;
116+ if (ks .getLauncher ().isLaunchSupported ()) {
117+ // Being launched, don't touch it.
118+ continue ;
119+ }
110120 String ns = ks .getNamespace ();
111121 String name = ks .getPodName ();
112122 try {
@@ -230,7 +240,7 @@ public void onEvent(@NonNull Action action, @NonNull KubernetesSlave node, @NonN
230240 LOGGER .info (() -> ns + "/" + name + " Container " + c .getName () + " was just terminated, so removing the corresponding Jenkins agent" );
231241 runListener .getLogger ().printf ("%s/%s Container %s was terminated (Exit Code: %d, Reason: %s)%n" , ns , name , c .getName (), t .getExitCode (), t .getReason ());
232242 });
233- node . terminate ( );
243+ logLastLinesThenTerminateNode ( node , pod , runListener );
234244 }
235245 }
236246 }
@@ -248,20 +258,24 @@ public void onEvent(@NonNull Action action, @NonNull KubernetesSlave node, @NonN
248258 TaskListener runListener = node .getTemplate ().getListener ();
249259 LOGGER .info (() -> ns + "/" + name + " Pod just failed. Removing the corresponding Jenkins agent. Reason: " + pod .getStatus ().getReason () + ", Message: " + pod .getStatus ().getMessage ());
250260 runListener .getLogger ().printf ("%s/%s Pod just failed (Reason: %s, Message: %s)%n" , ns , name , pod .getStatus ().getReason (), pod .getStatus ().getMessage ());
251- try {
252- String lines = PodUtils .logLastLines (pod , node .getKubernetesCloud ().connect ());
253- if (lines != null ) {
254- runListener .getLogger ().print (lines );
255- }
256- } catch (KubernetesAuthException e ) {
257- LOGGER .log (Level .FINE , e , () -> "Unable to get logs after pod failed event" );
258- } finally {
259- node .terminate ();
260- }
261+ logLastLinesThenTerminateNode (node , pod , runListener );
261262 }
262263 }
263264 }
264265
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 );
271+ }
272+ } catch (KubernetesAuthException e ) {
273+ LOGGER .log (Level .FINE , e , () -> "Unable to get logs after pod failed event" );
274+ } finally {
275+ node .terminate ();
276+ }
277+ }
278+
265279 @ Extension
266280 public static class TerminateAgentOnImagePullBackOff implements Listener {
267281
0 commit comments