@@ -41,6 +41,10 @@ import (
4141func (s * Service ) reconcileNatGateways () error {
4242 if s .scope .VPC ().IsUnmanaged (s .scope .Name ()) {
4343 s .scope .Trace ("Skipping NAT gateway reconcile in unmanaged mode" )
44+ _ , err := s .updateNatGatewayIPs (false )
45+ if err != nil {
46+ return err
47+ }
4448 return nil
4549 }
4650
@@ -66,44 +70,11 @@ func (s *Service) reconcileNatGateways() error {
6670 return nil
6771 }
6872
69- existing , err := s .describeNatGatewaysBySubnet ( )
73+ subnetIDs , err := s .updateNatGatewayIPs ( true )
7074 if err != nil {
7175 return err
7276 }
7377
74- natGatewaysIPs := []string {}
75- subnetIDs := []string {}
76-
77- for _ , sn := range s .scope .Subnets ().FilterPublic ().FilterNonCni () {
78- if sn .GetResourceID () == "" {
79- continue
80- }
81-
82- if ngw , ok := existing [sn .GetResourceID ()]; ok {
83- if len (ngw .NatGatewayAddresses ) > 0 && ngw .NatGatewayAddresses [0 ].PublicIp != nil {
84- natGatewaysIPs = append (natGatewaysIPs , * ngw .NatGatewayAddresses [0 ].PublicIp )
85- }
86- // Make sure tags are up to date.
87- if err := wait .WaitForWithRetryable (wait .NewBackoff (), func () (bool , error ) {
88- buildParams := s .getNatGatewayTagParams (* ngw .NatGatewayId )
89- tagsBuilder := tags .New (& buildParams , tags .WithEC2 (s .EC2Client ))
90- if err := tagsBuilder .Ensure (converters .TagsToMap (ngw .Tags )); err != nil {
91- return false , err
92- }
93- return true , nil
94- }, awserrors .ResourceNotFound ); err != nil {
95- record .Warnf (s .scope .InfraCluster (), "FailedTagNATGateway" , "Failed to tag managed NAT Gateway %q: %v" , * ngw .NatGatewayId , err )
96- return errors .Wrapf (err , "failed to tag nat gateway %q" , * ngw .NatGatewayId )
97- }
98-
99- continue
100- }
101-
102- subnetIDs = append (subnetIDs , sn .GetResourceID ())
103- }
104-
105- s .scope .SetNatGatewaysIPs (natGatewaysIPs )
106-
10778 // Batch the creation of NAT gateways
10879 if len (subnetIDs ) > 0 {
10980 // set NatGatewayCreationStarted if the condition has never been set before
@@ -133,6 +104,49 @@ func (s *Service) reconcileNatGateways() error {
133104 return nil
134105}
135106
107+ func (s * Service ) updateNatGatewayIPs (updateTags bool ) ([]string , error ) {
108+ existing , err := s .describeNatGatewaysBySubnet ()
109+ if err != nil {
110+ return nil , err
111+ }
112+
113+ natGatewaysIPs := []string {}
114+ subnetIDs := []string {}
115+
116+ for _ , sn := range s .scope .Subnets ().FilterPublic ().FilterNonCni () {
117+ if sn .GetResourceID () == "" {
118+ continue
119+ }
120+
121+ if ngw , ok := existing [sn .GetResourceID ()]; ok {
122+ if len (ngw .NatGatewayAddresses ) > 0 && ngw .NatGatewayAddresses [0 ].PublicIp != nil {
123+ natGatewaysIPs = append (natGatewaysIPs , * ngw .NatGatewayAddresses [0 ].PublicIp )
124+ }
125+ if updateTags {
126+ // Make sure tags are up to date.
127+ if err := wait .WaitForWithRetryable (wait .NewBackoff (), func () (bool , error ) {
128+ buildParams := s .getNatGatewayTagParams (* ngw .NatGatewayId )
129+ tagsBuilder := tags .New (& buildParams , tags .WithEC2 (s .EC2Client ))
130+ if err := tagsBuilder .Ensure (converters .TagsToMap (ngw .Tags )); err != nil {
131+ return false , err
132+ }
133+ return true , nil
134+ }, awserrors .ResourceNotFound ); err != nil {
135+ record .Warnf (s .scope .InfraCluster (), "FailedTagNATGateway" , "Failed to tag managed NAT Gateway %q: %v" , * ngw .NatGatewayId , err )
136+ return nil , errors .Wrapf (err , "failed to tag nat gateway %q" , * ngw .NatGatewayId )
137+ }
138+ }
139+
140+ continue
141+ }
142+
143+ subnetIDs = append (subnetIDs , sn .GetResourceID ())
144+ }
145+
146+ s .scope .SetNatGatewaysIPs (natGatewaysIPs )
147+ return subnetIDs , nil
148+ }
149+
136150func (s * Service ) deleteNatGateways () error {
137151 if s .scope .VPC ().IsUnmanaged (s .scope .Name ()) {
138152 s .scope .Trace ("Skipping NAT gateway deletion in unmanaged mode" )
0 commit comments