From e4ea91588c809a8126358cbc8d033f8bb0f2875b Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Mon, 24 Jan 2022 09:20:20 +0100 Subject: [PATCH] Choose Vnet and subnet from a different resource group (#98) --- README.md | 1 + .../aci/AciPrivateIpAddress.java | 13 ++++++++++++ .../AciDeploymentTemplateBuilder.java | 5 ++++- .../util/AzureContainerUtils.java | 20 ------------------- .../help-privateIpAddress.html | 7 ++++--- .../aci/AciPrivateIpAddress/config.jelly | 3 +++ .../AciDeploymentTemplateBuilderTest.java | 19 ++++++++++++++++++ 7 files changed, 44 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index bc994b2..2b52e0e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main/java/com/microsoft/jenkins/containeragents/aci/AciPrivateIpAddress.java b/src/main/java/com/microsoft/jenkins/containeragents/aci/AciPrivateIpAddress.java index 80f4b77..dc6bdc4 100644 --- a/src/main/java/com/microsoft/jenkins/containeragents/aci/AciPrivateIpAddress.java +++ b/src/main/java/com/microsoft/jenkins/containeragents/aci/AciPrivateIpAddress.java @@ -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 { private String vnet; private String subnet; + private String resourceGroup; + @DataBoundConstructor public AciPrivateIpAddress(String vnet, String subnet) { this.vnet = vnet; @@ -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 { diff --git a/src/main/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilder.java b/src/main/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilder.java index 12beae7..fd09a3e 100644 --- a/src/main/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilder.java +++ b/src/main/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilder.java @@ -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()); } @@ -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); diff --git a/src/main/java/com/microsoft/jenkins/containeragents/util/AzureContainerUtils.java b/src/main/java/com/microsoft/jenkins/containeragents/util/AzureContainerUtils.java index 78abb1e..847ccd7 100644 --- a/src/main/java/com/microsoft/jenkins/containeragents/util/AzureContainerUtils.java +++ b/src/main/java/com/microsoft/jenkins/containeragents/util/AzureContainerUtils.java @@ -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; @@ -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; - } } diff --git a/src/main/resources/com/microsoft/jenkins/containeragents/aci/AciContainerTemplate/help-privateIpAddress.html b/src/main/resources/com/microsoft/jenkins/containeragents/aci/AciContainerTemplate/help-privateIpAddress.html index 5404925..0c11f4e 100644 --- a/src/main/resources/com/microsoft/jenkins/containeragents/aci/AciContainerTemplate/help-privateIpAddress.html +++ b/src/main/resources/com/microsoft/jenkins/containeragents/aci/AciContainerTemplate/help-privateIpAddress.html @@ -1,11 +1,12 @@
Select if the container instance should use a private ip address.

- 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.

- 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 Azure documentation about subnet delegation diff --git a/src/main/resources/com/microsoft/jenkins/containeragents/aci/AciPrivateIpAddress/config.jelly b/src/main/resources/com/microsoft/jenkins/containeragents/aci/AciPrivateIpAddress/config.jelly index e19a480..ff32ff8 100644 --- a/src/main/resources/com/microsoft/jenkins/containeragents/aci/AciPrivateIpAddress/config.jelly +++ b/src/main/resources/com/microsoft/jenkins/containeragents/aci/AciPrivateIpAddress/config.jelly @@ -1,6 +1,9 @@ + + + diff --git a/src/test/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilderTest.java b/src/test/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilderTest.java index 782e973..a1e951b 100644 --- a/src/test/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilderTest.java +++ b/src/test/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilderTest.java @@ -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\":")); @@ -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\":")));