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

Add Verbose Filters to Counting Existing Instances #650

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 47 additions & 1 deletion src/main/java/hudson/plugins/ec2/EC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public HttpResponse doProvision(@QueryParameter String template) throws ServletE
*
* @param template If left null, then all instances are counted.
*/
private int countCurrentEC2Slaves(SlaveTemplate template) throws AmazonClientException {
private int countCurrentEC2Slaves(SlaveTemplate template, List<Filter> addonFilters) throws AmazonClientException {
String jenkinsServerUrl = JenkinsLocationConfiguration.get().getUrl();

if (jenkinsServerUrl == null) {
Expand All @@ -487,6 +487,8 @@ private int countCurrentEC2Slaves(SlaveTemplate template) throws AmazonClientExc
String description = template != null ? template.description : null;

List<Filter> filters = getGenericFilters(jenkinsServerUrl, template);
if (addonFilters != null)
filters.addAll(addonFilters);
filters.add(new Filter("instance-state-name").withValues("running", "pending", "stopping"));
DescribeInstancesRequest dir = new DescribeInstancesRequest().withFilters(filters);
DescribeInstancesResult result = null;
Expand All @@ -510,6 +512,10 @@ private int countCurrentEC2Slaves(SlaveTemplate template) throws AmazonClientExc
return n;
}

int countCurrentEC2Slaves(SlaveTemplate template) throws AmazonClientException {
return countCurrentEC2Slaves(template, null);
}

/**
* Counts the number of EC2 Spot instances that can be used with the specified image and a template. Also removes any
* nodes associated with canceled requests.
Expand Down Expand Up @@ -646,6 +652,24 @@ private List<Filter> getGenericFilters(String jenkinsServerUrl, SlaveTemplate te
}
}
}
// Add Security group filters
if (template.getSecurityGroupSet() != null && template.getSecurityGroupSet().size() > 0) {
for (String sg: template.getSecurityGroupSet()) {
filters.add(new Filter("group-name").withValues(sg));
}
}
// Add Subnet filters
if (template.getSubnetId() != null && !template.getSubnetId().isEmpty()) {
for (String s: template.getSubnetId().split(SlaveTemplate.EC2_RESOURCE_ID_DELIMETERS)) {
filters.add(new Filter("subnet-id").withValues(s));
}
}

// Add IAM Instance profile filters
if (template.getIamInstanceProfile() != null && !template.getIamInstanceProfile().isEmpty()) {
filters.add(new Filter("iam-instance-profile.arn").withValues(template.getIamInstanceProfile()));
}

}
return filters;
}
Expand Down Expand Up @@ -674,6 +698,11 @@ private boolean isEc2ProvisionedAmiSlave(List<Tag> tags, String description) {
* Returns the maximum number of possible agents that can be created.
*/
private int getPossibleNewSlavesCount(SlaveTemplate template) throws AmazonClientException {
List<Filter> vpcFilters = new ArrayList<>();
for (String vpc: getTemplateVpcs(template)) {
vpcFilters.add(new Filter("vpc-id").withValues(vpc));
}

int estimatedTotalSlaves = countCurrentEC2Slaves(null);
int estimatedAmiSlaves = countCurrentEC2Slaves(template);

Expand All @@ -685,6 +714,23 @@ private int getPossibleNewSlavesCount(SlaveTemplate template) throws AmazonClien
return Math.min(availableAmiSlaves, availableTotalSlaves);
}

/* Gets the VPCs of the subnets configured for a given Slave Template */
private Set<String> getTemplateVpcs(SlaveTemplate template) throws AmazonClientException {
Set<String> vpcs = new HashSet<>();
if (template != null && template.getSubnetId() != null && !template.getSubnetId().isEmpty()) {
DescribeSubnetsRequest dsr = new DescribeSubnetsRequest().withSubnetIds(template.getSubnetId().split(SlaveTemplate.EC2_RESOURCE_ID_DELIMETERS));
DescribeSubnetsResult result = null;
do {
result = connect().describeSubnets(dsr);
dsr.setNextToken(result.getNextToken());
for (Subnet subnet: result.getSubnets()) {
vpcs.add(subnet.getVpcId());
}
} while (result.getNextToken() != null);
}
return vpcs;
}

/**
* Obtains a agent whose AMI matches the AMI of the given template, and that also has requiredLabel (if requiredLabel is non-null)
* forceCreateNew specifies that the creation of a new agent is required. Otherwise, an existing matching agent may be re-used
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/hudson/plugins/ec2/SlaveTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package hudson.plugins.ec2;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_ENDPOINT_ENABLED;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_TOKENS_REQUIRED;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_HOPS_LIMIT;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentialsProvider;
Expand Down Expand Up @@ -133,6 +129,10 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_ENDPOINT_ENABLED;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_HOPS_LIMIT;
import static hudson.plugins.ec2.EC2AbstractSlave.DEFAULT_METADATA_TOKENS_REQUIRED;

/**
* Template of {@link EC2AbstractSlave} to launch.
*
Expand All @@ -141,7 +141,7 @@
public class SlaveTemplate implements Describable<SlaveTemplate> {
private static final Logger LOGGER = Logger.getLogger(SlaveTemplate.class.getName());

private static final String EC2_RESOURCE_ID_DELIMETERS = "[\\s,;]+";
static final String EC2_RESOURCE_ID_DELIMETERS = "[\\s,;]+";

public String ami;

Expand Down Expand Up @@ -254,6 +254,7 @@ public class SlaveTemplate implements Describable<SlaveTemplate> {
@CheckForNull
private List<EC2Filter> amiFilters;


/*
* Necessary to handle reading from old configurations. The UnixData object is created in readResolve()
*/
Expand Down