Skip to content

Commit d7e0d9b

Browse files
authored
Merge pull request kubernetes#61064 from johanneswuerbach/nlb-cross-zone
AWS NLB: Support cross-zone load balancing annotation
2 parents 60561cd + 5e6d865 commit d7e0d9b

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

pkg/cloudprovider/providers/aws/aws_loadbalancer.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,64 @@ func (c *Cloud) ensureLoadBalancerv2(namespacedName types.NamespacedName, loadBa
313313
}
314314
}
315315

316+
desiredLoadBalancerAttributes := map[string]string{}
317+
// Default values to ensured a remove annotation reverts back to the default
318+
desiredLoadBalancerAttributes["load_balancing.cross_zone.enabled"] = "false"
319+
320+
// Determine if cross zone load balancing enabled/disabled has been specified
321+
crossZoneLoadBalancingEnabledAnnotation := annotations[ServiceAnnotationLoadBalancerCrossZoneLoadBalancingEnabled]
322+
if crossZoneLoadBalancingEnabledAnnotation != "" {
323+
crossZoneEnabled, err := strconv.ParseBool(crossZoneLoadBalancingEnabledAnnotation)
324+
if err != nil {
325+
return nil, fmt.Errorf("error parsing service annotation: %s=%s",
326+
ServiceAnnotationLoadBalancerCrossZoneLoadBalancingEnabled,
327+
crossZoneLoadBalancingEnabledAnnotation,
328+
)
329+
}
330+
331+
if crossZoneEnabled {
332+
desiredLoadBalancerAttributes["load_balancing.cross_zone.enabled"] = "true"
333+
}
334+
}
335+
336+
// Whether the ELB was new or existing, sync attributes regardless. This accounts for things
337+
// that cannot be specified at the time of creation and can only be modified after the fact,
338+
// e.g. idle connection timeout.
339+
describeAttributesRequest := &elbv2.DescribeLoadBalancerAttributesInput{
340+
LoadBalancerArn: loadBalancer.LoadBalancerArn,
341+
}
342+
describeAttributesOutput, err := c.elbv2.DescribeLoadBalancerAttributes(describeAttributesRequest)
343+
if err != nil {
344+
return nil, fmt.Errorf("Unable to retrieve load balancer attributes during attribute sync: %q", err)
345+
}
346+
347+
changedAttributes := []*elbv2.LoadBalancerAttribute{}
348+
349+
// Identify to be changed attributes
350+
for _, foundAttribute := range describeAttributesOutput.Attributes {
351+
if targetValue, ok := desiredLoadBalancerAttributes[*foundAttribute.Key]; ok {
352+
if targetValue != *foundAttribute.Value {
353+
changedAttributes = append(changedAttributes, &elbv2.LoadBalancerAttribute{
354+
Key: foundAttribute.Key,
355+
Value: aws.String(targetValue),
356+
})
357+
}
358+
}
359+
}
360+
361+
// Update attributes requiring changes
362+
if len(changedAttributes) > 0 {
363+
klog.V(2).Infof("Updating load-balancer attributes for %q", loadBalancerName)
364+
365+
_, err = c.elbv2.ModifyLoadBalancerAttributes(&elbv2.ModifyLoadBalancerAttributesInput{
366+
LoadBalancerArn: loadBalancer.LoadBalancerArn,
367+
Attributes: changedAttributes,
368+
})
369+
if err != nil {
370+
return nil, fmt.Errorf("Unable to update load balancer attributes during attribute sync: %q", err)
371+
}
372+
}
373+
316374
// Subnets cannot be modified on NLBs
317375
if dirty {
318376
loadBalancers, err := c.elbv2.DescribeLoadBalancers(

0 commit comments

Comments
 (0)