Skip to content

Commit

Permalink
Adding basic retention (#91)
Browse files Browse the repository at this point in the history
* Adding retention for idle slaves
* Using the idleTimeout setting properly
  • Loading branch information
jeffersongirao authored and carlossg committed Nov 30, 2016
1 parent 5d3a949 commit 78b1c25
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
<version>4.5.1</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>durable-task</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
import hudson.model.Node;
import hudson.security.ACL;
import hudson.slaves.Cloud;
import hudson.slaves.CloudRetentionStrategy;
import hudson.slaves.NodeProvisioner;
import hudson.slaves.RetentionStrategy;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import io.fabric8.kubernetes.api.model.Container;
Expand All @@ -73,6 +75,7 @@
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;
import static org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.substituteEnv;
import org.jenkinsci.plugins.durabletask.executors.OnceRetentionStrategy;

/**
* Kubernetes cloud provider.
Expand Down Expand Up @@ -539,9 +542,14 @@ public ProvisioningCallback(KubernetesCloud cloud, PodTemplate t, Label label) {

public Node call() throws Exception {
KubernetesSlave slave = null;
RetentionStrategy retentionStrategy = null;
try {

slave = new KubernetesSlave(t, t.getName(), cloud, t.getLabel());
if (t.getIdleMinutes() == 0) {
retentionStrategy = new OnceRetentionStrategy(cloud.getRetentionTimeout());
} else {
retentionStrategy = new CloudRetentionStrategy(t.getIdleMinutes());
}
slave = new KubernetesSlave(t, t.getName(), cloud, t.getLabel(), retentionStrategy);
Jenkins.getActiveInstance().addNode(slave);

Pod pod = getPodTemplate(slave, label);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import hudson.model.TaskListener;
import hudson.slaves.AbstractCloudSlave;
import hudson.slaves.JNLPLauncher;
import hudson.slaves.NodeProperty;
import hudson.slaves.OfflineCause;
import hudson.slaves.RetentionStrategy;
import io.fabric8.kubernetes.api.model.DoneablePod;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
Expand All @@ -44,34 +44,31 @@ public class KubernetesSlave extends AbstractCloudSlave {

private transient final KubernetesCloud cloud;

@DataBoundConstructor
public KubernetesSlave(PodTemplate template, String nodeDescription, KubernetesCloud cloud, String labelStr)
throws Descriptor.FormException, IOException {
super(getSlaveName(template),
nodeDescription,
template.getRemoteFs(),
1,
Node.Mode.NORMAL,
labelStr == null ? null : labelStr,
new JNLPLauncher(),
new OnceRetentionStrategy(cloud.getRetentionTimeout()),
template.getNodeProperties());

// this.pod = pod;
this.cloud = cloud;
this(template, nodeDescription, cloud, labelStr, new OnceRetentionStrategy(cloud.getRetentionTimeout()));
}

@Deprecated
public KubernetesSlave(PodTemplate template, String nodeDescription, KubernetesCloud cloud, Label label)
throws Descriptor.FormException, IOException {
this(template, nodeDescription, cloud, label.toString(), new OnceRetentionStrategy(cloud.getRetentionTimeout())) ;
}

@DataBoundConstructor
public KubernetesSlave(PodTemplate template, String nodeDescription, KubernetesCloud cloud, String labelStr,
RetentionStrategy rs)
throws Descriptor.FormException, IOException {

super(getSlaveName(template),
nodeDescription,
template.getRemoteFs(),
1,
Node.Mode.NORMAL,
label == null ? null : label.toString(),
labelStr == null ? null : labelStr,
new JNLPLauncher(),
new OnceRetentionStrategy(cloud.getRetentionTimeout()),
rs,
template.getNodeProperties());

// this.pod = pod;
Expand Down Expand Up @@ -114,7 +111,7 @@ protected void _terminate(TaskListener listener) throws IOException, Interrupted
computer.disconnect(OfflineCause.create(new Localizable(HOLDER, "offline")));
LOGGER.log(Level.INFO, "Disconnected computer {0}", name);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failure to terminate instance for slave " + name, e);
LOGGER.log(Level.SEVERE, "Failed to terminate pod for slave " + name, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import hudson.model.Label;
import hudson.model.labels.LabelAtom;

import org.apache.commons.lang3.StringUtils;

/**
* Kubernetes Pod Template
*
Expand Down Expand Up @@ -51,6 +53,8 @@ public class PodTemplate extends AbstractDescribableImpl<PodTemplate> {

private int instanceCap = Integer.MAX_VALUE;

private int idleMinutes;

private String label;

private String serviceAccount;
Expand Down Expand Up @@ -188,7 +192,7 @@ public int getInstanceCap() {

@DataBoundSetter
public void setInstanceCapStr(String instanceCapStr) {
if ("".equals(instanceCapStr)) {
if (StringUtils.isBlank(instanceCapStr)) {
setInstanceCap(Integer.MAX_VALUE);
} else {
setInstanceCap(Integer.parseInt(instanceCapStr));
Expand All @@ -203,6 +207,31 @@ public String getInstanceCapStr() {
}
}

public void setIdleMinutes(int i) {
this.idleMinutes = i;
}

public int getIdleMinutes() {
return idleMinutes;
}

@DataBoundSetter
public void setIdleMinutesStr(String idleMinutes) {
if (StringUtils.isBlank(idleMinutes)) {
setIdleMinutes(0);
} else {
setIdleMinutes(Integer.parseInt(idleMinutes));
}
}

public String getIdleMinutesStr() {
if (getIdleMinutes() == 0) {
return "";
} else {
return String.valueOf(idleMinutes);
}
}

public Set<LabelAtom> getLabelSet() {
return Label.parse(label);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
<f:textbox/>
</f:entry>

<f:entry field="idleMinutesStr" title="${%Time in minutes to retain slave when idle}">
<f:textbox/>
</f:entry>

<f:entry title="${%Annotations}" description="${%List of annotations to set in slave pod}">
<f:repeatableHeteroProperty field="annotations" hasHeader="true" addCaption="Add Annotation"
deleteCaption="Delete annotation Variable" />
Expand Down

0 comments on commit 78b1c25

Please sign in to comment.