diff --git a/pom.xml b/pom.xml index e2e0803b..c9fd6994 100644 --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,7 @@ 0.10.1 -SNAPSHOT - 2.375 + 2.407 2.8.0 diff --git a/src/main/java/org/jenkinsci/plugins/nomad/NomadCloud.java b/src/main/java/org/jenkinsci/plugins/nomad/NomadCloud.java index ea8a4e38..f6558c7a 100644 --- a/src/main/java/org/jenkinsci/plugins/nomad/NomadCloud.java +++ b/src/main/java/org/jenkinsci/plugins/nomad/NomadCloud.java @@ -134,8 +134,14 @@ public Collection provision(Label label, int excess if (prune) pruneOrphanedWorkers(template); + int existingNodes = 0; + // Let's avoid an useless request to nomad api if no MaxConcurrentJobs is configured + if(template.getMaxConcurrentJobs() >= 0) { + existingNodes = this.nomad.getRunningWorkers(template.getPrefix()).length; + } + try { - while (excessWorkload > 0) { + while (excessWorkload > 0 && checkExcessJobs(template,existingNodes,nodes.size())) { LOGGER.log(Level.INFO, "Excess workload of " + excessWorkload + ", provisioning new Jenkins worker on Nomad cluster"); final String workerName = template.createWorkerName(); @@ -156,6 +162,25 @@ public Collection provision(Label label, int excess return Collections.emptyList(); } + /** + * Determine if we reached the maximum number of job we can create for a specific template. + * If the maximum configured is negative, we consider this configuration as unlimited. + * @param template + * @param existingNodes + * @param created + * @return false if the template maximum configured jobs isn't reached, otherwise, return true + */ + private boolean checkExcessJobs(NomadWorkerTemplate template,int existingNodes, int created) { + int maxAllowed = template.getMaxConcurrentJobs(); + int maxAllowedLeft = maxAllowed - existingNodes - created; + + if (maxAllowed >= 0 && created >= maxAllowedLeft) { + LOGGER.log((Level.INFO), "Maximum jobs for template prefix: "+template.getPrefix()+" excedeed | Maximum: "+maxAllowed); + return false; + } + return true; + } + /** * Determines if some nomad worker needs to be stopped. diff --git a/src/main/java/org/jenkinsci/plugins/nomad/NomadWorkerTemplate.java b/src/main/java/org/jenkinsci/plugins/nomad/NomadWorkerTemplate.java index 1a5d354e..a387075c 100755 --- a/src/main/java/org/jenkinsci/plugins/nomad/NomadWorkerTemplate.java +++ b/src/main/java/org/jenkinsci/plugins/nomad/NomadWorkerTemplate.java @@ -29,6 +29,7 @@ public class NomadWorkerTemplate implements Describable { // persistent fields private final String prefix; + private final int maxConcurrentJobs; private final int idleTerminationInMinutes; private final boolean reusable; private final int numExecutors; @@ -98,6 +99,7 @@ public class NomadWorkerTemplate implements Describable { public NomadWorkerTemplate( String prefix, String labels, + int maxConcurrentJobs, int idleTerminationInMinutes, boolean reusable, int numExecutors, @@ -105,6 +107,7 @@ public NomadWorkerTemplate( String jobTemplate ) { this.prefix = prefix.isEmpty() ? SLAVE_PREFIX : prefix; + this.maxConcurrentJobs = maxConcurrentJobs; this.idleTerminationInMinutes = idleTerminationInMinutes; this.reusable = reusable; this.numExecutors = numExecutors; @@ -127,6 +130,10 @@ public String getPrefix() { return prefix; } + public int getMaxConcurrentJobs() { + return maxConcurrentJobs; + } + public int getIdleTerminationInMinutes() { return idleTerminationInMinutes; } @@ -201,6 +208,7 @@ public FormValidation doValidation( NomadWorkerTemplate template = new NomadWorkerTemplate( "validate-template", null, + -1, 0, false, 1, diff --git a/src/main/resources/org/jenkinsci/plugins/nomad/NomadWorkerTemplate/config.jelly b/src/main/resources/org/jenkinsci/plugins/nomad/NomadWorkerTemplate/config.jelly index df6a1979..884ba3a1 100755 --- a/src/main/resources/org/jenkinsci/plugins/nomad/NomadWorkerTemplate/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/nomad/NomadWorkerTemplate/config.jelly @@ -12,6 +12,10 @@ + + + + diff --git a/src/test/java/org/jenkinsci/plugins/nomad/NomadCloudTest.java b/src/test/java/org/jenkinsci/plugins/nomad/NomadCloudTest.java index 77d3e68e..f2a67c11 100644 --- a/src/test/java/org/jenkinsci/plugins/nomad/NomadCloudTest.java +++ b/src/test/java/org/jenkinsci/plugins/nomad/NomadCloudTest.java @@ -122,6 +122,7 @@ private NomadWorkerTemplate createTemplate(String labels) { return new NomadWorkerTemplate( "jenkins", labels, + -1, 1, true, 1,