From 0c6d5c4e7753c5c3acda0dc0511801ae8ef02a00 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Wed, 19 Dec 2018 15:34:09 +0100 Subject: [PATCH 1/3] Do not wait for agent connection if pod has failed Fix some logging --- .../plugins/kubernetes/KubernetesLauncher.java | 18 +++++++++++++++--- .../plugins/kubernetes/KubernetesSlave.java | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncher.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncher.java index 90c20bc580..45d2bc30a5 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncher.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncher.java @@ -122,6 +122,7 @@ public void launch(SlaveComputer computer, TaskListener listener) { logger.printf("Waiting for Pod to be scheduled (%2$s/%3$s): %1$s%n", podId, waitedSec, waitForPodSec); Thread.sleep(1000); + ++waitedSec; pod = client.pods().inNamespace(namespace).withName(podId).get(); if (pod == null) { throw new IllegalStateException("Pod no longer exists: " + podId); @@ -165,7 +166,6 @@ public void launch(SlaveComputer computer, TaskListener listener) { break; } - ++waitedSec; } while (waitedSec < waitForPodSec); String status = pod.getStatus().getPhase(); if (!validStates.contains(status)) { @@ -174,9 +174,10 @@ public void launch(SlaveComputer computer, TaskListener listener) { } int waitForSlaveToConnect = unwrappedTemplate.getSlaveConnectTimeout(); + int waitedForSlave; // now wait for agent to be online - for (int waitedForSlave = 0; waitedForSlave < waitForSlaveToConnect; waitedForSlave++) { + for (waitedForSlave = 0; waitedForSlave < waitForSlaveToConnect; waitedForSlave++) { SlaveComputer slaveComputer = slave.getComputer(); if (slaveComputer == null) { throw new IllegalStateException("Node was deleted, computer is null"); @@ -184,6 +185,17 @@ public void launch(SlaveComputer computer, TaskListener listener) { if (slaveComputer.isOnline()) { break; } + + // Check that the pod hasn't failed already + pod = client.pods().inNamespace(namespace).withName(podId).get(); + if (pod == null) { + throw new IllegalStateException("Pod no longer exists: " + podId); + } + status = pod.getStatus().getPhase(); + if (!validStates.contains(status)) { + break; + } + LOGGER.log(INFO, "Waiting for agent to connect ({1}/{2}): {0}", new Object[]{podId, waitedForSlave, waitForSlaveToConnect}); logger.printf("Waiting for agent to connect (%2$s/%3$s): %1$s%n", @@ -193,7 +205,7 @@ public void launch(SlaveComputer computer, TaskListener listener) { if (slave.getComputer() == null || slave.getComputer().isOffline()) { logLastLines(containerStatuses, podId, namespace, slave, null, client); throw new IllegalStateException( - "Agent is not connected after " + waitForSlaveToConnect + " seconds, status: " + status); + "Agent is not connected after " + waitedForSlave + " seconds, status: " + status); } computer.setAcceptingTasks(true); } catch (Throwable ex) { diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlave.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlave.java index ad85220e42..db49c5aa98 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlave.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlave.java @@ -177,7 +177,7 @@ private static KubernetesCloud getKubernetesCloud(String cloudName) { if (cloud instanceof KubernetesCloud) { return (KubernetesCloud) cloud; } else { - throw new IllegalStateException(KubernetesSlave.class.getName() + " can be launched only by instances of " + KubernetesCloud.class.getName()); + throw new IllegalStateException(KubernetesSlave.class.getName() + " can be launched only by instances of " + KubernetesCloud.class.getName() + ". Cloud is " + cloud.getClass().getName()); } } From a30d673696b1c598968d6f03bb74145325583dcf Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Wed, 19 Dec 2018 16:37:44 +0100 Subject: [PATCH 2/3] Fix findbugs null check --- .../jenkins/plugins/kubernetes/KubernetesLauncher.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncher.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncher.java index 45d2bc30a5..69bff82a00 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncher.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncher.java @@ -177,8 +177,9 @@ public void launch(SlaveComputer computer, TaskListener listener) { int waitedForSlave; // now wait for agent to be online + SlaveComputer slaveComputer = null; for (waitedForSlave = 0; waitedForSlave < waitForSlaveToConnect; waitedForSlave++) { - SlaveComputer slaveComputer = slave.getComputer(); + slaveComputer = slave.getComputer(); if (slaveComputer == null) { throw new IllegalStateException("Node was deleted, computer is null"); } @@ -202,7 +203,7 @@ public void launch(SlaveComputer computer, TaskListener listener) { podId, waitedForSlave, waitForSlaveToConnect); Thread.sleep(1000); } - if (slave.getComputer() == null || slave.getComputer().isOffline()) { + if (slaveComputer == null || slaveComputer.isOffline()) { logLastLines(containerStatuses, podId, namespace, slave, null, client); throw new IllegalStateException( "Agent is not connected after " + waitedForSlave + " seconds, status: " + status); From 56bac7976a0ea622754b182f6e7740d5d9602bda Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Wed, 19 Dec 2018 16:55:48 +0100 Subject: [PATCH 3/3] Build only in linux Given that windows is failing due to JENKINS-55240 --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index a229fa5176..f6f0696a6d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1 +1 @@ -buildPlugin() +buildPlugin(platforms: ['linux']) \ No newline at end of file