Skip to content

Commit

Permalink
#168 Add support of configuration as code
Browse files Browse the repository at this point in the history
- Upgrade min supported Jenkins version to 2.112
  • Loading branch information
terma authored Jan 30, 2020
1 parent 7f84142 commit f435798
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 90 deletions.
8 changes: 7 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#!groovy

buildPlugin(jenkinsVersions: [null, '2.60.1', '2.107.1'], failFast: false)
// null is min supported version of jenkins from plugin pom.xml
def minRequiredForPlugin = null

// from LTS releases https://jenkins.io/changelog-stable/
def lts = '2.164.1'

buildPlugin(jenkinsVersions: [minRequiredForPlugin, lts], failFast: false)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ automatically scaling the capacity with the load.
* [Preconfigure Slave](#preconfigure-slave)
* [Label Based Configuration (beta)](docs/LABEL-BASED-CONFIGURATION.md)
* [Windows Slave](docs/SETUP-WINDOWS-SLAVE.md)
* [Configuration As Code](docs/CONFIGURATION-AS-CODE.md)
* [Development](#development)

# Overview
Expand Down
93 changes: 93 additions & 0 deletions docs/CONFIGURATION-AS-CODE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Configuration As Code

[Jenkins Configuration As Code](https://jenkins.io/projects/jcasc/)

## Properties

### EC2FleetCloud

[Definition](https://github.com/jenkinsci/ec2-fleet-plugin/blob/master/src/main/java/com/amazon/jenkins/ec2fleet/EC2FleetCloud.java#L156-L179)

| Property | Type | Required | Description |
|-------------------|---|---|---|
| name |string|yes|ec2-fleet|
| awsCredentialsId |string|yes||
| computerConnector |object|yes|for example ```sshConnector```|
| region |string|yes|```us-east-2```, full [list](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html)|
| fleet |string|yes|my-fleet|
| endpoint|string|no|Set only if you need to use custome endpoint ```http://a.com```|
| fsRoot |string|no|my-root|
| privateIpUsed|boolean|no, default ```false```|connect to EC2 instance by private id instead of public|
| alwaysReconnect|boolean|no, default ```false```||
| labelString |string|yes||
| idleMinutes |int|no, default ```0```||
| minSize |int|no, default ```0```||
| maxSize |int|no, default ```0```||
| numExecutors|int|no, default ```1```||
| addNodeOnlyIfRunning|boolean|no, default ```false```||
| restrictUsage|boolean|no, default ```false```|if ```true``` fleet nodes will executed only jobs with same label|
| scaleExecutorsByWeight|boolean|no, default ```false```||
| initOnlineTimeoutSec|int|no, default ```180```||
| initOnlineCheckIntervalSec|int|no, default ```15```||
| cloudStatusIntervalSec|int|no, default ```10```||
| disableTaskResubmit|boolean|no, default ```false```||
| noDelayProvision|boolean|no, default ```false```||

## EC2FleetLabelCloud

More about this type [here](LABEL-BASED-CONFIGURATION.md)

[Definition](https://github.com/jenkinsci/ec2-fleet-plugin/blob/master/src/main/java/com/amazon/jenkins/ec2fleet/EC2FleetLabelCloud.java#L123-L145)

## Examples

### EC2FleetCloud (min set of properties)

```yaml
jenkins:
clouds:
- ec2Fleet:
name: ec2-fleet
awsCredentialsId: xx
computerConnector:
ssh:
credentialsId: cred
sshHostKeyVerificationStrategy:
NonVerifyingKeyVerificationStrategy
region: us-east-2
fleet: my-fleet
minSize: 1
maxSize: 10
```
### EC2FleetCloud (All properties)
```yaml
jenkins:
clouds:
- ec2Fleet:
name: ec2-fleet
awsCredentialsId: xx
computerConnector:
sshConnector:
credentialsId: cred
region: us-east-2
endpoint: http://a.com
fleet: my-fleet
fsRoot: my-root
privateIpUsed: true
alwaysReconnect: true
labelString: myLabel
idleMinutes: 33
minSize: 15
maxSize: 90
numExecutors: 12
addNodeOnlyIfRunning: true
restrictUsage: true
scaleExecutorsByWeight: true
initOnlineTimeoutSec: 181
initOnlineCheckIntervalSec: 13
cloudStatusIntervalSec: 11
disableTaskResubmit: true
noDelayProvision: true
```
51 changes: 40 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.6</version>
<version>3.56</version>
<relativePath />
</parent>

Expand All @@ -15,8 +15,9 @@
<packaging>hpi</packaging>

<properties>
<jenkins.version>1.625.3</jenkins.version>
<java.level>7</java.level>
<jenkins.version>2.112</jenkins.version>
<java.level>8</java.level>
<configuration-as-code.version>1.14</configuration-as-code.version>
</properties>

<name>EC2 Fleet Jenkins Plugin</name>
Expand Down Expand Up @@ -95,13 +96,20 @@
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<version>2.0.5</version>
<scope>test</scope>
<exclusions>
<!-- use old version, parent required new one -->
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<version>2.0.5</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand All @@ -110,12 +118,33 @@
</exclusion>
</exclusions>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.jenkins-ci.plugins</groupId>-->
<!-- <artifactId>jdk-tool</artifactId>-->
<!-- <version>1.3</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>

<!-- before jenkins 2.112 this plugin was part of jenkins core -->
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>configuration-as-code-support</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jdk-tool</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amazon.jenkins.ec2fleet;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.Action;
import hudson.model.Actionable;
import hudson.model.Executor;
Expand Down Expand Up @@ -72,6 +73,9 @@ public EC2FleetAutoResubmitComputerLauncher(final ComputerLauncher launcher) {
* @param computer computer
* @param listener listener
*/
@SuppressFBWarnings(
value = "BC_UNCONFIRMED_CAST",
justification = "to ignore EC2FleetNodeComputer cast")
@Override
public void afterDisconnect(final SlaveComputer computer, final TaskListener listener) {
// according to jenkins docs could be null in edge cases, check ComputerLauncher.afterDisconnect
Expand Down
52 changes: 12 additions & 40 deletions src/main/java/com/amazon/jenkins/ec2fleet/EC2FleetCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public class EC2FleetCloud extends AbstractEC2FleetCloud {
private final boolean alwaysReconnect;
private final String labelString;
private final Integer idleMinutes;
private final Integer minSize;
private final Integer maxSize;
private final Integer numExecutors;
private final int minSize;
private final int maxSize;
private final int numExecutors;
private final boolean addNodeOnlyIfRunning;
private final boolean restrictUsage;
private final boolean scaleExecutorsByWeight;
Expand Down Expand Up @@ -166,9 +166,9 @@ public EC2FleetCloud(final String name,
final boolean privateIpUsed,
final boolean alwaysReconnect,
final Integer idleMinutes,
final Integer minSize,
final Integer maxSize,
final Integer numExecutors,
final int minSize,
final int maxSize,
final int numExecutors,
final boolean addNodeOnlyIfRunning,
final boolean restrictUsage,
final boolean disableTaskResubmit,
Expand All @@ -192,7 +192,7 @@ public EC2FleetCloud(final String name,
this.alwaysReconnect = alwaysReconnect;
this.minSize = minSize;
this.maxSize = maxSize;
this.numExecutors = numExecutors;
this.numExecutors = Math.max(numExecutors, 1);
this.addNodeOnlyIfRunning = addNodeOnlyIfRunning;
this.restrictUsage = restrictUsage;
this.scaleExecutorsByWeight = scaleExecutorsByWeight;
Expand Down Expand Up @@ -289,15 +289,15 @@ public int getIdleMinutes() {
return (idleMinutes != null) ? idleMinutes : 0;
}

public Integer getMaxSize() {
public int getMaxSize() {
return maxSize;
}

public Integer getMinSize() {
public int getMinSize() {
return minSize;
}

public Integer getNumExecutors() {
public int getNumExecutors() {
return numExecutors;
}

Expand Down Expand Up @@ -414,8 +414,8 @@ public FleetStateStats update() {
info("Fleet under modification, try update later, %s", currentState.getState().getDetailed());
synchronized (this) {
stats = currentState;
return stats;
}
return stats;
}

// fleet could be updated outside of plugin, we should be ready that
Expand Down Expand Up @@ -445,9 +445,8 @@ public FleetStateStats update() {
// cancel to let jenkins no that node is not valid any more
plannedNodeToCancel.future.cancel(true);
}
return stats;
}

return stats;
}

private void updateByState(
Expand Down Expand Up @@ -737,13 +736,6 @@ private void warning(final Throwable t, final String msg, final Object... args)
@SuppressWarnings("unused")
public static class DescriptorImpl extends Descriptor<Cloud> {

public String accessId;
public String secretKey;
public String region;
public String privateKey;
public String fleet;
public boolean showAllFleets;

public DescriptorImpl() {
super();
load();
Expand Down Expand Up @@ -828,26 +820,6 @@ public boolean configure(final StaplerRequest req, final JSONObject formData) th
return super.configure(req, formData);
}

public boolean isShowAllFleets() {
return showAllFleets;
}

public String getAccessId() {
return accessId;
}

public String getSecretKey() {
return secretKey;
}

public String getRegion() {
return region;
}

public String getFleet() {
return fleet;
}

}

}
21 changes: 0 additions & 21 deletions src/main/java/com/amazon/jenkins/ec2fleet/EC2FleetLabelCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,6 @@ public void updateStacks() {
Set<String> labelsWithoutStacks = missed(labels, allStacks.keySet());
Set<String> stacksWithoutLabels = missed(allStacks.keySet(), labels);

Set<String> stacksWithLabels = new HashSet<>(allStacks.keySet());
stacksWithLabels.removeAll(stacksWithoutLabels);

final Set<String> runningStacksWithLabels = new HashSet<>();
for (Map.Entry<String, CloudFormationApi.StackInfo> stack : allStacks.entrySet()) {
if (labels.contains(stack.getKey()) && stack.getValue().stackStatus == StackStatus.CREATE_COMPLETE) {
Expand Down Expand Up @@ -802,11 +799,6 @@ public void run() {
@SuppressWarnings("unused")
public static class DescriptorImpl extends Descriptor<Cloud> {

public String accessId;
public String secretKey;
public String region;
public String privateKey;

public DescriptorImpl() {
super();
load();
Expand Down Expand Up @@ -856,7 +848,6 @@ public ListBoxModel doFillEc2KeyPairNameItems(
@QueryParameter final String region,
@QueryParameter final String endpoint) {
final ListBoxModel model = new ListBoxModel();
// model.add(NEW_EC2_KEY_PAIR_VALUE);

try {
final AmazonEC2 amazonEC2 = new EC2Api().connect(awsCredentialsId, region, endpoint);
Expand Down Expand Up @@ -893,18 +884,6 @@ public boolean configure(final StaplerRequest req, final JSONObject formData) th
return super.configure(req, formData);
}

public String getAccessId() {
return accessId;
}

public String getSecretKey() {
return secretKey;
}

public String getRegion() {
return region;
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amazon.jenkins.ec2fleet;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.Computer;
import hudson.model.Node;
import hudson.slaves.RetentionStrategy;
Expand All @@ -24,6 +25,9 @@ public class IdleRetentionStrategy extends RetentionStrategy<SlaveComputer> {
* @param computer computer
* @return delay in min before next run
*/
@SuppressFBWarnings(
value = "BC_UNCONFIRMED_CAST",
justification = "to ignore EC2FleetNodeComputer cast")
@Override
public long check(final SlaveComputer computer) {
final EC2FleetNodeComputer fc = (EC2FleetNodeComputer) computer;
Expand Down
Loading

0 comments on commit f435798

Please sign in to comment.