diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java index eca6a20d17..06137a3687 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -390,12 +390,12 @@ public void setContainerCapStr(String containerCapStr) { } public void setContainerCap(Integer containerCap) { - this.containerCap = (containerCap != null && containerCap >= 0) ? containerCap : null; + this.containerCap = (containerCap != null && containerCap > 0) ? containerCap : null; } public String getContainerCapStr() { - // serialized Integer.MAX_VALUE means no limit - return (containerCap == null || containerCap == Integer.MAX_VALUE) ? "" : String.valueOf(containerCap); + // null, serialized Integer.MAX_VALUE, or 0 means no limit + return (containerCap == null || containerCap == Integer.MAX_VALUE || containerCap == 0) ? "" : String.valueOf(containerCap); } public int getReadTimeout() { @@ -539,7 +539,7 @@ public synchronized Collection provision(@CheckForN for (PodTemplate t: getTemplatesFor(label)) { LOGGER.log(Level.INFO, "Template for label {0}: {1}", new Object[] { label, t.getName() }); for (int i = 0; i < toBeProvisioned; i++) { - if (!addProvisionedSlave(t, label, i)) { + if (!mayAddProvisionedSlave(t, label, i)) { break; } r.add(PlannedNodeBuilderFactory.createInstance().cloud(this).template(t).label(label).build()); @@ -573,11 +573,8 @@ public synchronized Collection provision(@CheckForN * Check not too many already running. * */ - private boolean addProvisionedSlave(@Nonnull PodTemplate template, @CheckForNull Label label, int scheduledCount) throws Exception { + private boolean mayAddProvisionedSlave(@Nonnull PodTemplate template, @CheckForNull Label label, int scheduledCount) throws Exception { int containerCap = getContainerCap(); - if (containerCap == 0) { - return false; - } KubernetesClient client = connect(); String templateNamespace = template.getNamespace(); @@ -588,23 +585,27 @@ private boolean addProvisionedSlave(@Nonnull PodTemplate template, @CheckForNull } Map podLabels = getPodLabelsMap(); - List allActiveSlavePods = getActiveSlavePods(client, templateNamespace, podLabels); - if (allActiveSlavePods != null && containerCap <= allActiveSlavePods.size() + scheduledCount) { - LOGGER.log(Level.INFO, - "Maximum number of concurrently running agent pods ({0}) reached for Kubernetes Cloud {4}, not provisioning: {1} running or pending in namespace {2} with Kubernetes labels {3}", - new Object[] { containerCap, allActiveSlavePods.size() + scheduledCount, templateNamespace, getLabels(), name }); - return false; + if (containerCap != Integer.MAX_VALUE) { // skip check when global concurrency limit is "unlimited" + List allActiveSlavePods = getActiveSlavePods(client, templateNamespace, podLabels); + if (allActiveSlavePods != null && containerCap <= allActiveSlavePods.size() + scheduledCount) { + LOGGER.log(Level.INFO, + "Maximum number of concurrently running agent pods ({0}) reached for Kubernetes Cloud {4}, not provisioning: {1} running or pending in namespace {2} with Kubernetes labels {3}", + new Object[]{containerCap, allActiveSlavePods.size() + scheduledCount, templateNamespace, getLabels(), name}); + return false; + } } Map labelsMap = new HashMap<>(podLabels); labelsMap.putAll(template.getLabelsMap()); - List activeTemplateSlavePods = getActiveSlavePods(client, templateNamespace, labelsMap); - if (activeTemplateSlavePods != null && allActiveSlavePods != null && template.getInstanceCap() <= activeTemplateSlavePods.size() + scheduledCount) { - LOGGER.log(Level.INFO, - "Maximum number of concurrently running agent pods ({0}) reached for template {1} in Kubernetes Cloud {6}, not provisioning: {2} running or pending in namespace {3} with label \"{4}\" and Kubernetes labels {5}", - new Object[] { template.getInstanceCap(), template.getName(), activeTemplateSlavePods.size() + scheduledCount, - templateNamespace, label == null ? "" : label.toString(), labelsMap, name }); - return false; + if (template.getInstanceCap() != Integer.MAX_VALUE) { // skip check when template concurrency limit is "unlimited" + List activeTemplateSlavePods = getActiveSlavePods(client, templateNamespace, labelsMap); + if (activeTemplateSlavePods != null && template.getInstanceCap() <= activeTemplateSlavePods.size() + scheduledCount) { + LOGGER.log(Level.INFO, + "Maximum number of concurrently running agent pods ({0}) reached for template {1} in Kubernetes Cloud {6}, not provisioning: {2} running or pending in namespace {3} with label \"{4}\" and Kubernetes labels {5}", + new Object[]{template.getInstanceCap(), template.getName(), activeTemplateSlavePods.size() + scheduledCount, + templateNamespace, label == null ? "" : label.toString(), labelsMap, name}); + return false; + } } return true; } diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/help-containerCapStr.html b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/help-containerCapStr.html index ff816d1b0f..21cc17bc84 100644 --- a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/help-containerCapStr.html +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/help-containerCapStr.html @@ -1,4 +1,4 @@
The maximum number of concurrently running agent pods that are permitted in this Kubernetes Cloud. - If set to empty it means no limit. Set to 0 means no pod will ever be started. + If set to empty it means no limit.
diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java index d0920a339f..01f9d213fd 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java @@ -221,11 +221,6 @@ public KubernetesClient connect() { Collection plannedNodes = cloud.provision(test, 200); assertEquals(200, plannedNodes.size()); - cloud.setContainerCapStr("0"); - podTemplate.setInstanceCap(20); - plannedNodes = cloud.provision(test, 200); - assertEquals(0, plannedNodes.size()); - cloud.setContainerCapStr("10"); podTemplate.setInstanceCap(20); plannedNodes = cloud.provision(test, 200);