@@ -30,32 +30,31 @@ const (
30
30
minimalAvailableIPAddressCount = int64 (8 )
31
31
)
32
32
33
- func (t * defaultModelBuildTask ) buildLoadBalancer (ctx context.Context , scheme elbv2model.LoadBalancerScheme ) ([] * ec2. Subnet , error ) {
34
- spec , ec2Subnets , err := t .buildLoadBalancerSpec (ctx , scheme )
33
+ func (t * defaultModelBuildTask ) buildLoadBalancer (ctx context.Context , scheme elbv2model.LoadBalancerScheme ) error {
34
+ spec , err := t .buildLoadBalancerSpec (ctx , scheme )
35
35
if err != nil {
36
- return nil , err
36
+ return err
37
37
}
38
38
t .loadBalancer = elbv2model .NewLoadBalancer (t .stack , resourceIDLoadBalancer , spec )
39
- return ec2Subnets , nil
39
+ return nil
40
40
}
41
41
42
- func (t * defaultModelBuildTask ) buildLoadBalancerSpec (ctx context.Context , scheme elbv2model.LoadBalancerScheme ) (elbv2model.LoadBalancerSpec , [] * ec2. Subnet , error ) {
42
+ func (t * defaultModelBuildTask ) buildLoadBalancerSpec (ctx context.Context , scheme elbv2model.LoadBalancerScheme ) (elbv2model.LoadBalancerSpec , error ) {
43
43
ipAddressType , err := t .buildLoadBalancerIPAddressType (ctx )
44
44
if err != nil {
45
- return elbv2model.LoadBalancerSpec {}, nil , err
45
+ return elbv2model.LoadBalancerSpec {}, err
46
46
}
47
47
lbAttributes , err := t .buildLoadBalancerAttributes (ctx )
48
48
if err != nil {
49
- return elbv2model.LoadBalancerSpec {}, nil , err
49
+ return elbv2model.LoadBalancerSpec {}, err
50
50
}
51
51
tags , err := t .buildLoadBalancerTags (ctx )
52
52
if err != nil {
53
- return elbv2model.LoadBalancerSpec {}, nil , err
53
+ return elbv2model.LoadBalancerSpec {}, err
54
54
}
55
-
56
- subnetMappings , ec2Subnets , err := t .buildLoadBalancerSubnetMappings (ctx , scheme )
55
+ subnetMappings , err := t .buildLoadBalancerSubnetMappings (ctx , scheme , t .ec2Subnets )
57
56
if err != nil {
58
- return elbv2model.LoadBalancerSpec {}, nil , err
57
+ return elbv2model.LoadBalancerSpec {}, err
59
58
}
60
59
name := t .buildLoadBalancerName (ctx , scheme )
61
60
spec := elbv2model.LoadBalancerSpec {
@@ -67,7 +66,7 @@ func (t *defaultModelBuildTask) buildLoadBalancerSpec(ctx context.Context, schem
67
66
LoadBalancerAttributes : lbAttributes ,
68
67
Tags : tags ,
69
68
}
70
- return spec , ec2Subnets , nil
69
+ return spec , nil
71
70
}
72
71
73
72
func (t * defaultModelBuildTask ) buildLoadBalancerIPAddressType (_ context.Context ) (elbv2model.IPAddressType , error ) {
@@ -179,31 +178,29 @@ func (t *defaultModelBuildTask) buildLoadBalancerTags(ctx context.Context) (map[
179
178
return t .buildAdditionalResourceTags (ctx )
180
179
}
181
180
182
- func (t * defaultModelBuildTask ) buildLoadBalancerSubnetMappings (ctx context.Context , scheme elbv2model.LoadBalancerScheme ) ([]elbv2model.SubnetMapping , []* ec2.Subnet , error ) {
183
-
181
+ func (t * defaultModelBuildTask ) buildLoadBalancerSubnetMappings (ctx context.Context , scheme elbv2model.LoadBalancerScheme , ec2Subnets []* ec2.Subnet ) ([]elbv2model.SubnetMapping , error ) {
184
182
var eipAllocation []string
185
183
eipConfigured := t .annotationParser .ParseStringSliceAnnotation (annotations .SvcLBSuffixEIPAllocations , & eipAllocation , t .service .Annotations )
186
184
var privateIpv4Addresses []string
187
185
ipv4Configured := t .annotationParser .ParseStringSliceAnnotation (annotations .SvcLBSuffixPrivateIpv4Addresses , & privateIpv4Addresses , t .service .Annotations )
188
186
189
187
// Validation
190
188
if eipConfigured && ipv4Configured {
191
- return []elbv2model.SubnetMapping {}, nil , errors .Errorf ("only one of EIP allocations or PrivateIpv4Addresses can be set" )
189
+ return []elbv2model.SubnetMapping {}, errors .Errorf ("only one of EIP allocations or PrivateIpv4Addresses can be set" )
192
190
}
193
- if eipConfigured && scheme == elbv2model .LoadBalancerSchemeInternal {
194
- return []elbv2model.SubnetMapping {}, nil , errors .Errorf ("EIP allocations can only be set for internet facing load balancers" )
195
- }
196
- if ipv4Configured && scheme == elbv2model .LoadBalancerSchemeInternetFacing {
197
- return []elbv2model.SubnetMapping {}, nil , errors .Errorf ("PrivateIpv4Addresses can only be set for internal balancers" )
198
- }
199
-
200
- ec2Subnets , _ := t .buildLoadBalancerSubnets (ctx , scheme , ipv4Configured )
201
- if eipConfigured && len (eipAllocation ) != len (ec2Subnets ) {
202
- return []elbv2model.SubnetMapping {}, nil , errors .Errorf ("number of EIP allocations (%d) and subnets (%d) must match" , len (eipAllocation ), len (ec2Subnets ))
191
+ if eipConfigured {
192
+ if scheme == elbv2model .LoadBalancerSchemeInternal {
193
+ return []elbv2model.SubnetMapping {}, errors .Errorf ("EIP allocations can only be set for internet facing load balancers" )
194
+ } else if len (eipAllocation ) != len (ec2Subnets ) {
195
+ return []elbv2model.SubnetMapping {}, errors .Errorf ("number of EIP allocations (%d) and subnets (%d) must match" , len (eipAllocation ), len (ec2Subnets ))
196
+ }
203
197
}
204
-
205
- if ipv4Configured && len (privateIpv4Addresses ) != len (ec2Subnets ) {
206
- return []elbv2model.SubnetMapping {}, nil , errors .Errorf ("number of PrivateIpv4Addresses (%d) and subnets (%d) must match" , len (privateIpv4Addresses ), len (ec2Subnets ))
198
+ if ipv4Configured {
199
+ if scheme == elbv2model .LoadBalancerSchemeInternetFacing {
200
+ return []elbv2model.SubnetMapping {}, errors .Errorf ("PrivateIpv4Addresses can only be set for internal balancers" )
201
+ } else if len (privateIpv4Addresses ) != len (ec2Subnets ) {
202
+ return []elbv2model.SubnetMapping {}, errors .Errorf ("number of PrivateIpv4Addresses (%d) and subnets (%d) must match" , len (privateIpv4Addresses ), len (ec2Subnets ))
203
+ }
207
204
}
208
205
209
206
subnetMappings := make ([]elbv2model.SubnetMapping , 0 , len (ec2Subnets ))
@@ -217,13 +214,13 @@ func (t *defaultModelBuildTask) buildLoadBalancerSubnetMappings(ctx context.Cont
217
214
if ipv4Configured {
218
215
ip , err := t .getMatchingIPforSubnet (ctx , subnet , privateIpv4Addresses )
219
216
if err != nil {
220
- return []elbv2model.SubnetMapping {}, nil , err
217
+ return []elbv2model.SubnetMapping {}, err
221
218
}
222
219
mapping .PrivateIPv4Address = aws .String (ip )
223
220
}
224
221
subnetMappings = append (subnetMappings , mapping )
225
222
}
226
- return subnetMappings , ec2Subnets , nil
223
+ return subnetMappings , nil
227
224
}
228
225
229
226
// Return the ip address which is in the subnet. Error if not match
@@ -245,7 +242,7 @@ func (t *defaultModelBuildTask) getMatchingIPforSubnet(_ context.Context, subnet
245
242
return "" , errors .Errorf ("no matching ip for subnet %s" , * subnet .SubnetId )
246
243
}
247
244
248
- func (t * defaultModelBuildTask ) buildLoadBalancerSubnets (ctx context.Context , scheme elbv2model.LoadBalancerScheme , ipv4Configured bool ) ([]* ec2.Subnet , error ) {
245
+ func (t * defaultModelBuildTask ) buildLoadBalancerSubnets (ctx context.Context , scheme elbv2model.LoadBalancerScheme ) ([]* ec2.Subnet , error ) {
249
246
var rawSubnetNameOrIDs []string
250
247
if exists := t .annotationParser .ParseStringSliceAnnotation (annotations .SvcLBSuffixSubnets , & rawSubnetNameOrIDs , t .service .Annotations ); exists {
251
248
return t .subnetsResolver .ResolveViaNameOrIDSlice (ctx , rawSubnetNameOrIDs ,
@@ -273,6 +270,8 @@ func (t *defaultModelBuildTask) buildLoadBalancerSubnets(ctx context.Context, sc
273
270
274
271
// for internet-facing Load Balancers, the subnets mush have at least 8 available IP addresses;
275
272
// for internal Load Balancers, this is only required if private ip address is not assigned
273
+ var privateIpv4Addresses []string
274
+ ipv4Configured := t .annotationParser .ParseStringSliceAnnotation (annotations .SvcLBSuffixPrivateIpv4Addresses , & privateIpv4Addresses , t .service .Annotations )
276
275
if (scheme == elbv2model .LoadBalancerSchemeInternetFacing ) ||
277
276
((scheme == elbv2model .LoadBalancerSchemeInternal ) && ! ipv4Configured ) {
278
277
return t .subnetsResolver .ResolveViaDiscovery (ctx ,
0 commit comments