@@ -62,11 +62,10 @@ type loadBalancerConfig struct {
62
62
Name string
63
63
Tags map [string ]string
64
64
65
- Type * string
66
- Scheme * string
67
- IpAddressType * string
68
- SecurityGroups []string
69
- Subnets []string
65
+ Type * string
66
+ Scheme * string
67
+ IpAddressType * string
68
+ Subnets []string
70
69
}
71
70
72
71
type defaultController struct {
@@ -88,6 +87,7 @@ func (controller *defaultController) Reconcile(ctx context.Context, ingress *ext
88
87
if err != nil {
89
88
return nil , err
90
89
}
90
+
91
91
lbConfig , err := controller .buildLBConfig (ctx , ingress , ingressAnnos )
92
92
if err != nil {
93
93
return nil , fmt .Errorf ("failed to build LoadBalancer configuration due to %v" , err )
@@ -96,7 +96,12 @@ func (controller *defaultController) Reconcile(ctx context.Context, ingress *ext
96
96
return nil , err
97
97
}
98
98
99
- instance , err := controller .ensureLBInstance (ctx , lbConfig )
99
+ ingKey := k8s .NamespacedName (ingress )
100
+ sgAttachment , err := controller .sgAssociationController .Setup (ctx , ingKey )
101
+ if err != nil {
102
+ return nil , err
103
+ }
104
+ instance , err := controller .ensureLBInstance (ctx , lbConfig , sgAttachment )
100
105
if err != nil {
101
106
return nil , err
102
107
}
@@ -122,7 +127,7 @@ func (controller *defaultController) Reconcile(ctx context.Context, ingress *ext
122
127
return nil , fmt .Errorf ("failed to GC targetGroups due to %v" , err )
123
128
}
124
129
125
- if err := controller .sgAssociationController .Reconcile (ctx , ingress , instance , tgGroup ); err != nil {
130
+ if err := controller .sgAssociationController .Reconcile (ctx , ingKey , sgAttachment , instance , tgGroup ); err != nil {
126
131
return nil , fmt .Errorf ("failed to reconcile securityGroup associations due to %v" , err )
127
132
}
128
133
return & LoadBalancer {
@@ -138,9 +143,6 @@ func (controller *defaultController) Delete(ctx context.Context, ingressKey type
138
143
return fmt .Errorf ("failed to find existing LoadBalancer due to %v" , err )
139
144
}
140
145
if instance != nil {
141
- if err = controller .sgAssociationController .Delete (ctx , ingressKey , instance ); err != nil {
142
- return fmt .Errorf ("failed to clean up securityGroups due to %v" , err )
143
- }
144
146
if err = controller .lsGroupController .Delete (ctx , aws .StringValue (instance .LoadBalancerArn )); err != nil {
145
147
return fmt .Errorf ("failed to delete listeners due to %v" , err )
146
148
}
@@ -153,24 +155,27 @@ func (controller *defaultController) Delete(ctx context.Context, ingressKey type
153
155
return err
154
156
}
155
157
}
158
+ if err = controller .sgAssociationController .Delete (ctx , ingressKey ); err != nil {
159
+ return fmt .Errorf ("failed to clean up securityGroups due to %v" , err )
160
+ }
156
161
157
162
return nil
158
163
}
159
164
160
- func (controller * defaultController ) ensureLBInstance (ctx context.Context , lbConfig * loadBalancerConfig ) (* elbv2.LoadBalancer , error ) {
165
+ func (controller * defaultController ) ensureLBInstance (ctx context.Context , lbConfig * loadBalancerConfig , sgAttachment sg. LbAttachmentInfo ) (* elbv2.LoadBalancer , error ) {
161
166
instance , err := controller .cloud .GetLoadBalancerByName (ctx , lbConfig .Name )
162
167
if err != nil {
163
168
return nil , fmt .Errorf ("failed to find existing LoadBalancer due to %v" , err )
164
169
}
165
170
if instance == nil {
166
- instance , err = controller .newLBInstance (ctx , lbConfig )
171
+ instance , err = controller .newLBInstance (ctx , lbConfig , sgAttachment )
167
172
if err != nil {
168
173
return nil , fmt .Errorf ("failed to create LoadBalancer due to %v" , err )
169
174
}
170
175
return instance , nil
171
176
}
172
177
if controller .isLBInstanceNeedRecreation (ctx , instance , lbConfig ) {
173
- instance , err = controller .recreateLBInstance (ctx , instance , lbConfig )
178
+ instance , err = controller .recreateLBInstance (ctx , instance , lbConfig , sgAttachment )
174
179
if err != nil {
175
180
return nil , fmt .Errorf ("failed to recreate LoadBalancer due to %v" , err )
176
181
}
@@ -182,14 +187,14 @@ func (controller *defaultController) ensureLBInstance(ctx context.Context, lbCon
182
187
return instance , nil
183
188
}
184
189
185
- func (controller * defaultController ) newLBInstance (ctx context.Context , lbConfig * loadBalancerConfig ) (* elbv2.LoadBalancer , error ) {
190
+ func (controller * defaultController ) newLBInstance (ctx context.Context , lbConfig * loadBalancerConfig , sgAttachment sg. LbAttachmentInfo ) (* elbv2.LoadBalancer , error ) {
186
191
albctx .GetLogger (ctx ).Infof ("creating LoadBalancer %v" , lbConfig .Name )
187
192
resp , err := controller .cloud .CreateLoadBalancerWithContext (ctx , & elbv2.CreateLoadBalancerInput {
188
193
Name : aws .String (lbConfig .Name ),
189
194
Type : lbConfig .Type ,
190
195
Scheme : lbConfig .Scheme ,
191
196
IpAddressType : lbConfig .IpAddressType ,
192
- SecurityGroups : aws .StringSlice (lbConfig . SecurityGroups ),
197
+ SecurityGroups : aws .StringSlice (sgAttachment . SGIDs () ),
193
198
Subnets : aws .StringSlice (lbConfig .Subnets ),
194
199
Tags : tags .ConvertToELBV2 (lbConfig .Tags ),
195
200
})
@@ -205,13 +210,13 @@ func (controller *defaultController) newLBInstance(ctx context.Context, lbConfig
205
210
return instance , nil
206
211
}
207
212
208
- func (controller * defaultController ) recreateLBInstance (ctx context.Context , existingInstance * elbv2.LoadBalancer , lbConfig * loadBalancerConfig ) (* elbv2.LoadBalancer , error ) {
213
+ func (controller * defaultController ) recreateLBInstance (ctx context.Context , existingInstance * elbv2.LoadBalancer , lbConfig * loadBalancerConfig , sgAttachment sg. LbAttachmentInfo ) (* elbv2.LoadBalancer , error ) {
209
214
existingLBArn := aws .StringValue (existingInstance .LoadBalancerArn )
210
215
albctx .GetLogger (ctx ).Infof ("deleting LoadBalancer %v for recreation" , existingLBArn )
211
216
if err := controller .cloud .DeleteLoadBalancerByArn (ctx , existingLBArn ); err != nil {
212
217
return nil , err
213
218
}
214
- return controller .newLBInstance (ctx , lbConfig )
219
+ return controller .newLBInstance (ctx , lbConfig , sgAttachment )
215
220
}
216
221
217
222
func (controller * defaultController ) reconcileLBInstance (ctx context.Context , instance * elbv2.LoadBalancer , lbConfig * loadBalancerConfig ) error {
@@ -307,15 +312,15 @@ func (controller *defaultController) buildLBConfig(ctx context.Context, ingress
307
312
if err != nil {
308
313
return nil , err
309
314
}
315
+
310
316
return & loadBalancerConfig {
311
317
Name : controller .nameTagGen .NameLB (ingress .Namespace , ingress .Name ),
312
318
Tags : lbTags ,
313
319
314
- Type : aws .String (elbv2 .LoadBalancerTypeEnumApplication ),
315
- Scheme : ingressAnnos .LoadBalancer .Scheme ,
316
- IpAddressType : ingressAnnos .LoadBalancer .IPAddressType ,
317
- SecurityGroups : ingressAnnos .LoadBalancer .SecurityGroups ,
318
- Subnets : subnets ,
320
+ Type : aws .String (elbv2 .LoadBalancerTypeEnumApplication ),
321
+ Scheme : ingressAnnos .LoadBalancer .Scheme ,
322
+ IpAddressType : ingressAnnos .LoadBalancer .IPAddressType ,
323
+ Subnets : subnets ,
319
324
}, nil
320
325
}
321
326
0 commit comments