@@ -10,7 +10,6 @@ import (
10
10
"github.com/go-logr/logr"
11
11
"github.com/pkg/errors"
12
12
"k8s.io/apimachinery/pkg/types"
13
- "k8s.io/apimachinery/pkg/util/sets"
14
13
"regexp"
15
14
elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1"
16
15
"sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/routeutils"
@@ -79,7 +78,7 @@ func (builder *securityGroupBuilderImpl) buildSecurityGroups(ctx context.Context
79
78
80
79
func (builder * securityGroupBuilderImpl ) handleManagedSecurityGroup (ctx context.Context , stack core.Stack , lbConf elbv2gw.LoadBalancerConfiguration , gw * gwv1.Gateway , routes map [int32 ][]routeutils.RouteDescriptor , ipAddressType elbv2model.IPAddressType ) (securityGroupOutput , error ) {
81
80
var lbSGTokens []core.StringToken
82
- managedSG , err := builder .buildManagedSecurityGroup (stack , lbConf , gw , routes , ipAddressType )
81
+ managedSG , err := builder .buildManagedSecurityGroup (stack , lbConf , gw , ipAddressType )
83
82
if err != nil {
84
83
return securityGroupOutput {}, err
85
84
}
@@ -144,14 +143,14 @@ func (builder *securityGroupBuilderImpl) getBackendSecurityGroup(ctx context.Con
144
143
return core .LiteralStringToken (backendSGID ), nil
145
144
}
146
145
147
- func (builder * securityGroupBuilderImpl ) buildManagedSecurityGroup (stack core.Stack , lbConf elbv2gw.LoadBalancerConfiguration , gw * gwv1.Gateway , routes map [ int32 ][]routeutils. RouteDescriptor , ipAddressType elbv2model.IPAddressType ) (* ec2model.SecurityGroup , error ) {
146
+ func (builder * securityGroupBuilderImpl ) buildManagedSecurityGroup (stack core.Stack , lbConf elbv2gw.LoadBalancerConfiguration , gw * gwv1.Gateway , ipAddressType elbv2model.IPAddressType ) (* ec2model.SecurityGroup , error ) {
148
147
name := builder .buildManagedSecurityGroupName (gw )
149
148
tags , err := builder .tagHelper .getGatewayTags (lbConf )
150
149
if err != nil {
151
150
return nil , err
152
151
}
153
152
154
- ingressPermissions := builder .buildManagedSecurityGroupIngressPermissions (lbConf , routes , ipAddressType )
153
+ ingressPermissions := builder .buildManagedSecurityGroupIngressPermissions (lbConf , gw , ipAddressType )
155
154
return ec2model .NewSecurityGroup (stack , resourceIDManagedSecurityGroup , ec2model.SecurityGroupSpec {
156
155
GroupName : name ,
157
156
Description : managedSGDescription ,
@@ -173,7 +172,7 @@ func (builder *securityGroupBuilderImpl) buildManagedSecurityGroupName(gw *gwv1.
173
172
return fmt .Sprintf ("k8s-%.8s-%.8s-%.10s" , sanitizedNamespace , sanitizedName , uuid )
174
173
}
175
174
176
- func (builder * securityGroupBuilderImpl ) buildManagedSecurityGroupIngressPermissions (lbConf elbv2gw.LoadBalancerConfiguration , routes map [ int32 ][]routeutils. RouteDescriptor , ipAddressType elbv2model.IPAddressType ) []ec2model.IPPermission {
175
+ func (builder * securityGroupBuilderImpl ) buildManagedSecurityGroupIngressPermissions (lbConf elbv2gw.LoadBalancerConfiguration , gw * gwv1. Gateway , ipAddressType elbv2model.IPAddressType ) []ec2model.IPPermission {
177
176
var permissions []ec2model.IPPermission
178
177
179
178
// Default to 0.0.0.0/0 and ::/0
@@ -200,97 +199,88 @@ func (builder *securityGroupBuilderImpl) buildManagedSecurityGroupIngressPermiss
200
199
201
200
includeIPv6 := isIPv6Supported (ipAddressType )
202
201
203
- // Port Loop
204
- for port , cfg := range routes {
205
- // Protocol Loop
206
- for _ , protocol := range generateProtocolListFromRoutes ( cfg ) {
207
- // CIDR Loop
208
- for _ , cidr := range sourceRanges {
209
- isIPv6 := isIPv6CIDR (cidr )
202
+ //listener loop
203
+ for _ , listener := range gw . Spec . Listeners {
204
+ port := int32 ( listener . Port )
205
+ protocol := getSgRuleProtocol ( listener . Protocol )
206
+ // CIDR Loop
207
+ for _ , cidr := range sourceRanges {
208
+ isIPv6 := isIPv6CIDR (cidr )
210
209
211
- if ! isIPv6 {
210
+ if ! isIPv6 {
211
+ permissions = append (permissions , ec2model.IPPermission {
212
+ IPProtocol : string (protocol ),
213
+ FromPort : awssdk .Int32 (int32 (port )),
214
+ ToPort : awssdk .Int32 (int32 (port )),
215
+ IPRanges : []ec2model.IPRange {
216
+ {
217
+ CIDRIP : cidr ,
218
+ },
219
+ },
220
+ })
221
+
222
+ if enableICMP {
212
223
permissions = append (permissions , ec2model.IPPermission {
213
- IPProtocol : protocol ,
214
- FromPort : awssdk .Int32 (int32 ( port ) ),
215
- ToPort : awssdk .Int32 (int32 ( port ) ),
224
+ IPProtocol : shared_constants . ICMPV4Protocol ,
225
+ FromPort : awssdk .Int32 (shared_constants . ICMPV4TypeForPathMtu ),
226
+ ToPort : awssdk .Int32 (shared_constants . ICMPV4CodeForPathMtu ),
216
227
IPRanges : []ec2model.IPRange {
217
228
{
218
229
CIDRIP : cidr ,
219
230
},
220
231
},
221
232
})
233
+ }
222
234
223
- if enableICMP {
224
- permissions = append (permissions , ec2model.IPPermission {
225
- IPProtocol : shared_constants .ICMPV4Protocol ,
226
- FromPort : awssdk .Int32 (shared_constants .ICMPV4TypeForPathMtu ),
227
- ToPort : awssdk .Int32 (shared_constants .ICMPV4CodeForPathMtu ),
228
- IPRanges : []ec2model.IPRange {
229
- {
230
- CIDRIP : cidr ,
231
- },
232
- },
233
- })
234
- }
235
+ } else if includeIPv6 {
236
+ permissions = append (permissions , ec2model.IPPermission {
237
+ IPProtocol : string (protocol ),
238
+ FromPort : awssdk .Int32 (int32 (port )),
239
+ ToPort : awssdk .Int32 (int32 (port )),
240
+ IPv6Range : []ec2model.IPv6Range {
241
+ {
242
+ CIDRIPv6 : cidr ,
243
+ },
244
+ },
245
+ })
235
246
236
- } else if includeIPv6 {
247
+ if enableICMP {
237
248
permissions = append (permissions , ec2model.IPPermission {
238
- IPProtocol : protocol ,
239
- FromPort : awssdk .Int32 (int32 ( port ) ),
240
- ToPort : awssdk .Int32 (int32 ( port ) ),
249
+ IPProtocol : shared_constants . ICMPV6Protocol ,
250
+ FromPort : awssdk .Int32 (shared_constants . ICMPV6TypeForPathMtu ),
251
+ ToPort : awssdk .Int32 (shared_constants . ICMPV6CodeForPathMtu ),
241
252
IPv6Range : []ec2model.IPv6Range {
242
253
{
243
254
CIDRIPv6 : cidr ,
244
255
},
245
256
},
246
257
})
247
-
248
- if enableICMP {
249
- permissions = append (permissions , ec2model.IPPermission {
250
- IPProtocol : shared_constants .ICMPV6Protocol ,
251
- FromPort : awssdk .Int32 (shared_constants .ICMPV6TypeForPathMtu ),
252
- ToPort : awssdk .Int32 (shared_constants .ICMPV6CodeForPathMtu ),
253
- IPv6Range : []ec2model.IPv6Range {
254
- {
255
- CIDRIPv6 : cidr ,
256
- },
257
- },
258
- })
259
- }
260
258
}
261
- } // CIDR Loop
262
- // PL loop
263
- for _ , prefixID := range prefixes {
264
- permissions = append ( permissions , ec2model. IPPermission {
265
- IPProtocol : protocol ,
266
- FromPort : awssdk . Int32 ( int32 ( port ) ),
267
- ToPort : awssdk .Int32 (int32 (port )),
268
- PrefixLists : []ec2model. PrefixList {
269
- {
270
- ListID : prefixID ,
271
- } ,
259
+ }
260
+ } // CIDR Loop
261
+ // PL loop
262
+ for _ , prefixID := range prefixes {
263
+ permissions = append ( permissions , ec2model. IPPermission {
264
+ IPProtocol : string ( protocol ),
265
+ FromPort : awssdk .Int32 (int32 (port )),
266
+ ToPort : awssdk . Int32 ( int32 ( port )),
267
+ PrefixLists : []ec2model. PrefixList {
268
+ {
269
+ ListID : prefixID ,
272
270
},
273
- })
274
- } // PL Loop
275
- } // Protocol Loop
276
- } // Port Loop
271
+ },
272
+ })
273
+ } // PL loop
274
+ } // listener loop
277
275
return permissions
278
276
}
279
277
280
- func generateProtocolListFromRoutes (routes []routeutils.RouteDescriptor ) []string {
281
- protocolSet := sets .New [string ]()
282
-
283
- for _ , route := range routes {
284
- switch route .GetRouteKind () {
285
- case routeutils .HTTPRouteKind , routeutils .GRPCRouteKind , routeutils .TCPRouteKind , routeutils .TLSRouteKind :
286
- protocolSet .Insert (string (ec2types .ProtocolTcp ))
287
- break
288
- case routeutils .UDPRouteKind :
289
- protocolSet = protocolSet .Insert (string (ec2types .ProtocolUdp ))
290
- break
291
- default :
292
- // Ignore? Throw error?
293
- }
278
+ func getSgRuleProtocol (protocol gwv1.ProtocolType ) ec2types.Protocol {
279
+ switch protocol {
280
+ case gwv1 .HTTPProtocolType , gwv1 .HTTPSProtocolType , gwv1 .TCPProtocolType , gwv1 .TLSProtocolType :
281
+ return ec2types .ProtocolTcp
282
+ case gwv1 .UDPProtocolType :
283
+ return ec2types .ProtocolUdp
294
284
}
295
- return protocolSet . UnsortedList ()
285
+ return ec2types . ProtocolTcp
296
286
}
0 commit comments