Skip to content

Commit a982397

Browse files
authored
Merge pull request #1304 from jglick/lazy-pod
2 parents 7ff395e + cd43a3c commit a982397

File tree

8 files changed

+42
-23
lines changed

8 files changed

+42
-23
lines changed

Jenkinsfile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ stage('Tests') {
4242
}
4343
}
4444
branches['jdk11'] = {
45-
node('maven-11') {
46-
timeout(60) {
47-
checkout scm
48-
sh 'mvn -B -ntp -Dset.changelist -Dmaven.test.failure.ignore clean install'
49-
infra.prepareToPublishIncrementals()
50-
junit 'target/surefire-reports/*.xml'
45+
retry(count: 3, conditions: [kubernetesAgent(handleNonKubernetes: true), nonresumable()]) {
46+
node('maven-11') {
47+
timeout(60) {
48+
checkout scm
49+
sh 'mvn -B -ntp -Dset.changelist -Dmaven.test.failure.ignore clean install'
50+
infra.prepareToPublishIncrementals()
51+
junit 'target/surefire-reports/*.xml'
52+
}
5153
}
5254
}
5355
}

src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlave.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ protected void _terminate(TaskListener listener) throws IOException, Interrupted
310310
// Prior to termination, determine if we should delete the slave pod based on
311311
// the slave pod's current state and the pod retention policy.
312312
// Healthy slave pods should still have a JNLP agent running at this point.
313-
Pod pod = client.pods().inNamespace(getNamespace()).withName(name).get();
314-
boolean deletePod = getPodRetention(cloud).shouldDeletePod(cloud, pod);
313+
boolean deletePod = getPodRetention(cloud).shouldDeletePod(cloud, () -> client.pods().inNamespace(getNamespace()).withName(name).get());
315314

316315
Computer computer = toComputer();
317316
if (computer == null) {
@@ -367,6 +366,7 @@ private void deleteSlavePod(TaskListener listener, KubernetesClient client) thro
367366
e.getMessage());
368367
LOGGER.log(Level.WARNING, msg, e);
369368
listener.error(msg);
369+
// TODO should perhaps retry later, in case API server is just overloaded currently
370370
return;
371371
}
372372

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pod/retention/Always.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import hudson.Extension;
1010
import io.fabric8.kubernetes.api.model.Pod;
11+
import java.util.function.Supplier;
1112

1213
public class Always extends PodRetention implements Serializable {
1314

@@ -19,7 +20,7 @@ public Always() {
1920
}
2021

2122
@Override
22-
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
23+
public boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> pod) {
2324
return false;
2425
}
2526

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pod/retention/Default.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import hudson.model.Descriptor;
1111
import hudson.model.DescriptorVisibilityFilter;
1212
import io.fabric8.kubernetes.api.model.Pod;
13+
import java.util.function.Supplier;
1314

1415
public class Default extends PodRetention implements Serializable {
1516

@@ -21,7 +22,7 @@ public Default() {
2122
}
2223

2324
@Override
24-
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
25+
public boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> pod) {
2526
PodRetention parent = cloud.getPodRetention();
2627
if (!(parent instanceof Default)) {
2728
return parent.shouldDeletePod(cloud, pod);

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pod/retention/Never.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import hudson.Extension;
1010
import io.fabric8.kubernetes.api.model.Pod;
11+
import java.util.function.Supplier;
1112

1213
public class Never extends PodRetention implements Serializable {
1314

@@ -19,7 +20,7 @@ public Never() {
1920
}
2021

2122
@Override
22-
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
23+
public boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> pod) {
2324
return true;
2425
}
2526

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pod/retention/OnFailure.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@
88

99
import hudson.Extension;
1010
import io.fabric8.kubernetes.api.model.Pod;
11+
import java.util.function.Supplier;
12+
import java.util.logging.Level;
13+
import java.util.logging.Logger;
1114

1215
public class OnFailure extends PodRetention implements Serializable {
1316

1417
private static final long serialVersionUID = 6424267627207206819L;
1518

19+
private static final Logger LOGGER = Logger.getLogger(OnFailure.class.getName());
20+
1621
@DataBoundConstructor
1722
public OnFailure() {
1823

1924
}
2025

2126
@Override
22-
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
27+
public boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> podS) {
28+
Pod pod = null;
29+
try {
30+
pod = podS.get();
31+
} catch (RuntimeException x) {
32+
LOGGER.log(Level.WARNING, null, x);
33+
}
2334
if (pod == null || pod.getStatus() == null) {
2435
return false;
2536
}

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pod/retention/PodRetention.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hudson.ExtensionPoint;
66
import hudson.model.AbstractDescribableImpl;
77
import io.fabric8.kubernetes.api.model.Pod;
8+
import java.util.function.Supplier;
89

910
/**
1011
* <code>PodRetention</code> instances determine if the Kubernetes pod running a Jenkins agent
@@ -41,7 +42,7 @@ public static PodRetention getPodTemplateDefault() {
4142
*
4243
* @return <code>true</code> if the agent pod should be deleted.
4344
*/
44-
public abstract boolean shouldDeletePod(KubernetesCloud cloud, Pod pod);
45+
public abstract boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> pod);
4546

4647
@Override
4748
public String toString() {

src/test/java/org/csanchez/jenkins/plugins/kubernetes/pod/retention/PodRetentionTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import io.fabric8.kubernetes.api.model.Pod;
1010
import io.fabric8.kubernetes.api.model.PodStatus;
1111
import io.fabric8.kubernetes.api.model.PodStatusBuilder;
12+
import java.util.function.Supplier;
1213

1314
public class PodRetentionTest {
1415

1516
private KubernetesCloud cloud;
1617
private Pod pod;
18+
private Supplier<Pod> podS = () -> pod;
1719

1820
@Before
1921
public void setUp() {
@@ -24,39 +26,39 @@ public void setUp() {
2426
@Test
2527
public void testAlwaysPodRetention() {
2628
PodRetention subject = new Always();
27-
assertFalse(subject.shouldDeletePod(cloud, pod));
29+
assertFalse(subject.shouldDeletePod(cloud, podS));
2830
}
2931

3032
@Test
3133
public void testNeverPodRetention() {
3234
PodRetention subject = new Never();
33-
assertTrue(subject.shouldDeletePod(cloud, pod));
35+
assertTrue(subject.shouldDeletePod(cloud, podS));
3436
}
3537

3638
@Test
3739
public void testDefaultPodRetention() {
3840
PodRetention subject = new Default();
3941
cloud.setPodRetention(new Always());
40-
assertFalse(subject.shouldDeletePod(cloud, pod));
42+
assertFalse(subject.shouldDeletePod(cloud, podS));
4143
cloud.setPodRetention(new Never());
42-
assertTrue(subject.shouldDeletePod(cloud, pod));
44+
assertTrue(subject.shouldDeletePod(cloud, podS));
4345
cloud.setPodRetention(new Default());
44-
assertTrue(subject.shouldDeletePod(cloud, pod));
46+
assertTrue(subject.shouldDeletePod(cloud, podS));
4547
cloud.setPodRetention(null);
46-
assertTrue(subject.shouldDeletePod(cloud, pod));
48+
assertTrue(subject.shouldDeletePod(cloud, podS));
4749
}
4850

4951
@Test
5052
public void testOnFailurePodRetention() {
5153
PodRetention subject = new OnFailure();
5254
pod.setStatus(buildStatus("Failed"));
53-
assertFalse(subject.shouldDeletePod(cloud, pod));
55+
assertFalse(subject.shouldDeletePod(cloud, podS));
5456
pod.setStatus(buildStatus("Unknown"));
55-
assertFalse(subject.shouldDeletePod(cloud, pod));
57+
assertFalse(subject.shouldDeletePod(cloud, podS));
5658
pod.setStatus(buildStatus("Running"));
57-
assertTrue(subject.shouldDeletePod(cloud, pod));
59+
assertTrue(subject.shouldDeletePod(cloud, podS));
5860
pod.setStatus(buildStatus("Pending"));
59-
assertTrue(subject.shouldDeletePod(cloud, pod));
61+
assertTrue(subject.shouldDeletePod(cloud, podS));
6062
}
6163

6264
private PodStatus buildStatus(String phase) {

0 commit comments

Comments
 (0)