Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminate running nodes after exiting podTemplate block #1095

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static KubernetesProvisioningLimits get() {
* @param numExecutors the number of executors (pretty much always 1)
*/
public synchronized boolean register(@NonNull KubernetesCloud cloud, @NonNull PodTemplate podTemplate, int numExecutors) {
LOGGER.log(Level.FINEST, "Registering {0} executors on cloud {1}, pod template {2}", new Object[]{numExecutors, cloud.name, podTemplate.getName()});
int newGlobalCount = getGlobalCount(cloud.name) + numExecutors;
if (newGlobalCount <= cloud.getContainerCap()) {
int newPodTemplateCount = getPodTemplateCount(podTemplate.getId()) + numExecutors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.lang.RandomStringUtils;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesFolderProperty;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave;
import org.csanchez.jenkins.plugins.kubernetes.Messages;
import org.csanchez.jenkins.plugins.kubernetes.PodImagePullSecret;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplate;
Expand Down Expand Up @@ -262,6 +263,18 @@ private PodTemplateCallback(PodTemplate podTemplate) {
protected void finished(StepContext context) throws Exception {
try {
KubernetesCloud cloud = resolveCloud();
LOGGER.log(Level.FINE, () -> "Terminating nodes of pod template " + podTemplate.getName());
Jenkins.get().getNodes()
.stream()
.filter(KubernetesSlave.class::isInstance)
.map(KubernetesSlave.class::cast)
.forEach(node -> {
try {
node.terminate();
} catch (InterruptedException | IOException e) {
LOGGER.log(Level.WARNING, "Failed to terminate " + node.getNodeName(), e);
}
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is removing ALL KubernetesSlave nodes, no? In my Jenkins, I have many jobs running at once, and each job declares it's own podTemplate step. How is this not terminating all such nodes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this would need to filter based on pod template, and if (after resolving merge conflicts) this passes all existing tests then a new test would need to be written which runs two podTemplate steps in parallel and lets one finish and asserts that the other continues running.

LOGGER.log(Level.FINE, () -> "Removing pod template " + podTemplate.getName()
+ " from cloud " + cloud.name);
cloud.removeDynamicTemplate(podTemplate);
Expand Down