@@ -41,6 +41,10 @@ import (
41
41
func (s * Service ) reconcileNatGateways () error {
42
42
if s .scope .VPC ().IsUnmanaged (s .scope .Name ()) {
43
43
s .scope .Trace ("Skipping NAT gateway reconcile in unmanaged mode" )
44
+ _ , err := s .updateNatGatewayIPs (s .scope .TagUnmanagedNetworkResources ())
45
+ if err != nil {
46
+ return err
47
+ }
44
48
return nil
45
49
}
46
50
@@ -66,44 +70,11 @@ func (s *Service) reconcileNatGateways() error {
66
70
return nil
67
71
}
68
72
69
- existing , err := s .describeNatGatewaysBySubnet ( )
73
+ subnetIDs , err := s .updateNatGatewayIPs ( true )
70
74
if err != nil {
71
75
return err
72
76
}
73
77
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
-
107
78
// Batch the creation of NAT gateways
108
79
if len (subnetIDs ) > 0 {
109
80
// set NatGatewayCreationStarted if the condition has never been set before
@@ -133,6 +104,49 @@ func (s *Service) reconcileNatGateways() error {
133
104
return nil
134
105
}
135
106
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
+
136
150
func (s * Service ) deleteNatGateways () error {
137
151
if s .scope .VPC ().IsUnmanaged (s .scope .Name ()) {
138
152
s .scope .Trace ("Skipping NAT gateway deletion in unmanaged mode" )
0 commit comments