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

fix(provider/azure): Enable Azure Load Balancer from Azure VM Scale Set #3552

Merged
merged 3 commits into from
Apr 10, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AzureUtilities {
static final String IPCONFIG_NAME_PREFIX = "ipc-"
static final String NETWORK_INTERFACE_PREFIX = "nic-"
static final Pattern IPV4_PREFIX_REGEX = ~/^(?<addr3>\d+)\.(?<addr2>\d+)\.(?<addr1>\d+)\.(?<addr0>\d+)\/(?<length>\d+)$/
static final String LB_NAME_PREFIX = "lb-"
static final String INBOUND_NATPOOL_PREFIX = "np-"
static final String VNET_DEFAULT_ADDRESS_PREFIX = "10.0.0.0/8"
static final int SUBNET_DEFAULT_ADDRESS_PREFIX_LENGTH = 24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class AzureServerGroupDescription extends AzureResourceOpsDescription implements
Boolean hasNewSubnet = false
Boolean createNewSubnet = false
AzureExtensionCustomScriptSettings customScriptsSettings
Boolean enableInboundNAT = false

static class AzureScaleSetSku {
String name
Expand Down Expand Up @@ -161,6 +162,7 @@ class AzureServerGroupDescription extends AzureResourceOpsDescription implements
azureSG.clusterName = scaleSet.tags?.cluster ?: parsedName.cluster
azureSG.securityGroupName = scaleSet.tags?.securityGroupName
azureSG.loadBalancerName = scaleSet.tags?.loadBalancerName
azureSG.enableInboundNAT = scaleSet.tags?.enableInboundNAT
azureSG.appGatewayName = scaleSet.tags?.appGatewayName
azureSG.appGatewayBapId = scaleSet.tags?.appGatewayBapId
// TODO: appGatewayBapId can be retrieved via scaleSet->networkProfile->networkInterfaceConfigurations->ipConfigurations->ApplicationGatewayBackendAddressPools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.netflix.spinnaker.clouddriver.azure.common.AzureUtilities
import com.netflix.spinnaker.clouddriver.azure.resources.servergroup.model.AzureServerGroupDescription

import com.netflix.spinnaker.clouddriver.azure.resources.servergroup.model.AzureServerGroupDescription.AzureInboundPortConfig
import groovy.util.logging.Slf4j

@Slf4j
class AzureServerGroupResourceTemplate {
static final String STORAGE_ACCOUNT_SUFFIX = "sa"
static String LB_NAME = null

protected static ObjectMapper mapper = new ObjectMapper()
.configure(SerializationFeature.INDENT_OUTPUT, true)
Expand All @@ -57,6 +58,14 @@ class AzureServerGroupResourceTemplate {
mapper.writeValueAsString(template)
}

/**
* Initialize variables that will be used in mulitple places
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/mulitple/multiple

* @param description Azure Server Group description object
*/
private static void initializeCommonVariables(AzureServerGroupDescription description) {
LB_NAME = AzureUtilities.LB_NAME_PREFIX + description.name
}

/**
*
*/
Expand All @@ -74,11 +83,17 @@ class AzureServerGroupResourceTemplate {
* @param description
*/
ServerGroupTemplate(AzureServerGroupDescription description) {
if (description.enableInboundNAT){
initializeCommonVariables(description)
resources.add(new PublicIpResource(properties: new PublicIPPropertiesWithDns()))
resources.add(new LoadBalancer(description))
}

parameters = new ServerGroupTemplateParameters()

//If it's custom,
if (description.image.isCustom) {
variables = new CoreServerGroupTemplateVariables()
variables = new CoreServerGroupTemplateVariables(description)
} else {
variables = new ExtendedServerGroupTemplateVariables(description)
resources.add(new StorageAccount(description))
Expand All @@ -93,7 +108,33 @@ class AzureServerGroupResourceTemplate {

static class CoreServerGroupTemplateVariables implements TemplateVariables {
final String apiVersion = "2018-10-01"
String publicIPAddressName = ""
String publicIPAddressID = ""
String publicIPAddressType = ""
String dnsNameForLBIP = ""
String loadBalancerBackend = ""
String loadBalancerFrontEnd = ""
String loadBalancerName = ""
String loadBalancerID = ""
String frontEndIPConfigID = ""
String inboundNatPoolName = ""

CoreServerGroupTemplateVariables() {}

CoreServerGroupTemplateVariables(AzureServerGroupDescription description) {
if(description.enableInboundNAT){
publicIPAddressName = AzureUtilities.PUBLICIP_NAME_PREFIX + description.name
publicIPAddressID = "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
publicIPAddressType = "Dynamic"
dnsNameForLBIP = AzureUtilities.DNS_NAME_PREFIX + description.name.toLowerCase()
frontEndIPConfigID = "[resourceId('Microsoft.Network/loadBalancers/frontendIPConfigurations/', variables('loadBalancerName'), variables('loadBalancerFrontEnd'))]"
loadBalancerFrontEnd = AzureUtilities.LBFRONTEND_NAME_PREFIX + description.name
loadBalancerBackend = AzureUtilities.LBBACKEND_NAME_PREFIX + description.name
loadBalancerName = LB_NAME
loadBalancerID = "[resourceId('Microsoft.Network/loadBalancers', variables('loadBalancerName'))]"
inboundNatPoolName = AzureUtilities.INBOUND_NATPOOL_PREFIX + description.name
}
}
}

/**
Expand All @@ -118,7 +159,8 @@ class AzureServerGroupResourceTemplate {
* @param description
*/
ExtendedServerGroupTemplateVariables(AzureServerGroupDescription description) {
super()
super(description)

vhdContainerName = description.name.toLowerCase()
osType = new OsType(description)
imageReference = "[variables('osType')]"
Expand Down Expand Up @@ -304,6 +346,9 @@ class AzureServerGroupResourceTemplate {
def currentTime = System.currentTimeMillis()
tags = [:]
tags.createdTime = currentTime.toString()
if (description.subnetId) tags.subnetId = description.subnetId
if (description.securityGroupName) tags.securityGroupName = description.securityGroupName

if (description.instanceTags != null) {
tags << description.instanceTags
}
Expand All @@ -317,6 +362,12 @@ class AzureServerGroupResourceTemplate {
zones = description.zones.asList()
}

if(description.enableInboundNAT){
tags.enableInboundNAT = description.enableInboundNAT ? "true" : "false"
tags.loadBalancerName = LB_NAME
this.dependsOn.add("[concat('Microsoft.Network/loadBalancers/', variables('loadBalancerName'))]")
}

properties = new VirtualMachineScaleSetProperty(description)
sku = new ScaleSetSkuProperty(description)
}
Expand Down Expand Up @@ -474,7 +525,7 @@ class AzureServerGroupResourceTemplate {
*/
NetworkInterfaceIPConfiguration(AzureServerGroupDescription description) {
name = AzureUtilities.IPCONFIG_NAME_PREFIX + description.getIdentifier()
properties = new NetworkInterfaceIPConfigurationsProperty()
properties = new NetworkInterfaceIPConfigurationsProperty(description)
}
}

Expand All @@ -484,13 +535,19 @@ class AzureServerGroupResourceTemplate {
static class NetworkInterfaceIPConfigurationsProperty {
NetworkInterfaceIPConfigurationSubnet subnet
ArrayList<AppGatewayBackendAddressPool> ApplicationGatewayBackendAddressPools = []
ArrayList<LoadBalancerBackendAddressPool> loadBalancerBackendAddressPools = []
ArrayList<LoadBalancerInboundNatPoolId> loadBalancerInboundNatPools = []

/**
*
* @param description
*/
NetworkInterfaceIPConfigurationsProperty() {
NetworkInterfaceIPConfigurationsProperty(AzureServerGroupDescription description) {
subnet = new NetworkInterfaceIPConfigurationSubnet()
if(description.enableInboundNAT) {
loadBalancerBackendAddressPools.add(new LoadBalancerBackendAddressPool())
loadBalancerInboundNatPools.add(new LoadBalancerInboundNatPoolId())
}
ApplicationGatewayBackendAddressPools.add(new AppGatewayBackendAddressPool())
}
}
Expand All @@ -506,6 +563,20 @@ class AzureServerGroupResourceTemplate {
}
}

static class LoadBalancerBackendAddressPool {
String id

LoadBalancerBackendAddressPool() {
id = "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('loadBalancerName'), variables('loadBalancerBackend'))]"
}
}

static class LoadBalancerInboundNatPoolId extends IdRef {
LoadBalancerInboundNatPoolId() {
id = "[resourceId('Microsoft.Network/loadBalancers/inboundNatPools', variables('loadBalancerName'), variables('inboundNatPoolName'))]"
}
}

static class AppGatewayBackendAddressPool {
String id

Expand Down Expand Up @@ -661,4 +732,96 @@ class AzureServerGroupResourceTemplate {
fileUris = description.customScriptsSettings.fileUris
}
}

/**** Load Balancer Resource ****/
static class LoadBalancer extends DependingResource {
LoadBalancerProperties properties

LoadBalancer(AzureServerGroupDescription description) {
apiVersion = "[variables('apiVersion')]"
name = "[variables('loadBalancerName')]"
type = "Microsoft.Network/loadBalancers"
location = "[parameters('${locationParameterName}')]"
def currentTime = System.currentTimeMillis()
tags = [:]
tags.appName = description.application
tags.stack = description.stack
tags.detail = description.detail
tags.createdTime = currentTime.toString()
if (description.clusterName) tags.cluster = description.clusterName
if (description.name) tags.serverGroup = description.name
if (description.securityGroupName) tags.securityGroupName = description.securityGroupName

this.dependsOn.add("[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]")

properties = new LoadBalancerProperties(description)
}
}

static class LoadBalancerProperties {
ArrayList<FrontEndIpConfiguration> frontendIPConfigurations = []
ArrayList<BackEndAddressPool> backendAddressPools = []
ArrayList<InboundNatPool> inboundNatPools = []

LoadBalancerProperties(AzureServerGroupDescription description) {
frontendIPConfigurations.add(new FrontEndIpConfiguration())
backendAddressPools.add(new BackEndAddressPool())
description.inboundPortConfigs?.each {
inboundNatPools.add(new InboundNatPool(it))
}
}
}

static class FrontEndIpConfiguration {
String name
FrontEndIpProperties properties

FrontEndIpConfiguration() {
name = "[variables('loadBalancerFrontEnd')]"
properties = new FrontEndIpProperties("[variables('publicIPAddressID')]")
}
}

static class FrontEndIpProperties {
IdRef publicIpAddress

FrontEndIpProperties(String id) {
publicIpAddress = new IdRef(id)
}
}

static class BackEndAddressPool {
String name

BackEndAddressPool() {
name = "[variables('loadBalancerBackEnd')]"
}
}


static class InboundNatPool {
String name
InboundNatPoolProperties properties

InboundNatPool(AzureInboundPortConfig inboundPortConfig) {
name = inboundPortConfig.name
properties = new InboundNatPoolProperties(inboundPortConfig)
}
}

static class InboundNatPoolProperties {
IdRef frontendIPConfiguration
String protocol
int frontendPortRangeStart
int frontendPortRangeEnd
int backendPort

InboundNatPoolProperties(AzureInboundPortConfig inboundPortConfig) {
frontendIPConfiguration = new IdRef("[variables('frontEndIPConfigID')]")
protocol = inboundPortConfig.protocol
frontendPortRangeStart = inboundPortConfig.frontEndPortRangeStart
frontendPortRangeEnd = inboundPortConfig.frontEndPortRangeEnd
backendPort = inboundPortConfig.backendPort
}
}
}
Loading