@@ -15,6 +15,7 @@ import (
15
15
"sigs.k8s.io/aws-load-balancer-controller/pkg/equality"
16
16
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
17
17
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
18
+ "sigs.k8s.io/aws-load-balancer-controller/pkg/networking"
18
19
"strings"
19
20
)
20
21
@@ -151,19 +152,16 @@ func (t *defaultModelBuildTask) buildLoadBalancerSubnetMappings(ctx context.Cont
151
152
}
152
153
explicitSubnetNameOrIDsList = append (explicitSubnetNameOrIDsList , rawSubnetNameOrIDs )
153
154
}
155
+
154
156
if len (explicitSubnetNameOrIDsList ) == 0 {
155
- chosenSubnets , err := t .subnetsResolver .DiscoverSubnets (ctx , scheme )
157
+ chosenSubnets , err := t .subnetsResolver .ResolveViaDiscovery (ctx ,
158
+ networking .WithSubnetsResolveLBType (elbv2model .LoadBalancerTypeApplication ),
159
+ networking .WithSubnetsResolveLBScheme (scheme ),
160
+ )
156
161
if err != nil {
157
- return nil , err
158
- }
159
- var chosenSubnetIDs []string
160
- for _ , subnet := range chosenSubnets {
161
- chosenSubnetIDs = append (chosenSubnetIDs , awssdk .StringValue (subnet .SubnetId ))
162
- }
163
- if len (chosenSubnetIDs ) < 2 {
164
- return nil , errors .Errorf ("cannot find at least two subnets from different Availability Zones, discovered subnetIDs: %v" , chosenSubnetIDs )
162
+ return nil , errors .Wrap (err , "couldn't auto-discover subnets" )
165
163
}
166
- return buildLoadBalancerSubnetMappingsWithSubnetIDs ( chosenSubnetIDs ), nil
164
+ return buildLoadBalancerSubnetMappingsWithSubnets ( chosenSubnets ), nil
167
165
}
168
166
169
167
chosenSubnetNameOrIDs := explicitSubnetNameOrIDsList [0 ]
@@ -173,11 +171,14 @@ func (t *defaultModelBuildTask) buildLoadBalancerSubnetMappings(ctx context.Cont
173
171
return nil , errors .Errorf ("conflicting subnets: %v | %v" , chosenSubnetNameOrIDs , subnetNameOrIDs )
174
172
}
175
173
}
176
- chosenSubnetIDs , err := t .resolveSubnetIDsViaNameOrIDSlice (ctx , chosenSubnetNameOrIDs )
174
+ chosenSubnets , err := t .subnetsResolver .ResolveViaNameOrIDSlice (ctx , chosenSubnetNameOrIDs ,
175
+ networking .WithSubnetsResolveLBType (elbv2model .LoadBalancerTypeApplication ),
176
+ networking .WithSubnetsResolveLBScheme (scheme ),
177
+ )
177
178
if err != nil {
178
179
return nil , err
179
180
}
180
- return buildLoadBalancerSubnetMappingsWithSubnetIDs ( chosenSubnetIDs ), nil
181
+ return buildLoadBalancerSubnetMappingsWithSubnets ( chosenSubnets ), nil
181
182
}
182
183
183
184
func (t * defaultModelBuildTask ) buildLoadBalancerSecurityGroups (ctx context.Context , listenPortConfigByPort map [int64 ]listenPortConfig , ipAddressType elbv2model.IPAddressType ) ([]core.StringToken , error ) {
@@ -256,57 +257,6 @@ func (t *defaultModelBuildTask) buildLoadBalancerTags(_ context.Context) (map[st
256
257
return mergedTags , nil
257
258
}
258
259
259
- // resolveSubnetIDsViaNameOrIDSlice resolves the subnetIDs for LoadBalancer via a slice of subnetName or subnetIDs.
260
- func (t * defaultModelBuildTask ) resolveSubnetIDsViaNameOrIDSlice (ctx context.Context , subnetNameOrIDs []string ) ([]string , error ) {
261
- var subnetIDs []string
262
- var subnetNames []string
263
- for _ , nameOrID := range subnetNameOrIDs {
264
- if strings .HasPrefix (nameOrID , "subnet-" ) {
265
- subnetIDs = append (subnetIDs , nameOrID )
266
- } else {
267
- subnetNames = append (subnetNames , nameOrID )
268
- }
269
- }
270
- var resolvedSubnets []* ec2sdk.Subnet
271
- if len (subnetIDs ) > 0 {
272
- req := & ec2sdk.DescribeSubnetsInput {
273
- SubnetIds : awssdk .StringSlice (subnetIDs ),
274
- }
275
- subnets , err := t .ec2Client .DescribeSubnetsAsList (ctx , req )
276
- if err != nil {
277
- return nil , err
278
- }
279
- resolvedSubnets = append (resolvedSubnets , subnets ... )
280
- }
281
- if len (subnetNames ) > 0 {
282
- req := & ec2sdk.DescribeSubnetsInput {
283
- Filters : []* ec2sdk.Filter {
284
- {
285
- Name : awssdk .String ("tag:Name" ),
286
- Values : awssdk .StringSlice (subnetNames ),
287
- },
288
- {
289
- Name : awssdk .String ("vpc-id" ),
290
- Values : awssdk .StringSlice ([]string {t .vpcID }),
291
- },
292
- },
293
- }
294
- subnets , err := t .ec2Client .DescribeSubnetsAsList (ctx , req )
295
- if err != nil {
296
- return nil , err
297
- }
298
- resolvedSubnets = append (resolvedSubnets , subnets ... )
299
- }
300
- resolvedSubnetIDs := make ([]string , 0 , len (resolvedSubnets ))
301
- for _ , subnet := range resolvedSubnets {
302
- resolvedSubnetIDs = append (resolvedSubnetIDs , awssdk .StringValue (subnet .SubnetId ))
303
- }
304
- if len (resolvedSubnetIDs ) != len (subnetNameOrIDs ) {
305
- return nil , errors .Errorf ("couldn't found all subnets, nameOrIDs: %v, found: %v" , subnetNameOrIDs , resolvedSubnetIDs )
306
- }
307
- return resolvedSubnetIDs , nil
308
- }
309
-
310
260
func (t * defaultModelBuildTask ) resolveSecurityGroupIDsViaNameOrIDSlice (ctx context.Context , sgNameOrIDs []string ) ([]string , error ) {
311
261
var sgIDs []string
312
262
var sgNames []string
@@ -357,11 +307,11 @@ func (t *defaultModelBuildTask) resolveSecurityGroupIDsViaNameOrIDSlice(ctx cont
357
307
return resolvedSGIDs , nil
358
308
}
359
309
360
- func buildLoadBalancerSubnetMappingsWithSubnetIDs ( subnetIDs []string ) []elbv2model.SubnetMapping {
361
- subnetMappings := make ([]elbv2model.SubnetMapping , 0 , len (subnetIDs ))
362
- for _ , subnetID := range subnetIDs {
310
+ func buildLoadBalancerSubnetMappingsWithSubnets ( subnets []* ec2sdk. Subnet ) []elbv2model.SubnetMapping {
311
+ subnetMappings := make ([]elbv2model.SubnetMapping , 0 , len (subnets ))
312
+ for _ , subnet := range subnets {
363
313
subnetMappings = append (subnetMappings , elbv2model.SubnetMapping {
364
- SubnetID : subnetID ,
314
+ SubnetID : awssdk . StringValue ( subnet . SubnetId ) ,
365
315
})
366
316
}
367
317
return subnetMappings
0 commit comments