From 57b41c38a763e86afa0136a3710b5d19f863b928 Mon Sep 17 00:00:00 2001 From: Ted Xiao Date: Tue, 15 Feb 2022 15:51:06 +0800 Subject: [PATCH] fix JENKINS-67679 add maintenanceMode to pause provisioning nodes --- .../plugins/kubernetes/KubernetesCloud.java | 14 +++++++++++- .../kubernetes/KubernetesCloud/config.jelly | 4 +++- .../KubernetesCloud/config_zh_CN.properties | 1 + .../KubernetesCloud/help-maintenanceMode.html | 3 +++ .../kubernetes/KubernetesCloudTest.java | 22 +++++++++++++++---- 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/help-maintenanceMode.html 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 bb9a58bee0..1b1e047599 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -131,7 +131,7 @@ public class KubernetesCloud extends Cloud { // Integer to differentiate null from 0 private Integer waitForPodSec = DEFAULT_WAIT_FOR_POD_SEC; - + private boolean maintenanceMode=false; @CheckForNull private PodRetention podRetention = PodRetention.getKubernetesCloudDefault(); @@ -499,6 +499,14 @@ public void setPodRetention(PodRetention podRetention) { this.podRetention = podRetention; } + public boolean isMaintenanceMode() { + return maintenanceMode; + } + @DataBoundSetter + public void setMaintenanceMode(boolean maintenanceMode) { + this.maintenanceMode = maintenanceMode; + } + /** * Connects to Kubernetes. * @@ -563,6 +571,10 @@ public Collection provision(@NonNull final Cloud.Cl @Override public boolean canProvision(@NonNull Cloud.CloudState state) { + if (isMaintenanceMode()){ + LOGGER.log(Level.FINE, "skip provision because maintenanceMode is set"); + return false; + } return getTemplate(state.getLabel()) != null; } diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/config.jelly b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/config.jelly index 93a44c4c66..66141ac951 100644 --- a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/config.jelly +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/config.jelly @@ -78,7 +78,9 @@ - + + + diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/config_zh_CN.properties b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/config_zh_CN.properties index 2895317a9d..6a6cede19d 100644 --- a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/config_zh_CN.properties +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/config_zh_CN.properties @@ -42,3 +42,4 @@ Images=\u955C\u50CF List\ of\ Images\ to\ be\ launched\ as\ agents=\u4F5C\u4E3A\u4EE3\u7406\u542F\u52A8\u7684\u955C\u50CF\u5217\u8868 Delete\ Template=\u5220\u9664\u6A21\u677F Add\ Pod\ Template=\u6DFB\u52A0 Pod \u6A21\u677F +Maintenance\ Mode=\u00e7\u00bb\u00b4\u00e6\u0160\u00a4\u00e6\u00a8\u00a1\u00e5\u00bc\ufffd \ No newline at end of file diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/help-maintenanceMode.html b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/help-maintenanceMode.html new file mode 100644 index 0000000000..dbc412438c --- /dev/null +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud/help-maintenanceMode.html @@ -0,0 +1,3 @@ +
+ Stop provisioning pods until maintenance is completed +
\ No newline at end of file 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 80be894630..6dcc343662 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java @@ -1,9 +1,5 @@ package org.csanchez.jenkins.plugins.kubernetes; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -21,6 +17,8 @@ import com.gargoylesoftware.htmlunit.html.HtmlFormUtil; import com.gargoylesoftware.htmlunit.html.HtmlInput; import com.gargoylesoftware.htmlunit.html.HtmlPage; +import hudson.model.Label; +import hudson.slaves.Cloud; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; @@ -38,6 +36,8 @@ import jenkins.model.JenkinsLocationConfiguration; +import static org.junit.Assert.*; + public class KubernetesCloudTest { @Rule @@ -297,6 +297,20 @@ public void readResolveContainerCapZero() { assertEquals(cloud.getContainerCap(), Integer.MAX_VALUE); } + @Test + public void maintenanceModeNoProvision() throws Exception { + String testNodeLabel="test-node"; + PodTemplate podTemplate=new PodTemplate("test"); + podTemplate.setLabel(testNodeLabel); + KubernetesCloud cloud = new KubernetesCloud("kubernetes"); + cloud.addTemplate(podTemplate); + Cloud.CloudState state=new Cloud.CloudState(Label.parseExpression(testNodeLabel),1); + assertTrue(cloud.canProvision(state)); + // disable provision + cloud.setMaintenanceMode(true); + assertFalse(cloud.canProvision(state)); + } + public HtmlInput getInputByName(DomElement root, String name) { DomNodeList inputs = root.getElementsByTagName("input"); for (HtmlElement input : inputs) {