@@ -36,6 +36,7 @@ import (
3636 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
3737 "sigs.k8s.io/cluster-api-provider-azure/azure"
3838 "sigs.k8s.io/cluster-api-provider-azure/azure/services/groups"
39+ "sigs.k8s.io/cluster-api-provider-azure/azure/services/natgateways"
3940 "sigs.k8s.io/cluster-api-provider-azure/azure/services/vnetpeerings"
4041 "sigs.k8s.io/cluster-api-provider-azure/util/futures"
4142 "sigs.k8s.io/cluster-api-provider-azure/util/tele"
@@ -138,7 +139,7 @@ func (s *ClusterScope) PublicIPSpecs() []azure.PublicIPSpec {
138139 publicIPSpecs = append (publicIPSpecs , nodeOutboundIPSpecs ... )
139140 }
140141
141- // Public IP specs for node nat gateways
142+ // Public IP specs for node NAT gateways
142143 var nodeNatGatewayIPSpecs []azure.PublicIPSpec
143144 for _ , subnet := range s .NodeSubnets () {
144145 if subnet .IsNatGatewayEnabled () {
@@ -220,20 +221,26 @@ func (s *ClusterScope) RouteTableSpecs() []azure.RouteTableSpec {
220221 return routetables
221222}
222223
223- // NatGatewaySpecs returns the node nat gateway.
224- func (s * ClusterScope ) NatGatewaySpecs () []azure.NatGatewaySpec {
225- var natGateways []azure.NatGatewaySpec
224+ // NatGatewaySpecs returns the node NAT gateway.
225+ func (s * ClusterScope ) NatGatewaySpecs () []azure.ResourceSpecGetter {
226+ natGatewaySet := make (map [string ]struct {})
227+ var natGateways []azure.ResourceSpecGetter
226228
227- // We ignore the control plane nat gateway, as we will always use a LB to enable egress on the control plane.
229+ // We ignore the control plane NAT gateway, as we will always use a LB to enable egress on the control plane.
228230 for _ , subnet := range s .NodeSubnets () {
229231 if subnet .IsNatGatewayEnabled () {
230- natGateways = append (natGateways , azure.NatGatewaySpec {
231- Name : subnet .NatGateway .Name ,
232- NatGatewayIP : infrav1.PublicIPSpec {
233- Name : subnet .NatGateway .NatGatewayIP .Name ,
234- },
235- Subnet : subnet ,
236- })
232+ if _ , ok := natGatewaySet [subnet .NatGateway .Name ]; ! ok {
233+ natGatewaySet [subnet .NatGateway .Name ] = struct {}{} // empty struct to represent hash set
234+ natGateways = append (natGateways , & natgateways.NatGatewaySpec {
235+ Name : subnet .NatGateway .Name ,
236+ ResourceGroup : s .ResourceGroup (),
237+ SubscriptionID : s .SubscriptionID (),
238+ Location : s .Location (),
239+ NatGatewayIP : infrav1.PublicIPSpec {
240+ Name : subnet .NatGateway .NatGatewayIP .Name ,
241+ },
242+ })
243+ }
237244 }
238245 }
239246
@@ -446,6 +453,15 @@ func (s *ClusterScope) SetSubnet(subnetSpec infrav1.SubnetSpec) {
446453 }
447454}
448455
456+ func (s * ClusterScope ) SetNatGatewayIDInSubnets (name string , id string ) {
457+ for _ , subnet := range s .Subnets () {
458+ if subnet .NatGateway .Name == name {
459+ subnet .NatGateway .ID = id
460+ s .SetSubnet (subnet )
461+ }
462+ }
463+ }
464+
449465// ControlPlaneRouteTable returns the cluster controlplane routetable.
450466func (s * ClusterScope ) ControlPlaneRouteTable () infrav1.RouteTable {
451467 subnet , _ := s .AzureCluster .Spec .NetworkSpec .GetControlPlaneSubnet ()
@@ -597,6 +613,7 @@ func (s *ClusterScope) PatchObject(ctx context.Context) error {
597613 infrav1 .NetworkInfrastructureReadyCondition ,
598614 infrav1 .VnetPeeringReadyCondition ,
599615 infrav1 .DisksReadyCondition ,
616+ infrav1 .NATGatewaysReadyCondition ,
600617 ),
601618 )
602619
@@ -609,6 +626,7 @@ func (s *ClusterScope) PatchObject(ctx context.Context) error {
609626 infrav1 .NetworkInfrastructureReadyCondition ,
610627 infrav1 .VnetPeeringReadyCondition ,
611628 infrav1 .DisksReadyCondition ,
629+ infrav1 .NATGatewaysReadyCondition ,
612630 }})
613631}
614632
0 commit comments