Skip to content

Commit 424cc29

Browse files
authored
Per template max agent (#338)
1 parent e77d45c commit 424cc29

7 files changed

Lines changed: 40 additions & 11 deletions

File tree

src/main/java/com/microsoft/azure/vmagent/AzureVMAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ public synchronized void deprovision(Localizable reason) throws Exception {
661661
// Adjust estimated virtual machine count.
662662
AzureVMCloud parentCloud = getCloud();
663663
if (parentCloud != null) {
664-
parentCloud.adjustVirtualMachineCount(-1);
664+
parentCloud.adjustVirtualMachineCount(-1, template.getMaxVirtualMachinesLimit());
665665
}
666666

667667
Jenkins.get().removeNode(this);

src/main/java/com/microsoft/azure/vmagent/AzureVMAgentTemplate.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ public int hashCode() {
352352

353353
private List<AzureTagPair> tags;
354354

355+
private int maxVirtualMachinesLimit;
356+
355357
// deprecated fields
356358
private transient boolean isInstallDocker;
357359
private transient boolean isInstallMaven;
@@ -560,6 +562,15 @@ public String getAvailabilitySet() {
560562
return availabilityType != null ? availabilityType.getAvailabilitySet() : null;
561563
}
562564

565+
public int getMaxVirtualMachinesLimit() {
566+
return maxVirtualMachinesLimit;
567+
}
568+
569+
@DataBoundSetter
570+
public void setMaxVirtualMachinesLimit(int maxVirtualMachinesLimit) {
571+
this.maxVirtualMachinesLimit = maxVirtualMachinesLimit;
572+
}
573+
563574
public String getJavaPath() {
564575
return javaPath;
565576
}

src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,10 @@ public int getApproximateVirtualMachineCount() {
375375
* of virtual machines we currently have available.
376376
*
377377
* @param delta Number that are desired, or if less than 0, the number we are 'returning' to the pool
378+
* @param templateLimit max agents for the template in use
378379
* @return Number that can be allocated, up to the number desired. 0 if the number desired was < 0
379380
*/
380-
public int adjustVirtualMachineCount(int delta) {
381+
public int adjustVirtualMachineCount(int delta, int templateLimit) {
381382
synchronized (this) {
382383
if (delta < 0) {
383384
LOGGER.log(Level.FINE, "Current estimated VM count: {0}, reducing by {1}",
@@ -387,7 +388,8 @@ public int adjustVirtualMachineCount(int delta) {
387388
} else {
388389
LOGGER.log(Level.FINE, "Current estimated VM count: {0}, quantity desired {1}",
389390
new Object[]{approximateVirtualMachineCount, delta});
390-
if (approximateVirtualMachineCount + delta <= getMaxVirtualMachinesLimit()) {
391+
int limit = determineLimit(templateLimit);
392+
if (approximateVirtualMachineCount + delta <= limit) {
391393
// Enough available, return the desired quantity, and update the number we think we
392394
// have laying around.
393395
approximateVirtualMachineCount += delta;
@@ -396,14 +398,25 @@ public int adjustVirtualMachineCount(int delta) {
396398
// Not enough available, return what we have. Remember we could
397399
// go negative (if for instance another Jenkins instance had
398400
// a higher limit.
399-
int quantityAvailable = Math.max(0, getMaxVirtualMachinesLimit() - approximateVirtualMachineCount);
401+
int quantityAvailable = Math.max(0, limit - approximateVirtualMachineCount);
400402
approximateVirtualMachineCount += quantityAvailable;
401403
return quantityAvailable;
402404
}
403405
}
404406
}
405407
}
406408

409+
private int determineLimit(int templateLimit) {
410+
int cloudLimit = getMaxVirtualMachinesLimit();
411+
if (templateLimit > cloudLimit) {
412+
return cloudLimit;
413+
}
414+
if (templateLimit == 0) {
415+
return cloudLimit;
416+
}
417+
return templateLimit;
418+
}
419+
407420
/**
408421
* Sets the new approximate virtual machine count.
409422
* This is run by the verification task to update the VM count periodically.
@@ -670,11 +683,12 @@ public Collection<PlannedNode> provision(CloudState cloudState, int workLoad) {
670683
try {
671684
// Determine how many agents we can actually provision from here and
672685
// adjust our count (before deployment to avoid races)
673-
int adjustedNumberOfAgents = adjustVirtualMachineCount(numberOfAgents);
686+
int adjustedNumberOfAgents = adjustVirtualMachineCount(numberOfAgents,
687+
template.getMaxVirtualMachinesLimit());
674688
if (adjustedNumberOfAgents == 0) {
675689
LOGGER.log(Level.INFO,
676690
"Not able to create {0} nodes, at or above maximum VM count of {1} and already {2} VM(s)",
677-
new Object[]{numberOfAgents, getMaxVirtualMachinesLimit(),
691+
new Object[]{numberOfAgents, determineLimit(template.getMaxVirtualMachinesLimit()),
678692
getApproximateVirtualMachineCount()});
679693
return plannedNodes;
680694
} else if (adjustedNumberOfAgents < numberOfAgents) {
@@ -844,7 +858,8 @@ private void handleFailure(
844858
// Do not throw to avoid it being recorded
845859
}
846860
}
847-
template.retrieveAzureCloudReference().adjustVirtualMachineCount(-1);
861+
template.retrieveAzureCloudReference().adjustVirtualMachineCount(-1,
862+
template.getMaxVirtualMachinesLimit());
848863
// Update the template status given this new issue.
849864
template.handleTemplateProvisioningFailure(e.getMessage(), stage);
850865
}

src/main/resources/com/microsoft/azure/vmagent/AzureVMAgentTemplate/config.jelly

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<f:textbox/>
1414
</f:entry>
1515

16+
<f:entry title="${%Max_Virtual_Machines_Limit}" field="maxVirtualMachinesLimit"
17+
help="/plugin/azure-vm-agents/help-maxVirtualMachinesLimit.html">
18+
<f:number />
19+
</f:entry>
20+
1621
<f:entry title="${%Custom_Tag}" field="tags">
1722
<f:repeatableProperty field="tags">
1823
<f:entry>

src/main/resources/com/microsoft/azure/vmagent/AzureVMAgentTemplate/config.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
General_Configuration=General Configuration
22
Template_Name=Name
33
Template_Description=Description
4+
Max_Virtual_Machines_Limit=Max Jenkins Agents Limit
45
Labels=Labels
56
Location=Region
67
No_Availability=No infrastructure redundancy required

src/main/resources/com/microsoft/azure/vmagent/AzureVMCloud/config.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</f:entry>
1616
<f:entry title="${%Max_Virtual_Machines_Limit}" field="maxVirtualMachinesLimit"
1717
help="/plugin/azure-vm-agents/help-maxVirtualMachinesLimit.html">
18-
<f:textbox default="${descriptor.getDefaultMaxVMLimit()}"/>
18+
<f:number default="${descriptor.defaultMaxVMLimit}"/>
1919
</f:entry>
2020

2121
<f:entry title="${%Deployment_Timeout}" field="deploymentTimeout"
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
<div>
22
Specify the maximum number of virtual machines that can be created.<br>
3-
4-
<b>This number only includes those VMs in the specified resource group</b>
5-
63
</div>

0 commit comments

Comments
 (0)