Skip to content

Commit 34edeed

Browse files
committed
Correct arguments and clarify var names
* reconcileTargetGroupsAndListeners needs both the desired specification as well as the ARN assigned by the AWS API calls. * Rename `spec` in reconcileV2LB to `desiredLB` to clarify what data is actually held in it. Signed-off-by: Nolan Brubaker <[email protected]>
1 parent a49371d commit 34edeed

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

pkg/cloud/services/elb/loadbalancer.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (s *Service) reconcileV2LB(lbSpec *infrav1.AWSLoadBalancerSpec) error {
8787
}
8888

8989
// Get default api server spec.
90-
spec, err := s.getAPIServerLBSpec(name, lbSpec)
90+
desiredLB, err := s.getAPIServerLBSpec(name, lbSpec)
9191
if err != nil {
9292
return err
9393
}
@@ -97,7 +97,7 @@ func (s *Service) reconcileV2LB(lbSpec *infrav1.AWSLoadBalancerSpec) error {
9797
// if elb is not found and owner cluster ControlPlaneEndpoint is already populated, then we should not recreate the elb.
9898
return errors.Wrapf(err, "no loadbalancer exists for the AWSCluster %s, the cluster has become unrecoverable and should be deleted manually", s.scope.InfraClusterName())
9999
case IsNotFound(err):
100-
lb, err = s.createLB(spec, lbSpec)
100+
lb, err = s.createLB(desiredLB, lbSpec)
101101
if err != nil {
102102
s.scope.Error(err, "failed to create LB")
103103
return err
@@ -113,41 +113,42 @@ func (s *Service) reconcileV2LB(lbSpec *infrav1.AWSLoadBalancerSpec) error {
113113
lb.LoadBalancerType = lbSpec.LoadBalancerType
114114
if lb.IsManaged(s.scope.Name()) {
115115
// Reconcile the target groups and listeners from the spec and the ones currently attached to the load balancer.
116-
_, _, err := s.reconcileTargetGroupsAndListeners(lb, lbSpec)
116+
// Pass in the ARN that AWS gave us, as well as the rest of the desired specification.
117+
_, _, err := s.reconcileTargetGroupsAndListeners(lb.ARN, desiredLB, lbSpec)
117118
if err != nil {
118119
return errors.Wrapf(err, "failed to create target groups/listeners for load balancer %q", lb.Name)
119120
}
120121

121-
if !cmp.Equal(spec.ELBAttributes, lb.ELBAttributes) {
122-
if err := s.configureLBAttributes(lb.ARN, spec.ELBAttributes); err != nil {
122+
if !cmp.Equal(desiredLB.ELBAttributes, lb.ELBAttributes) {
123+
if err := s.configureLBAttributes(lb.ARN, desiredLB.ELBAttributes); err != nil {
123124
return err
124125
}
125126
}
126127

127-
if err := s.reconcileV2LBTags(lb, spec.Tags); err != nil {
128+
if err := s.reconcileV2LBTags(lb, desiredLB.Tags); err != nil {
128129
return errors.Wrapf(err, "failed to reconcile tags for apiserver load balancer %q", lb.Name)
129130
}
130131

131-
// Reconcile the subnets and availability zones from the spec
132+
// Reconcile the subnets and availability zones from the desiredLB
132133
// and the ones currently attached to the load balancer.
133-
if len(lb.SubnetIDs) != len(spec.SubnetIDs) {
134+
if len(lb.SubnetIDs) != len(desiredLB.SubnetIDs) {
134135
_, err := s.ELBV2Client.SetSubnets(&elbv2.SetSubnetsInput{
135136
LoadBalancerArn: &lb.ARN,
136-
Subnets: aws.StringSlice(spec.SubnetIDs),
137+
Subnets: aws.StringSlice(desiredLB.SubnetIDs),
137138
})
138139
if err != nil {
139140
return errors.Wrapf(err, "failed to set subnets for apiserver load balancer '%s'", lb.Name)
140141
}
141142
}
142-
if len(lb.AvailabilityZones) != len(spec.AvailabilityZones) {
143-
lb.AvailabilityZones = spec.AvailabilityZones
143+
if len(lb.AvailabilityZones) != len(desiredLB.AvailabilityZones) {
144+
lb.AvailabilityZones = desiredLB.AvailabilityZones
144145
}
145146

146-
// Reconcile the security groups from the spec and the ones currently attached to the load balancer
147-
if shouldReconcileSGs(s.scope, lb, spec.SecurityGroupIDs) {
147+
// Reconcile the security groups from the desiredLB and the ones currently attached to the load balancer
148+
if shouldReconcileSGs(s.scope, lb, desiredLB.SecurityGroupIDs) {
148149
_, err := s.ELBV2Client.SetSecurityGroups(&elbv2.SetSecurityGroupsInput{
149150
LoadBalancerArn: &lb.ARN,
150-
SecurityGroups: aws.StringSlice(spec.SecurityGroupIDs),
151+
SecurityGroups: aws.StringSlice(desiredLB.SecurityGroupIDs),
151152
})
152153
if err != nil {
153154
return errors.Wrapf(err, "failed to apply security groups to load balancer %q", lb.Name)
@@ -401,6 +402,7 @@ func (s *Service) createLB(spec *infrav1.LoadBalancer, lbSpec *infrav1.AWSLoadBa
401402
res := spec.DeepCopy()
402403
s.scope.Debug("applying load balancer DNS to result", "dns", *out.LoadBalancers[0].DNSName)
403404
res.DNSName = *out.LoadBalancers[0].DNSName
405+
res.ARN = *out.LoadBalancers[0].LoadBalancerArn
404406
return res, nil
405407
}
406408

@@ -1530,22 +1532,22 @@ func (s *Service) reconcileV2LBTags(lb *infrav1.LoadBalancer, desiredTags map[st
15301532

15311533
// reconcileTargetGroupsAndListeners reconciles a Load Balancer's defined listeners with corresponding AWS Target Groups and Listeners.
15321534
// These are combined into a single function since they are tightly integrated.
1533-
func (s *Service) reconcileTargetGroupsAndListeners(spec *infrav1.LoadBalancer, lbSpec *infrav1.AWSLoadBalancerSpec) ([]*elbv2.TargetGroup, []*elbv2.Listener, error) {
1535+
func (s *Service) reconcileTargetGroupsAndListeners(lbARN string, spec *infrav1.LoadBalancer, lbSpec *infrav1.AWSLoadBalancerSpec) ([]*elbv2.TargetGroup, []*elbv2.Listener, error) {
15341536
existingTargetGroups, err := s.ELBV2Client.DescribeTargetGroups(
15351537
&elbv2.DescribeTargetGroupsInput{
1536-
LoadBalancerArn: aws.String(spec.ARN),
1538+
LoadBalancerArn: aws.String(lbARN),
15371539
})
15381540
if err != nil {
1539-
s.scope.Error(err, "could not describe target groups for load balancer", "arn", spec.ARN)
1541+
s.scope.Error(err, "could not describe target groups for load balancer", "arn", lbARN)
15401542
return nil, nil, err
15411543
}
15421544

15431545
existingListeners, err := s.ELBV2Client.DescribeListeners(
15441546
&elbv2.DescribeListenersInput{
1545-
LoadBalancerArn: aws.String(spec.ARN),
1547+
LoadBalancerArn: aws.String(lbARN),
15461548
})
15471549
if err != nil {
1548-
s.scope.Error(err, "could not describe listeners for load balancer", "arn", spec.ARN)
1550+
s.scope.Error(err, "could not describe listeners for load balancer", "arn", lbARN)
15491551
}
15501552

15511553
if len(existingTargetGroups.TargetGroups) == len(existingListeners.Listeners) && len(existingListeners.Listeners) == len(spec.ELBListeners) {
@@ -1596,7 +1598,7 @@ func (s *Service) reconcileTargetGroupsAndListeners(spec *infrav1.LoadBalancer,
15961598
}
15971599

15981600
if listener == nil {
1599-
listener, err = s.createListener(ln, group, spec.ARN, spec.Tags)
1601+
listener, err = s.createListener(ln, group, lbARN, spec.Tags)
16001602
if err != nil {
16011603
return nil, nil, err
16021604
}

pkg/cloud/services/elb/loadbalancer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,7 @@ func TestReconcileTargetGroupsAndListeners(t *testing.T) {
21982198
}
21992199

22002200
spec := tc.spec(*loadBalancerSpec)
2201-
tgs, listeners, err := s.reconcileTargetGroupsAndListeners(&spec, clusterScope.ControlPlaneLoadBalancer())
2201+
tgs, listeners, err := s.reconcileTargetGroupsAndListeners(spec.ARN, &spec, clusterScope.ControlPlaneLoadBalancer())
22022202
tc.check(t, tgs, listeners, err)
22032203
})
22042204
}

0 commit comments

Comments
 (0)