Skip to content

Commit

Permalink
Merge pull request #1304 from jglick/lazy-pod
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlatombe authored Feb 1, 2023
2 parents 7ff395e + cd43a3c commit a982397
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 23 deletions.
14 changes: 8 additions & 6 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ stage('Tests') {
}
}
branches['jdk11'] = {
node('maven-11') {
timeout(60) {
checkout scm
sh 'mvn -B -ntp -Dset.changelist -Dmaven.test.failure.ignore clean install'
infra.prepareToPublishIncrementals()
junit 'target/surefire-reports/*.xml'
retry(count: 3, conditions: [kubernetesAgent(handleNonKubernetes: true), nonresumable()]) {
node('maven-11') {
timeout(60) {
checkout scm
sh 'mvn -B -ntp -Dset.changelist -Dmaven.test.failure.ignore clean install'
infra.prepareToPublishIncrementals()
junit 'target/surefire-reports/*.xml'
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ protected void _terminate(TaskListener listener) throws IOException, Interrupted
// Prior to termination, determine if we should delete the slave pod based on
// the slave pod's current state and the pod retention policy.
// Healthy slave pods should still have a JNLP agent running at this point.
Pod pod = client.pods().inNamespace(getNamespace()).withName(name).get();
boolean deletePod = getPodRetention(cloud).shouldDeletePod(cloud, pod);
boolean deletePod = getPodRetention(cloud).shouldDeletePod(cloud, () -> client.pods().inNamespace(getNamespace()).withName(name).get());

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import hudson.Extension;
import io.fabric8.kubernetes.api.model.Pod;
import java.util.function.Supplier;

public class Always extends PodRetention implements Serializable {

Expand All @@ -19,7 +20,7 @@ public Always() {
}

@Override
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
public boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> pod) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import hudson.model.Descriptor;
import hudson.model.DescriptorVisibilityFilter;
import io.fabric8.kubernetes.api.model.Pod;
import java.util.function.Supplier;

public class Default extends PodRetention implements Serializable {

Expand All @@ -21,7 +22,7 @@ public Default() {
}

@Override
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
public boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> pod) {
PodRetention parent = cloud.getPodRetention();
if (!(parent instanceof Default)) {
return parent.shouldDeletePod(cloud, pod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import hudson.Extension;
import io.fabric8.kubernetes.api.model.Pod;
import java.util.function.Supplier;

public class Never extends PodRetention implements Serializable {

Expand All @@ -19,7 +20,7 @@ public Never() {
}

@Override
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
public boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> pod) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@

import hudson.Extension;
import io.fabric8.kubernetes.api.model.Pod;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

public class OnFailure extends PodRetention implements Serializable {

private static final long serialVersionUID = 6424267627207206819L;

private static final Logger LOGGER = Logger.getLogger(OnFailure.class.getName());

@DataBoundConstructor
public OnFailure() {

}

@Override
public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
public boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> podS) {
Pod pod = null;
try {
pod = podS.get();
} catch (RuntimeException x) {
LOGGER.log(Level.WARNING, null, x);
}
if (pod == null || pod.getStatus() == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hudson.ExtensionPoint;
import hudson.model.AbstractDescribableImpl;
import io.fabric8.kubernetes.api.model.Pod;
import java.util.function.Supplier;

/**
* <code>PodRetention</code> instances determine if the Kubernetes pod running a Jenkins agent
Expand Down Expand Up @@ -41,7 +42,7 @@ public static PodRetention getPodTemplateDefault() {
*
* @return <code>true</code> if the agent pod should be deleted.
*/
public abstract boolean shouldDeletePod(KubernetesCloud cloud, Pod pod);
public abstract boolean shouldDeletePod(KubernetesCloud cloud, Supplier<Pod> pod);

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodStatus;
import io.fabric8.kubernetes.api.model.PodStatusBuilder;
import java.util.function.Supplier;

public class PodRetentionTest {

private KubernetesCloud cloud;
private Pod pod;
private Supplier<Pod> podS = () -> pod;

@Before
public void setUp() {
Expand All @@ -24,39 +26,39 @@ public void setUp() {
@Test
public void testAlwaysPodRetention() {
PodRetention subject = new Always();
assertFalse(subject.shouldDeletePod(cloud, pod));
assertFalse(subject.shouldDeletePod(cloud, podS));
}

@Test
public void testNeverPodRetention() {
PodRetention subject = new Never();
assertTrue(subject.shouldDeletePod(cloud, pod));
assertTrue(subject.shouldDeletePod(cloud, podS));
}

@Test
public void testDefaultPodRetention() {
PodRetention subject = new Default();
cloud.setPodRetention(new Always());
assertFalse(subject.shouldDeletePod(cloud, pod));
assertFalse(subject.shouldDeletePod(cloud, podS));
cloud.setPodRetention(new Never());
assertTrue(subject.shouldDeletePod(cloud, pod));
assertTrue(subject.shouldDeletePod(cloud, podS));
cloud.setPodRetention(new Default());
assertTrue(subject.shouldDeletePod(cloud, pod));
assertTrue(subject.shouldDeletePod(cloud, podS));
cloud.setPodRetention(null);
assertTrue(subject.shouldDeletePod(cloud, pod));
assertTrue(subject.shouldDeletePod(cloud, podS));
}

@Test
public void testOnFailurePodRetention() {
PodRetention subject = new OnFailure();
pod.setStatus(buildStatus("Failed"));
assertFalse(subject.shouldDeletePod(cloud, pod));
assertFalse(subject.shouldDeletePod(cloud, podS));
pod.setStatus(buildStatus("Unknown"));
assertFalse(subject.shouldDeletePod(cloud, pod));
assertFalse(subject.shouldDeletePod(cloud, podS));
pod.setStatus(buildStatus("Running"));
assertTrue(subject.shouldDeletePod(cloud, pod));
assertTrue(subject.shouldDeletePod(cloud, podS));
pod.setStatus(buildStatus("Pending"));
assertTrue(subject.shouldDeletePod(cloud, pod));
assertTrue(subject.shouldDeletePod(cloud, podS));
}

private PodStatus buildStatus(String phase) {
Expand Down

0 comments on commit a982397

Please sign in to comment.