Skip to content

Commit 857ce64

Browse files
committed
decouple buildLoadBalancerSubnets from buildLoadBalancerSubnetMappings
1 parent 8925c89 commit 857ce64

File tree

3 files changed

+153
-195
lines changed

3 files changed

+153
-195
lines changed

pkg/service/model_build_load_balancer.go

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,31 @@ const (
3030
minimalAvailableIPAddressCount = int64(8)
3131
)
3232

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)
3535
if err != nil {
36-
return nil, err
36+
return err
3737
}
3838
t.loadBalancer = elbv2model.NewLoadBalancer(t.stack, resourceIDLoadBalancer, spec)
39-
return ec2Subnets, nil
39+
return nil
4040
}
4141

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) {
4343
ipAddressType, err := t.buildLoadBalancerIPAddressType(ctx)
4444
if err != nil {
45-
return elbv2model.LoadBalancerSpec{}, nil, err
45+
return elbv2model.LoadBalancerSpec{}, err
4646
}
4747
lbAttributes, err := t.buildLoadBalancerAttributes(ctx)
4848
if err != nil {
49-
return elbv2model.LoadBalancerSpec{}, nil, err
49+
return elbv2model.LoadBalancerSpec{}, err
5050
}
5151
tags, err := t.buildLoadBalancerTags(ctx)
5252
if err != nil {
53-
return elbv2model.LoadBalancerSpec{}, nil, err
53+
return elbv2model.LoadBalancerSpec{}, err
5454
}
55-
56-
subnetMappings, ec2Subnets, err := t.buildLoadBalancerSubnetMappings(ctx, scheme)
55+
subnetMappings, err := t.buildLoadBalancerSubnetMappings(ctx, scheme, t.ec2Subnets)
5756
if err != nil {
58-
return elbv2model.LoadBalancerSpec{}, nil, err
57+
return elbv2model.LoadBalancerSpec{}, err
5958
}
6059
name := t.buildLoadBalancerName(ctx, scheme)
6160
spec := elbv2model.LoadBalancerSpec{
@@ -67,7 +66,7 @@ func (t *defaultModelBuildTask) buildLoadBalancerSpec(ctx context.Context, schem
6766
LoadBalancerAttributes: lbAttributes,
6867
Tags: tags,
6968
}
70-
return spec, ec2Subnets, nil
69+
return spec, nil
7170
}
7271

7372
func (t *defaultModelBuildTask) buildLoadBalancerIPAddressType(_ context.Context) (elbv2model.IPAddressType, error) {
@@ -179,31 +178,29 @@ func (t *defaultModelBuildTask) buildLoadBalancerTags(ctx context.Context) (map[
179178
return t.buildAdditionalResourceTags(ctx)
180179
}
181180

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) {
184182
var eipAllocation []string
185183
eipConfigured := t.annotationParser.ParseStringSliceAnnotation(annotations.SvcLBSuffixEIPAllocations, &eipAllocation, t.service.Annotations)
186184
var privateIpv4Addresses []string
187185
ipv4Configured := t.annotationParser.ParseStringSliceAnnotation(annotations.SvcLBSuffixPrivateIpv4Addresses, &privateIpv4Addresses, t.service.Annotations)
188186

189187
// Validation
190188
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")
192190
}
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+
}
203197
}
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+
}
207204
}
208205

209206
subnetMappings := make([]elbv2model.SubnetMapping, 0, len(ec2Subnets))
@@ -217,13 +214,13 @@ func (t *defaultModelBuildTask) buildLoadBalancerSubnetMappings(ctx context.Cont
217214
if ipv4Configured {
218215
ip, err := t.getMatchingIPforSubnet(ctx, subnet, privateIpv4Addresses)
219216
if err != nil {
220-
return []elbv2model.SubnetMapping{}, nil, err
217+
return []elbv2model.SubnetMapping{}, err
221218
}
222219
mapping.PrivateIPv4Address = aws.String(ip)
223220
}
224221
subnetMappings = append(subnetMappings, mapping)
225222
}
226-
return subnetMappings, ec2Subnets, nil
223+
return subnetMappings, nil
227224
}
228225

229226
// Return the ip address which is in the subnet. Error if not match
@@ -245,7 +242,7 @@ func (t *defaultModelBuildTask) getMatchingIPforSubnet(_ context.Context, subnet
245242
return "", errors.Errorf("no matching ip for subnet %s", *subnet.SubnetId)
246243
}
247244

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) {
249246
var rawSubnetNameOrIDs []string
250247
if exists := t.annotationParser.ParseStringSliceAnnotation(annotations.SvcLBSuffixSubnets, &rawSubnetNameOrIDs, t.service.Annotations); exists {
251248
return t.subnetsResolver.ResolveViaNameOrIDSlice(ctx, rawSubnetNameOrIDs,
@@ -273,6 +270,8 @@ func (t *defaultModelBuildTask) buildLoadBalancerSubnets(ctx context.Context, sc
273270

274271
// for internet-facing Load Balancers, the subnets mush have at least 8 available IP addresses;
275272
// 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)
276275
if (scheme == elbv2model.LoadBalancerSchemeInternetFacing) ||
277276
((scheme == elbv2model.LoadBalancerSchemeInternal) && !ipv4Configured) {
278277
return t.subnetsResolver.ResolveViaDiscovery(ctx,

0 commit comments

Comments
 (0)