Skip to content

Commit

Permalink
Choose Vnet and subnet from a different resource group (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
sparsick authored Jan 24, 2022
1 parent 02fd999 commit e4ea915
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ You can install/update this plugin in Jenkins update center (Manage Jenkins -> M
8. Add `Ports`, `Environment Variables` and `Volumes` as needed.
9. Choose a retention strategy. You can get details by clicking the help icon.
10. Specify `CPU Requirement` and `Memory Requirement`, ACI containers costs per second. Find more details in [Container Instances pricing](https://azure.microsoft.com/en-us/pricing/details/container-instances/).
11. Decide if the container instance should use a private IP address or not. You can get details by clicking the help icon.

## Configure Azure Container Instance via Groovy Script

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public class AciPrivateIpAddress extends AbstractDescribableImpl<AciPrivateIpAddress> {
private String vnet;

private String subnet;

private String resourceGroup;

@DataBoundConstructor
public AciPrivateIpAddress(String vnet, String subnet) {
this.vnet = vnet;
Expand All @@ -24,6 +27,16 @@ public String getSubnet() {
return subnet;
}

public String getResourceGroup() {
return resourceGroup;
}


@DataBoundSetter
public void setResourceGroup(String resourceGroup) {
this.resourceGroup = resourceGroup;
}

@Extension
public static class DescriptorImpl extends Descriptor<AciPrivateIpAddress> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public AciDeploymentTemplate buildDeploymentTemplate(AciCloud cloud, AciContaine
AciPrivateIpAddress privateIpAddress = template.getPrivateIpAddress();
variables.put("ipType", mapIpType(privateIpAddress));
if (privateIpAddress != null) {
variables.put("vnetResourceGroupName", privateIpAddress.getResourceGroup() != null
? privateIpAddress.getResourceGroup() : cloud.getResourceGroup());
variables.put("vnetName", privateIpAddress.getVnet());
variables.put("subnetName", privateIpAddress.getSubnet());
}
Expand Down Expand Up @@ -117,7 +119,8 @@ private void addSubnetIds(JsonNode tmp, ObjectMapper mapper, AciPrivateIpAddress

ObjectNode subnetIdNode = mapper.createObjectNode();
subnetIdNode.put("id",
"[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnetName'),"
"[resourceId(variables('vnetResourceGroupName'), 'Microsoft.Network/virtualNetworks/subnets',"
+ " variables('vnetName'),"
+ " variables('subnetName'))]");
ArrayNode subnetIdsArray = mapper.createArrayNode();
subnetIdsArray.add(subnetIdNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.microsoft.jenkins.containeragents.Messages;
import hudson.util.ListBoxModel;
import io.jenkins.plugins.azuresdk.HttpClientRetriever;
import jenkins.model.Jenkins;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;

Expand Down Expand Up @@ -88,23 +87,4 @@ private static AzureResourceManager getAzureResourceManager(
.authenticate(tokenCredential, profile)
.withSubscription(subscriptionId);
}

private static String getUserAgent() {
String version = null;
String instanceId = null;
try {
version = AzureContainerUtils.class.getPackage().getImplementationVersion();
instanceId = Jenkins.get().getLegacyInstanceId();
} catch (Exception e) {
}

if (version == null) {
version = "local";
}
if (instanceId == null) {
instanceId = "local";
}

return "AzureContainerService(Kubernetes)/" + version + "/" + instanceId;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<div>
Select if the container instance should use a private ip address.
<p>
If it use a private ip address, then information about the vnet and the subnet are mandatory.
If it uses a private ip address, then information about the vnet and the subnet are mandatory. The resource group is
optional. If it is not filled, then the resource group of container instance is used.
</p>
<p>
Important: Vnet and subnet has to be created before launching the Container agent and the subnet delegation must
be enabled
Important: Vnet and subnet has to be created in the correct resource group before launching the Container agent and
the subnet delegation must be enabled.
(See also
<a href="https://docs.microsoft.com/en-us/azure/virtual-network/manage-subnet-delegation">
Azure documentation about subnet delegation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">

<f:entry title="Name of the Resource Group" >
<f:textbox field="resourceGroup"/>
</f:entry>
<f:entry title="Name of the VNET" >
<f:textbox field="vnet"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ public void templateWithVnet() throws IOException {

AciDeploymentTemplateBuilder.AciDeploymentTemplate aciDeploymentTemplate = builderUnderTest.buildDeploymentTemplate(cloud, template, agentMock);

assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), containsString("\"vnetResourceGroupName\":\"resourceGroup\","));
assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), containsString("\"vnetName\":\"vnet\","));
assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), containsString("\"subnetName\":\"subnet\""));
assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), containsString("\"subnetIds\":"));
}

@Test
public void templateWithVnetAndOwnRg() throws IOException {
AciCloud cloud = new AciCloud("testcloud", "credentialId", "resourceGroup", emptyList());

AciContainerTemplate template = new AciContainerTemplate("containerName", "label", 100, "linux", "helloworld", "command", "rootFs", emptyList(), emptyList(), emptyList(), emptyList(), new RetentionStrategy.Always(), "cpu", "memory" );
AciPrivateIpAddress privateIpAddress = new AciPrivateIpAddress("vnet", "subnet");
privateIpAddress.setResourceGroup("vnetResourceGroup");
template.setPrivateIpAddress(privateIpAddress);

AciDeploymentTemplateBuilder.AciDeploymentTemplate aciDeploymentTemplate = builderUnderTest.buildDeploymentTemplate(cloud, template, agentMock);

assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), containsString("\"vnetResourceGroupName\":\"vnetResourceGroup\","));
assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), containsString("\"vnetName\":\"vnet\","));
assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), containsString("\"subnetName\":\"subnet\""));
assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), containsString("\"subnetIds\":"));
Expand All @@ -59,6 +77,7 @@ public void templateWithoutVnet() throws IOException {

AciDeploymentTemplateBuilder.AciDeploymentTemplate aciDeploymentTemplate = builderUnderTest.buildDeploymentTemplate(cloud, template, agentMock);

assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), not(containsString("\"vnetResourceGroupName\"")));
assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), not(containsString("\"vnetName\": \"vnet\",")));
assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), not(containsString("\"subnetName\": \"subnet\"")));
assertThat(aciDeploymentTemplate.deploymentTemplateAsString(), not(containsString("\"subnetIds\":")));
Expand Down

0 comments on commit e4ea915

Please sign in to comment.