2015-06-13 18:45:38 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-06-13 18:45:38 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-10-10 08:18:12 +00:00
|
|
|
package aws
|
2015-06-13 18:45:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-29 05:19:00 +00:00
|
|
|
"reflect"
|
2015-07-31 04:24:46 +00:00
|
|
|
"strconv"
|
2017-05-16 02:56:39 +00:00
|
|
|
"strings"
|
2015-07-31 04:24:46 +00:00
|
|
|
|
2015-06-13 18:45:38 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/aws/aws-sdk-go/service/elb"
|
|
|
|
"github.com/golang/glog"
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2015-06-13 18:45:38 +00:00
|
|
|
)
|
|
|
|
|
2016-04-20 21:41:18 +00:00
|
|
|
const ProxyProtocolPolicyName = "k8s-proxyprotocol-enabled"
|
|
|
|
|
2017-05-16 02:56:39 +00:00
|
|
|
// getLoadBalancerAdditionalTags converts the comma separated list of key-value
|
|
|
|
// pairs in the ServiceAnnotationLoadBalancerAdditionalTags annotation and returns
|
|
|
|
// it as a map.
|
|
|
|
func getLoadBalancerAdditionalTags(annotations map[string]string) map[string]string {
|
|
|
|
additionalTags := make(map[string]string)
|
|
|
|
if additionalTagsList, ok := annotations[ServiceAnnotationLoadBalancerAdditionalTags]; ok {
|
|
|
|
additionalTagsList = strings.TrimSpace(additionalTagsList)
|
|
|
|
|
|
|
|
// Break up list of "Key1=Val,Key2=Val2"
|
|
|
|
tagList := strings.Split(additionalTagsList, ",")
|
|
|
|
|
|
|
|
// Break up "Key=Val"
|
|
|
|
for _, tagSet := range tagList {
|
|
|
|
tag := strings.Split(strings.TrimSpace(tagSet), "=")
|
|
|
|
|
|
|
|
// Accept "Key=val" or "Key=" or just "Key"
|
|
|
|
if len(tag) >= 2 && len(tag[0]) != 0 {
|
|
|
|
// There is a key and a value, so save it
|
|
|
|
additionalTags[tag[0]] = tag[1]
|
|
|
|
} else if len(tag) == 1 && len(tag[0]) != 0 {
|
|
|
|
// Just "Key"
|
|
|
|
additionalTags[tag[0]] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return additionalTags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cloud) ensureLoadBalancer(namespacedName types.NamespacedName, loadBalancerName string, listeners []*elb.Listener, subnetIDs []string, securityGroupIDs []string, internalELB, proxyProtocol bool, loadBalancerAttributes *elb.LoadBalancerAttributes, annotations map[string]string) (*elb.LoadBalancerDescription, error) {
|
2016-04-17 20:31:02 +00:00
|
|
|
loadBalancer, err := c.describeLoadBalancer(loadBalancerName)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dirty := false
|
|
|
|
|
|
|
|
if loadBalancer == nil {
|
|
|
|
createRequest := &elb.CreateLoadBalancerInput{}
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
createRequest.LoadBalancerName = aws.String(loadBalancerName)
|
2015-06-13 18:45:38 +00:00
|
|
|
|
|
|
|
createRequest.Listeners = listeners
|
|
|
|
|
2016-03-04 17:15:12 +00:00
|
|
|
if internalELB {
|
|
|
|
createRequest.Scheme = aws.String("internal")
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:45:38 +00:00
|
|
|
// We are supposed to specify one subnet per AZ.
|
|
|
|
// TODO: What happens if we have more than one subnet per AZ?
|
|
|
|
createRequest.Subnets = stringPointerArray(subnetIDs)
|
|
|
|
|
|
|
|
createRequest.SecurityGroups = stringPointerArray(securityGroupIDs)
|
|
|
|
|
2017-05-16 02:56:39 +00:00
|
|
|
// Get additional tags set by the user
|
|
|
|
tags := getLoadBalancerAdditionalTags(annotations)
|
|
|
|
|
|
|
|
// Add default tags
|
|
|
|
tags[TagNameKubernetesService] = namespacedName.String()
|
|
|
|
tags = c.tagging.buildTags(ResourceLifecycleOwned, tags)
|
2017-02-18 18:11:08 +00:00
|
|
|
|
|
|
|
for k, v := range tags {
|
|
|
|
createRequest.Tags = append(createRequest.Tags, &elb.Tag{
|
|
|
|
Key: aws.String(k), Value: aws.String(v),
|
|
|
|
})
|
2016-02-07 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 03:14:45 +00:00
|
|
|
glog.Infof("Creating load balancer for %v with name: %s", namespacedName, loadBalancerName)
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.CreateLoadBalancer(createRequest)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-20 21:41:18 +00:00
|
|
|
|
|
|
|
if proxyProtocol {
|
2016-04-17 20:31:02 +00:00
|
|
|
err = c.createProxyProtocolPolicy(loadBalancerName)
|
2016-04-20 21:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, listener := range listeners {
|
|
|
|
glog.V(2).Infof("Adjusting AWS loadbalancer proxy protocol on node port %d. Setting to true", *listener.InstancePort)
|
2016-04-17 20:31:02 +00:00
|
|
|
err := c.setBackendPolicies(loadBalancerName, *listener.InstancePort, []*string{aws.String(ProxyProtocolPolicyName)})
|
2016-04-20 21:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:45:38 +00:00
|
|
|
dirty = true
|
|
|
|
} else {
|
2016-03-04 17:15:12 +00:00
|
|
|
// TODO: Sync internal vs non-internal
|
|
|
|
|
2015-06-13 18:45:38 +00:00
|
|
|
{
|
|
|
|
// Sync subnets
|
2015-09-09 17:45:01 +00:00
|
|
|
expected := sets.NewString(subnetIDs...)
|
2015-06-13 18:45:38 +00:00
|
|
|
actual := stringSetFromPointers(loadBalancer.Subnets)
|
|
|
|
|
|
|
|
additions := expected.Difference(actual)
|
|
|
|
removals := actual.Difference(expected)
|
|
|
|
|
2015-09-21 01:51:25 +00:00
|
|
|
if removals.Len() != 0 {
|
2015-06-13 18:45:38 +00:00
|
|
|
request := &elb.DetachLoadBalancerFromSubnetsInput{}
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
request.LoadBalancerName = aws.String(loadBalancerName)
|
2015-06-13 18:45:38 +00:00
|
|
|
request.Subnets = stringSetToPointers(removals)
|
|
|
|
glog.V(2).Info("Detaching load balancer from removed subnets")
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.DetachLoadBalancerFromSubnets(request)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
2017-06-22 19:08:20 +00:00
|
|
|
return nil, fmt.Errorf("error detaching AWS loadbalancer from subnets: %q", err)
|
2015-06-13 18:45:38 +00:00
|
|
|
}
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
|
2015-09-21 01:51:25 +00:00
|
|
|
if additions.Len() != 0 {
|
2015-06-13 18:45:38 +00:00
|
|
|
request := &elb.AttachLoadBalancerToSubnetsInput{}
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
request.LoadBalancerName = aws.String(loadBalancerName)
|
2015-06-13 18:45:38 +00:00
|
|
|
request.Subnets = stringSetToPointers(additions)
|
|
|
|
glog.V(2).Info("Attaching load balancer to added subnets")
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.AttachLoadBalancerToSubnets(request)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
2017-06-22 19:08:20 +00:00
|
|
|
return nil, fmt.Errorf("error attaching AWS loadbalancer to subnets: %q", err)
|
2015-06-13 18:45:38 +00:00
|
|
|
}
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Sync security groups
|
2015-09-09 17:45:01 +00:00
|
|
|
expected := sets.NewString(securityGroupIDs...)
|
2015-06-13 18:45:38 +00:00
|
|
|
actual := stringSetFromPointers(loadBalancer.SecurityGroups)
|
|
|
|
|
|
|
|
if !expected.Equal(actual) {
|
|
|
|
// This call just replaces the security groups, unlike e.g. subnets (!)
|
|
|
|
request := &elb.ApplySecurityGroupsToLoadBalancerInput{}
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
request.LoadBalancerName = aws.String(loadBalancerName)
|
2015-06-13 18:45:38 +00:00
|
|
|
request.SecurityGroups = stringPointerArray(securityGroupIDs)
|
|
|
|
glog.V(2).Info("Applying updated security groups to load balancer")
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.ApplySecurityGroupsToLoadBalancer(request)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
2017-06-22 19:08:20 +00:00
|
|
|
return nil, fmt.Errorf("error applying AWS loadbalancer security groups: %q", err)
|
2015-06-13 18:45:38 +00:00
|
|
|
}
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Sync listeners
|
|
|
|
listenerDescriptions := loadBalancer.ListenerDescriptions
|
|
|
|
|
|
|
|
foundSet := make(map[int]bool)
|
|
|
|
removals := []*int64{}
|
|
|
|
for _, listenerDescription := range listenerDescriptions {
|
|
|
|
actual := listenerDescription.Listener
|
|
|
|
if actual == nil {
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
glog.Warning("Ignoring empty listener in AWS loadbalancer: ", loadBalancerName)
|
2015-06-13 18:45:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
found := -1
|
|
|
|
for i, expected := range listeners {
|
2017-06-13 03:30:53 +00:00
|
|
|
if elbProtocolsAreEqual(actual.Protocol, expected.Protocol) {
|
2015-06-13 18:45:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-06-13 03:30:53 +00:00
|
|
|
if elbProtocolsAreEqual(actual.InstanceProtocol, expected.InstanceProtocol) {
|
2015-06-13 18:45:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if orZero(actual.InstancePort) != orZero(expected.InstancePort) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if orZero(actual.LoadBalancerPort) != orZero(expected.LoadBalancerPort) {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-13 03:30:53 +00:00
|
|
|
if awsArnEquals(actual.SSLCertificateId, expected.SSLCertificateId) {
|
2015-06-13 18:45:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
found = i
|
|
|
|
}
|
|
|
|
if found != -1 {
|
|
|
|
foundSet[found] = true
|
|
|
|
} else {
|
|
|
|
removals = append(removals, actual.LoadBalancerPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
additions := []*elb.Listener{}
|
|
|
|
for i := range listeners {
|
|
|
|
if foundSet[i] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
additions = append(additions, listeners[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(removals) != 0 {
|
|
|
|
request := &elb.DeleteLoadBalancerListenersInput{}
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
request.LoadBalancerName = aws.String(loadBalancerName)
|
2015-06-13 18:45:38 +00:00
|
|
|
request.LoadBalancerPorts = removals
|
|
|
|
glog.V(2).Info("Deleting removed load balancer listeners")
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.DeleteLoadBalancerListeners(request)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
2017-06-22 19:08:20 +00:00
|
|
|
return nil, fmt.Errorf("error deleting AWS loadbalancer listeners: %q", err)
|
2015-06-13 18:45:38 +00:00
|
|
|
}
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(additions) != 0 {
|
|
|
|
request := &elb.CreateLoadBalancerListenersInput{}
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
request.LoadBalancerName = aws.String(loadBalancerName)
|
2015-06-13 18:45:38 +00:00
|
|
|
request.Listeners = additions
|
|
|
|
glog.V(2).Info("Creating added load balancer listeners")
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.CreateLoadBalancerListeners(request)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
2017-06-22 19:08:20 +00:00
|
|
|
return nil, fmt.Errorf("error creating AWS loadbalancer listeners: %q", err)
|
2015-06-13 18:45:38 +00:00
|
|
|
}
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 21:41:18 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Sync proxy protocol state for new and existing listeners
|
|
|
|
|
|
|
|
proxyPolicies := make([]*string, 0)
|
|
|
|
if proxyProtocol {
|
|
|
|
// Ensure the backend policy exists
|
|
|
|
|
|
|
|
// NOTE The documentation for the AWS API indicates we could get an HTTP 400
|
|
|
|
// back if a policy of the same name already exists. However, the aws-sdk does not
|
2016-08-30 21:46:06 +00:00
|
|
|
// seem to return an error to us in these cases. Therefore, this will issue an API
|
2016-06-17 16:27:25 +00:00
|
|
|
// request every time.
|
2016-04-17 20:31:02 +00:00
|
|
|
err := c.createProxyProtocolPolicy(loadBalancerName)
|
2016-04-20 21:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyPolicies = append(proxyPolicies, aws.String(ProxyProtocolPolicyName))
|
|
|
|
}
|
|
|
|
|
|
|
|
foundBackends := make(map[int64]bool)
|
|
|
|
proxyProtocolBackends := make(map[int64]bool)
|
|
|
|
for _, backendListener := range loadBalancer.BackendServerDescriptions {
|
|
|
|
foundBackends[*backendListener.InstancePort] = false
|
|
|
|
proxyProtocolBackends[*backendListener.InstancePort] = proxyProtocolEnabled(backendListener)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, listener := range listeners {
|
|
|
|
setPolicy := false
|
|
|
|
instancePort := *listener.InstancePort
|
|
|
|
|
|
|
|
if currentState, ok := proxyProtocolBackends[instancePort]; !ok {
|
|
|
|
// This is a new ELB backend so we only need to worry about
|
2016-08-04 09:16:50 +00:00
|
|
|
// potentially adding a policy and not removing an
|
2016-04-20 21:41:18 +00:00
|
|
|
// existing one
|
|
|
|
setPolicy = proxyProtocol
|
|
|
|
} else {
|
|
|
|
foundBackends[instancePort] = true
|
|
|
|
// This is an existing ELB backend so we need to determine
|
|
|
|
// if the state changed
|
|
|
|
setPolicy = (currentState != proxyProtocol)
|
|
|
|
}
|
|
|
|
|
|
|
|
if setPolicy {
|
|
|
|
glog.V(2).Infof("Adjusting AWS loadbalancer proxy protocol on node port %d. Setting to %t", instancePort, proxyProtocol)
|
2016-04-17 20:31:02 +00:00
|
|
|
err := c.setBackendPolicies(loadBalancerName, instancePort, proxyPolicies)
|
2016-04-20 21:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We now need to figure out if any backend policies need removed
|
|
|
|
// because these old policies will stick around even if there is no
|
|
|
|
// corresponding listener anymore
|
|
|
|
for instancePort, found := range foundBackends {
|
|
|
|
if !found {
|
|
|
|
glog.V(2).Infof("Adjusting AWS loadbalancer proxy protocol on node port %d. Setting to false", instancePort)
|
2016-04-17 20:31:02 +00:00
|
|
|
err := c.setBackendPolicies(loadBalancerName, instancePort, []*string{})
|
2016-04-20 21:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 18:45:38 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 05:19:00 +00:00
|
|
|
// 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 := &elb.DescribeLoadBalancerAttributesInput{}
|
|
|
|
describeAttributesRequest.LoadBalancerName = aws.String(loadBalancerName)
|
|
|
|
describeAttributesOutput, err := c.elb.DescribeLoadBalancerAttributes(describeAttributesRequest)
|
|
|
|
if err != nil {
|
|
|
|
glog.Warning("Unable to retrieve load balancer attributes during attribute sync")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
foundAttributes := &describeAttributesOutput.LoadBalancerAttributes
|
|
|
|
|
|
|
|
// Update attributes if they're dirty
|
|
|
|
if !reflect.DeepEqual(loadBalancerAttributes, foundAttributes) {
|
2017-01-16 21:27:39 +00:00
|
|
|
glog.V(2).Infof("Updating load-balancer attributes for %q", loadBalancerName)
|
2016-09-27 15:19:19 +00:00
|
|
|
|
2016-04-29 05:19:00 +00:00
|
|
|
modifyAttributesRequest := &elb.ModifyLoadBalancerAttributesInput{}
|
|
|
|
modifyAttributesRequest.LoadBalancerName = aws.String(loadBalancerName)
|
|
|
|
modifyAttributesRequest.LoadBalancerAttributes = loadBalancerAttributes
|
|
|
|
_, err = c.elb.ModifyLoadBalancerAttributes(modifyAttributesRequest)
|
|
|
|
if err != nil {
|
2017-06-22 19:08:20 +00:00
|
|
|
return nil, fmt.Errorf("Unable to update load balancer attributes during attribute sync: %q", err)
|
2016-04-29 05:19:00 +00:00
|
|
|
}
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:45:38 +00:00
|
|
|
if dirty {
|
2016-04-17 20:31:02 +00:00
|
|
|
loadBalancer, err = c.describeLoadBalancer(loadBalancerName)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Warning("Unable to retrieve load balancer after creation/update")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadBalancer, nil
|
|
|
|
}
|
|
|
|
|
2017-06-13 03:30:53 +00:00
|
|
|
// elbProtocolsAreEqual checks if two ELB protocol strings are considered the same
|
|
|
|
// Comparison is case insensitive
|
|
|
|
func elbProtocolsAreEqual(l, r *string) bool {
|
|
|
|
if l == nil || r == nil {
|
|
|
|
return l == r
|
|
|
|
}
|
|
|
|
return strings.EqualFold(aws.StringValue(l), aws.StringValue(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
// awsArnEquals checks if two ARN strings are considered the same
|
|
|
|
// Comparison is case insensitive
|
|
|
|
func awsArnEquals(l, r *string) bool {
|
|
|
|
if l == nil || r == nil {
|
|
|
|
return l == r
|
|
|
|
}
|
|
|
|
return strings.EqualFold(aws.StringValue(l), aws.StringValue(r))
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:43:57 +00:00
|
|
|
// Makes sure that the health check for an ELB matches the configured health check node port
|
2017-04-19 21:12:28 +00:00
|
|
|
func (c *Cloud) ensureLoadBalancerHealthCheck(loadBalancer *elb.LoadBalancerDescription, protocol string, port int32, path string) error {
|
2016-09-27 15:20:42 +00:00
|
|
|
name := aws.StringValue(loadBalancer.LoadBalancerName)
|
|
|
|
|
2015-07-31 04:24:46 +00:00
|
|
|
actual := loadBalancer.HealthCheck
|
|
|
|
|
|
|
|
// Default AWS settings
|
2016-01-22 03:31:21 +00:00
|
|
|
expectedHealthyThreshold := int64(2)
|
|
|
|
expectedUnhealthyThreshold := int64(6)
|
2015-07-31 04:24:46 +00:00
|
|
|
expectedTimeout := int64(5)
|
2016-01-22 03:31:21 +00:00
|
|
|
expectedInterval := int64(10)
|
2015-07-31 04:24:46 +00:00
|
|
|
|
2017-04-19 21:12:28 +00:00
|
|
|
expectedTarget := protocol + ":" + strconv.FormatInt(int64(port), 10) + path
|
2015-07-31 04:24:46 +00:00
|
|
|
|
2017-08-21 01:37:49 +00:00
|
|
|
if expectedTarget == aws.StringValue(actual.Target) &&
|
2015-07-31 04:24:46 +00:00
|
|
|
expectedHealthyThreshold == orZero(actual.HealthyThreshold) &&
|
|
|
|
expectedUnhealthyThreshold == orZero(actual.UnhealthyThreshold) &&
|
|
|
|
expectedTimeout == orZero(actual.Timeout) &&
|
|
|
|
expectedInterval == orZero(actual.Interval) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-16 21:27:39 +00:00
|
|
|
glog.V(2).Infof("Updating load-balancer health-check for %q", name)
|
2015-07-31 04:24:46 +00:00
|
|
|
|
|
|
|
healthCheck := &elb.HealthCheck{}
|
|
|
|
healthCheck.HealthyThreshold = &expectedHealthyThreshold
|
|
|
|
healthCheck.UnhealthyThreshold = &expectedUnhealthyThreshold
|
|
|
|
healthCheck.Timeout = &expectedTimeout
|
|
|
|
healthCheck.Interval = &expectedInterval
|
|
|
|
healthCheck.Target = &expectedTarget
|
|
|
|
|
|
|
|
request := &elb.ConfigureHealthCheckInput{}
|
|
|
|
request.HealthCheck = healthCheck
|
|
|
|
request.LoadBalancerName = loadBalancer.LoadBalancerName
|
|
|
|
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.ConfigureHealthCheck(request)
|
2015-07-31 04:24:46 +00:00
|
|
|
if err != nil {
|
2017-06-22 19:08:20 +00:00
|
|
|
return fmt.Errorf("error configuring load-balancer health-check for %q: %q", name, err)
|
2015-07-31 04:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:45:38 +00:00
|
|
|
// Makes sure that exactly the specified hosts are registered as instances with the load balancer
|
2017-06-13 07:47:22 +00:00
|
|
|
func (c *Cloud) ensureLoadBalancerInstances(loadBalancerName string, lbInstances []*elb.Instance, instanceIDs map[awsInstanceID]*ec2.Instance) error {
|
2015-09-09 17:45:01 +00:00
|
|
|
expected := sets.NewString()
|
2017-06-13 07:47:22 +00:00
|
|
|
for id := range instanceIDs {
|
|
|
|
expected.Insert(string(id))
|
2015-06-13 18:45:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 17:45:01 +00:00
|
|
|
actual := sets.NewString()
|
2015-06-13 18:45:38 +00:00
|
|
|
for _, lbInstance := range lbInstances {
|
2017-08-21 01:37:49 +00:00
|
|
|
actual.Insert(aws.StringValue(lbInstance.InstanceId))
|
2015-06-13 18:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
additions := expected.Difference(actual)
|
|
|
|
removals := actual.Difference(expected)
|
|
|
|
|
|
|
|
addInstances := []*elb.Instance{}
|
2015-09-21 01:51:25 +00:00
|
|
|
for _, instanceId := range additions.List() {
|
2015-06-13 18:45:38 +00:00
|
|
|
addInstance := &elb.Instance{}
|
2015-09-20 21:10:57 +00:00
|
|
|
addInstance.InstanceId = aws.String(instanceId)
|
2015-06-13 18:45:38 +00:00
|
|
|
addInstances = append(addInstances, addInstance)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeInstances := []*elb.Instance{}
|
2015-09-21 01:51:25 +00:00
|
|
|
for _, instanceId := range removals.List() {
|
2015-06-13 18:45:38 +00:00
|
|
|
removeInstance := &elb.Instance{}
|
2015-09-20 21:10:57 +00:00
|
|
|
removeInstance.InstanceId = aws.String(instanceId)
|
2015-06-13 18:45:38 +00:00
|
|
|
removeInstances = append(removeInstances, removeInstance)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addInstances) > 0 {
|
|
|
|
registerRequest := &elb.RegisterInstancesWithLoadBalancerInput{}
|
|
|
|
registerRequest.Instances = addInstances
|
|
|
|
registerRequest.LoadBalancerName = aws.String(loadBalancerName)
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.RegisterInstancesWithLoadBalancer(registerRequest)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(1).Infof("Instances added to load-balancer %s", loadBalancerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(removeInstances) > 0 {
|
|
|
|
deregisterRequest := &elb.DeregisterInstancesFromLoadBalancerInput{}
|
|
|
|
deregisterRequest.Instances = removeInstances
|
|
|
|
deregisterRequest.LoadBalancerName = aws.String(loadBalancerName)
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.DeregisterInstancesFromLoadBalancer(deregisterRequest)
|
2015-06-13 18:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(1).Infof("Instances removed from load-balancer %s", loadBalancerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-20 21:41:18 +00:00
|
|
|
|
2016-04-17 20:31:02 +00:00
|
|
|
func (c *Cloud) createProxyProtocolPolicy(loadBalancerName string) error {
|
2016-04-20 21:41:18 +00:00
|
|
|
request := &elb.CreateLoadBalancerPolicyInput{
|
|
|
|
LoadBalancerName: aws.String(loadBalancerName),
|
|
|
|
PolicyName: aws.String(ProxyProtocolPolicyName),
|
|
|
|
PolicyTypeName: aws.String("ProxyProtocolPolicyType"),
|
|
|
|
PolicyAttributes: []*elb.PolicyAttribute{
|
|
|
|
{
|
|
|
|
AttributeName: aws.String("ProxyProtocol"),
|
|
|
|
AttributeValue: aws.String("true"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
glog.V(2).Info("Creating proxy protocol policy on load balancer")
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.CreateLoadBalancerPolicy(request)
|
2016-04-20 21:41:18 +00:00
|
|
|
if err != nil {
|
2017-06-22 19:08:20 +00:00
|
|
|
return fmt.Errorf("error creating proxy protocol policy on load balancer: %q", err)
|
2016-04-20 21:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-17 20:31:02 +00:00
|
|
|
func (c *Cloud) setBackendPolicies(loadBalancerName string, instancePort int64, policies []*string) error {
|
2016-04-20 21:41:18 +00:00
|
|
|
request := &elb.SetLoadBalancerPoliciesForBackendServerInput{
|
|
|
|
InstancePort: aws.Int64(instancePort),
|
|
|
|
LoadBalancerName: aws.String(loadBalancerName),
|
|
|
|
PolicyNames: policies,
|
|
|
|
}
|
|
|
|
if len(policies) > 0 {
|
|
|
|
glog.V(2).Infof("Adding AWS loadbalancer backend policies on node port %d", instancePort)
|
|
|
|
} else {
|
|
|
|
glog.V(2).Infof("Removing AWS loadbalancer backend policies on node port %d", instancePort)
|
|
|
|
}
|
2016-04-17 20:31:02 +00:00
|
|
|
_, err := c.elb.SetLoadBalancerPoliciesForBackendServer(request)
|
2016-04-20 21:41:18 +00:00
|
|
|
if err != nil {
|
2017-06-22 19:08:20 +00:00
|
|
|
return fmt.Errorf("error adjusting AWS loadbalancer backend policies: %q", err)
|
2016-04-20 21:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func proxyProtocolEnabled(backend *elb.BackendServerDescription) bool {
|
|
|
|
for _, policy := range backend.PolicyNames {
|
|
|
|
if aws.StringValue(policy) == ProxyProtocolPolicyName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2017-06-13 07:47:22 +00:00
|
|
|
|
|
|
|
// findInstancesForELB gets the EC2 instances corresponding to the Nodes, for setting up an ELB
|
|
|
|
// We ignore Nodes (with a log message) where the instanceid cannot be determined from the provider,
|
|
|
|
// and we ignore instances which are not found
|
|
|
|
func (c *Cloud) findInstancesForELB(nodes []*v1.Node) (map[awsInstanceID]*ec2.Instance, error) {
|
|
|
|
// Map to instance ids ignoring Nodes where we cannot find the id (but logging)
|
|
|
|
instanceIDs := mapToAWSInstanceIDsTolerant(nodes)
|
|
|
|
|
|
|
|
cacheCriteria := cacheCriteria{
|
|
|
|
// MaxAge not required, because we only care about security groups, which should not change
|
|
|
|
HasInstances: instanceIDs, // Refresh if any of the instance ids are missing
|
|
|
|
}
|
|
|
|
snapshot, err := c.instanceCache.describeAllInstancesCached(cacheCriteria)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
instances := snapshot.FindInstances(instanceIDs)
|
|
|
|
// We ignore instances that cannot be found
|
|
|
|
|
|
|
|
return instances, nil
|
|
|
|
}
|