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

Added constraints to Nomad Cloud plugin #17

Merged
merged 2 commits into from
Jan 5, 2018
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>

<!-- TESTS -->
<dependency>
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/org/jenkinsci/plugins/nomad/Api/Constraint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.jenkinsci.plugins.nomad.Api;

import org.jenkinsci.plugins.nomad.NomadConstraintTemplate;
import java.util.List;

public class Constraint {
private String LTarget;
private String Operand;
private String RTarget;

public Constraint (
String ltarget,
String operand,
String rtarget
) {
LTarget = ltarget;
Operand = operand;
RTarget = rtarget;
}

public Constraint (
NomadConstraintTemplate nomadConstraintTemplate
) {
LTarget = nomadConstraintTemplate.getLtarget();
Operand = nomadConstraintTemplate.getOperand();
RTarget = nomadConstraintTemplate.getRtarget();
}

public String getLtarget() {
return LTarget;
}

public void setLtarget(String ltarget) {
LTarget = ltarget;
}

public String getOperand() {
return Operand;
}

public void setOperand(String operand) {
Operand = operand;
}

public String getRtarget() {
return RTarget;
}

public void setRtarget(String rtarget) {
RTarget = rtarget;
}
}
31 changes: 31 additions & 0 deletions src/main/java/org/jenkinsci/plugins/nomad/Api/ConstraintGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jenkinsci.plugins.nomad.Api;
import java.util.*;
import org.jenkinsci.plugins.nomad.NomadConstraintTemplate;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

