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

[JENKINS-67679] add maintenance mode to pause provisioning nodes #1134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();

@@ -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<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;
}

Original file line number Diff line number Diff line change
@@ -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"/>
Original file line number Diff line number Diff line change
@@ -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;
@@ -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<HtmlElement> inputs = root.getElementsByTagName("input");
for (HtmlElement input : inputs) {