Skip to content

Commit

Permalink
fix JENKINS-67679 add maintenanceMode to pause provisioning nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
fengxx committed Feb 22, 2022
1 parent a6d44ae commit 5543680
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
@CheckForNull
private PodRetention podRetention = PodRetention.getKubernetesCloudDefault();

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -563,6 +571,10 @@ public Collection<NodeProvisioner.PlannedNode> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
<f:entry title="Seconds to wait for pod to be running" field="waitForPodSec">
<f:number clazz="required number" min="0" step="1" default="${descriptor.defaultWaitForPod}"/>
</f:entry>

<f:entry title="${%Maintenance Mode}" field="maintenanceMode">
<f:checkbox default="false" />
</f:entry>
<f:advanced>
<f:entry title="${%Container Cleanup Timeout (minutes)}" field="retentionTimeout">
<f:number min="${descriptor.defaultRetentionTimeout}" default="${descriptor.defaultRetentionTimeout}" checkMethod="post"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Stop provisioning pods until maintenance is completed
</div>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -38,6 +36,8 @@

import jenkins.model.JenkinsLocationConfiguration;

import static org.junit.Assert.*;

public class KubernetesCloudTest {

@Rule
Expand Down Expand Up @@ -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<HtmlElement> inputs = root.getElementsByTagName("input");
for (HtmlElement input : inputs) {
Expand Down

0 comments on commit 5543680

Please sign in to comment.