public class ConstraintGroup {
private List<Constraint> constraints = new ArrayList<Constraint>();

public ConstraintGroup (
List<NomadConstraintTemplate> constraintTemplate
) {
Iterator<NomadConstraintTemplate> constraintIterator = constraintTemplate.iterator();
while (constraintIterator.hasNext()) {
NomadConstraintTemplate nextTemplate = constraintIterator.next();
constraints.add(new Constraint(nextTemplate));
}
}

public List<Constraint> getConstraints() {
Iterator<Constraint> constraintIterator = constraints.iterator();

List<Constraint> Constraints = new ArrayList<Constraint>();

while (constraintIterator.hasNext()) {
Constraint nextConstraint = constraintIterator.next();
Constraints.add(nextConstraint);
}
return Constraints;
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/jenkinsci/plugins/nomad/Api/Job.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.jenkinsci.plugins.nomad.Api;

import java.util.List;
import org.jenkinsci.plugins.nomad.NomadConstraintTemplate;

public final class Job {

private String ID;
Expand All @@ -8,6 +11,7 @@ public final class Job {
private String Type;
private Integer Priority;
private String[] Datacenters;
private List<Constraint> Constraints;
private TaskGroup[] TaskGroups;

public Job(
Expand All @@ -17,6 +21,7 @@ public Job(
String type,
Integer priority,
String[] datacenters,
List<Constraint> constraints,
TaskGroup[] taskGroups)
{
this.ID = ID;
Expand All @@ -25,6 +30,7 @@ public Job(
Type = type;
Priority = priority;
Datacenters = datacenters;
Constraints = constraints;
TaskGroups = taskGroups;
}

Expand Down Expand Up @@ -84,4 +90,12 @@ public void setTaskGroups(TaskGroup[] taskGroups) {
TaskGroups = taskGroups;
}

public List<Constraint> getConstraints() {
return Constraints;
}

public void setConstraints(List<Constraint> constraints) {
Constraints = constraints;
}

}
6 changes: 6 additions & 0 deletions src/main/java/org/jenkinsci/plugins/nomad/NomadApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;

import java.util.List;

public final class NomadApi {

private static final Logger LOGGER = Logger.getLogger(NomadApi.class.getName());
Expand Down Expand Up @@ -149,13 +151,17 @@ String buildSlaveJob(
new EphemeralDisk(template.getDisk(), false, false)
);

ConstraintGroup constraintGroup = new ConstraintGroup(template.getConstraints());
List<Constraint> Constraints = constraintGroup.getConstraints();

Job job = new Job(
name,
name,
template.getRegion(),
"batch",
template.getPriority(),
template.getDatacenters().split(","),
Constraints,
new TaskGroup[]{taskGroup}
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.jenkinsci.plugins.nomad;

import hudson.Extension;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Util;
import hudson.model.Describable;
import hudson.model.Descriptor;
import jenkins.model.Jenkins;

import javax.annotation.Nullable;
import java.util.*;
import java.util.logging.Logger;

import org.json.JSONObject;

public class NomadConstraintTemplate implements Describable<NomadConstraintTemplate> {

private final String ltarget;
private final String operand;
private final String rtarget;

private NomadSlaveTemplate slave;

@DataBoundConstructor
public NomadConstraintTemplate(
String ltarget,
String operand,
String rtarget
) {
this.ltarget = ltarget;
this.operand = operand;
this.rtarget = rtarget;
readResolve();
}

protected Object readResolve() {
return this;
}


@Extension
public static final class DescriptorImpl extends Descriptor<NomadConstraintTemplate> {

public DescriptorImpl() {
load();
}

@Override
public String getDisplayName() {
return null;
}
}

@Override
@SuppressWarnings("unchecked")
public Descriptor<NomadConstraintTemplate> getDescriptor() {
return Jenkins.getInstance().getDescriptor(getClass());
}

public String getLtarget() {
return ltarget;
}

public String getOperand() {
return operand;
}

public String getRtarget() {
return rtarget;
}

public NomadSlaveTemplate getNomadSlaveTemplate() {
return slave;
}

public void setNomadSlaveTemplate(NomadSlaveTemplate slave) {
this.slave = slave;
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/jenkinsci/plugins/nomad/NomadSlaveTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
import hudson.model.labels.LabelAtom;
import jenkins.model.Jenkins;

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

import javax.annotation.Nullable;
import java.util.*;
import java.util.logging.Logger;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

public class NomadSlaveTemplate implements Describable<NomadSlaveTemplate> {

private static final String SLAVE_PREFIX = "jenkins-";
Expand All @@ -28,6 +34,7 @@ public class NomadSlaveTemplate implements Describable<NomadSlaveTemplate> {
private final int disk;
private final int priority;
private final String labels;
private final List<? extends NomadConstraintTemplate> constraints;
private final String region;
private final String remoteFs;
private final String image;
Expand All @@ -49,6 +56,7 @@ public NomadSlaveTemplate(
String memory,
String disk,
String labels,
List<? extends NomadConstraintTemplate> constraints,
String remoteFs,
String idleTerminationInMinutes,
String numExecutors,
Expand All @@ -72,6 +80,11 @@ public NomadSlaveTemplate(
this.mode = mode;
this.remoteFs = remoteFs;
this.labels = Util.fixNull(labels);
if (constraints == null) {
this.constraints = Collections.emptyList();
} else {
this.constraints = constraints;
}
this.labelSet = Label.parse(labels);
this.region = region;
this.image = image;
Expand Down Expand Up @@ -138,6 +151,10 @@ public String getLabels() {
return labels;
}

public List<NomadConstraintTemplate> getConstraints() {
return Collections.unmodifiableList(constraints);
}

public int getIdleTerminationInMinutes() {
return idleTerminationInMinutes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?jelly escape-by-default='false'?>

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<table width="100%">

<f:entry title="LTarget" field="ltarget">
<f:textbox/>
</f:entry>

<f:entry title="Operand" field="operand">
<f:textbox/>
</f:entry>

<f:entry title="RTarget" field="rtarget">
<f:textbox/>
</f:entry>

<f:entry>
<f:repeatableDeleteButton />
</f:entry>

</table>

</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
<f:textbox/>
</f:entry>

<f:entry title="Constraints" help="/plugin/nomad/help-constraints.html">
<f:repeatable field="constraints">
<st:include page="/org/jenkinsci/plugins/nomad/NomadConstraintTemplate/config.jelly" class="org.jenkinsci.plugins.nomad.NomadConstraintTemplate$DescriptorImpl"/>
</f:repeatable>
</f:entry>

<f:entry title="Disk" field="disk">
<f:textbox/>
</f:entry>
Expand Down
3 changes: 3 additions & 0 deletions src/main/webapp/help-constraints.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Read the JSON synax and Constraints API pages on nomadproject.io to see how to apply constraints (these apply on the Job level)
</div>
5 changes: 4 additions & 1 deletion src/test/java/org/jenkinsci/plugins/nomad/NomadApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

import java.util.Collections;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.ArrayList;

/**
* @author Yegor Andreenko
*/
public class NomadApiTest {

private NomadApi nomadApi = new NomadApi("http://localhost");
private List<NomadConstraintTemplate> constraintTest = new ArrayList<NomadConstraintTemplate>();
private NomadSlaveTemplate slaveTemplate = new NomadSlaveTemplate(
"300", "256", "100",
null, "remoteFs", "3","1", Node.Mode.NORMAL,
null, constraintTest, "remoteFs", "3","1", Node.Mode.NORMAL,
"ams", "0", "image", "dc01", "", "", false, "bridge",""
);

Expand Down