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

Adding basic retention #91

Merged
merged 8 commits into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
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
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you just call

this(template, nodeDescription, cloud, label, new OnceRetentionStrategy(cloud.getRetentionTimeout()))

to avoid duplicated code

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