AWS NLB: Support cross-zone load balancing annotation

AWS Network Load Balancer recently got support for cross-zone load balancing.

Use the existing `service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled`
annotation to configure it.
This commit is contained in:
Johannes Würbach 2018-03-12 22:03:01 +01:00
parent a7cb03f4cf
commit 5e6d865794
No known key found for this signature in database
GPG Key ID: 74DB0F4D956CCCE3

View File

@ -313,6 +313,64 @@ func (c *Cloud) ensureLoadBalancerv2(namespacedName types.NamespacedName, loadBa
}
}
desiredLoadBalancerAttributes := map[string]string{}
// Default values to ensured a remove annotation reverts back to the default
desiredLoadBalancerAttributes["load_balancing.cross_zone.enabled"] = "false"
// Determine if cross zone load balancing enabled/disabled has been specified
crossZoneLoadBalancingEnabledAnnotation := annotations[ServiceAnnotationLoadBalancerCrossZoneLoadBalancingEnabled]
if crossZoneLoadBalancingEnabledAnnotation != "" {
crossZoneEnabled, err := strconv.ParseBool(crossZoneLoadBalancingEnabledAnnotation)
if err != nil {
return nil, fmt.Errorf("error parsing service annotation: %s=%s",
ServiceAnnotationLoadBalancerCrossZoneLoadBalancingEnabled,
crossZoneLoadBalancingEnabledAnnotation,
)
}
if crossZoneEnabled {
desiredLoadBalancerAttributes["load_balancing.cross_zone.enabled"] = "true"
}
}
// Whether the ELB was new or existing, sync attributes regardless. This accounts for things
// that cannot be specified at the time of creation and can only be modified after the fact,
// e.g. idle connection timeout.
describeAttributesRequest := &elbv2.DescribeLoadBalancerAttributesInput{
LoadBalancerArn: loadBalancer.LoadBalancerArn,
}
describeAttributesOutput, err := c.elbv2.DescribeLoadBalancerAttributes(describeAttributesRequest)
if err != nil {
return nil, fmt.Errorf("Unable to retrieve load balancer attributes during attribute sync: %q", err)
}
changedAttributes := []*elbv2.LoadBalancerAttribute{}
// Identify to be changed attributes
for _, foundAttribute := range describeAttributesOutput.Attributes {
if targetValue, ok := desiredLoadBalancerAttributes[*foundAttribute.Key]; ok {
if targetValue != *foundAttribute.Value {
changedAttributes = append(changedAttributes, &elbv2.LoadBalancerAttribute{
Key: foundAttribute.Key,
Value: aws.String(targetValue),
})
}
}
}
// Update attributes requiring changes
if len(changedAttributes) > 0 {
klog.V(2).Infof("Updating load-balancer attributes for %q", loadBalancerName)
_, err = c.elbv2.ModifyLoadBalancerAttributes(&elbv2.ModifyLoadBalancerAttributesInput{
LoadBalancerArn: loadBalancer.LoadBalancerArn,
Attributes: changedAttributes,
})
if err != nil {
return nil, fmt.Errorf("Unable to update load balancer attributes during attribute sync: %q", err)
}
}
// Subnets cannot be modified on NLBs
if dirty {
loadBalancers, err := c.elbv2.DescribeLoadBalancers(