Merge pull request #61064 from johanneswuerbach/nlb-cross-zone

AWS NLB: Support cross-zone load balancing annotation
This commit is contained in:
Kubernetes Prow Robot 2019-01-03 03:53:33 -08:00 committed by GitHub
commit d7e0d9b45c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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(