Skip to content

Commit e98e2c3

Browse files
authored
AppGw Subnet Delegation Support (#1703)
* create and update agic managed subnet with delegation
1 parent 53a89fc commit e98e2c3

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

pkg/azure/client.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
n "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-03-01/network"
1616
"github.com/Azure/go-autorest/autorest"
1717
"github.com/Azure/go-autorest/autorest/azure/auth"
18+
"github.com/Azure/go-autorest/autorest/to"
1819
"k8s.io/klog/v2"
1920

2021
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/controllererrors"
@@ -308,19 +309,25 @@ func (az *azClient) DeployGatewayWithVnet(resourceGroupName ResourceGroup, vnetN
308309
return
309310
}
310311

311-
klog.Infof("Checking the Vnet %s for a subnet with prefix %s", vnetName, subnetPrefix)
312+
klog.Infof("Checking the Vnet '%s' for a subnet with prefix '%s'.", vnetName, subnetPrefix)
312313
subnet, err := az.findSubnet(vnet, subnetName, subnetPrefix)
313314
if err != nil {
314315
if subnetPrefix == "" {
315-
klog.Infof("Unable to find a subnet with subnetName %s. Please provide subnetPrefix in order to allow AGIC to create a subnet in Vnet %s", subnetName, vnetName)
316+
klog.Infof("Unable to find a subnet with subnetName '%s'. Please provide subnetPrefix in order to allow AGIC to create a subnet in Vnet '%s'.", subnetName, vnetName)
316317
return
317318
}
318319

319-
klog.Infof("Unable to find a subnet. Creating a subnet %s with prefix %s in Vnet %s", subnetName, subnetPrefix, vnetName)
320+
klog.Infof("Unable to find a subnet. Creating a subnet '%s' with prefix '%s' in Vnet '%s'.", subnetName, subnetPrefix, vnetName)
320321
subnet, err = az.createSubnet(vnet, subnetName, subnetPrefix)
321322
if err != nil {
322323
return
323324
}
325+
} else if subnet.SubnetPropertiesFormat != nil && (subnet.SubnetPropertiesFormat.Delegations == nil || (subnet.SubnetPropertiesFormat.Delegations != nil && len(*subnet.SubnetPropertiesFormat.Delegations) == 0)) {
326+
klog.Infof("Subnet '%s' is an existing subnet and subnet delegation to Application Gateway is not found, creating a delegation.", subnetName)
327+
subnet, err = az.createSubnet(vnet, subnetName, subnetPrefix)
328+
if err != nil {
329+
klog.Errorf("Backfill delegation to Application Gateway on existing subnet has failed. Please check the subnet '%s' in vnet '%s'.", subnetName, vnetName)
330+
}
324331
}
325332

326333
err = az.DeployGatewayWithSubnet(*subnet.ID, skuName)
@@ -398,6 +405,14 @@ func (az *azClient) createSubnet(vnet n.VirtualNetwork, subnetName ResourceName,
398405
subnet = n.Subnet{
399406
SubnetPropertiesFormat: &n.SubnetPropertiesFormat{
400407
AddressPrefix: &subnetPrefix,
408+
Delegations: &[]n.Delegation{
409+
{
410+
Name: to.StringPtr("Microsoft.Network/applicationGateways"),
411+
ServiceDelegationPropertiesFormat: &n.ServiceDelegationPropertiesFormat{
412+
ServiceName: to.StringPtr("Microsoft.Network/applicationGateways"),
413+
},
414+
},
415+
},
401416
},
402417
}
403418
subnetFuture, err := az.subnetsClient.CreateOrUpdate(az.ctx, string(resourceGroup), string(vnetName), string(subnetName), subnet)

0 commit comments

Comments
 (0)