|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + log "github.com/Sirupsen/logrus" |
| 7 | + "github.com/aws/aws-sdk-go/aws" |
| 8 | + "github.com/aws/aws-sdk-go/aws/credentials" |
| 9 | + "github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds" |
| 10 | + "github.com/aws/aws-sdk-go/aws/ec2metadata" |
| 11 | + "github.com/aws/aws-sdk-go/aws/session" |
| 12 | + "github.com/aws/aws-sdk-go/service/elb" |
| 13 | + "github.com/aws/aws-sdk-go/service/elb/elbiface" |
| 14 | + "github.com/docker/infrakit/pkg/spi/instance" |
| 15 | + "github.com/docker/infrakit/pkg/spi/loadbalancer" |
| 16 | +) |
| 17 | + |
| 18 | +// ELBOptions are the configuration parameters for the ELB provisioner. |
| 19 | +type ELBOptions struct { |
| 20 | + Region string |
| 21 | + Retries int |
| 22 | +} |
| 23 | + |
| 24 | +type elbPlugin struct { |
| 25 | + client elbiface.ELBAPI |
| 26 | + name string |
| 27 | +} |
| 28 | + |
| 29 | +// NewELBPlugin creates an AWS-based ELB provisioner. |
| 30 | +func NewELBPlugin(client elbiface.ELBAPI, name string) (loadbalancer.L4, error) { |
| 31 | + return &elbPlugin{ |
| 32 | + client: client, |
| 33 | + name: name, |
| 34 | + }, nil |
| 35 | +} |
| 36 | + |
| 37 | +// Credentials allocates a credential object that has the access key and secret id. |
| 38 | +func Credentials(cred *Credential) *credentials.Credentials { |
| 39 | + staticCred := new(Credential) |
| 40 | + if cred != nil { |
| 41 | + staticCred = cred |
| 42 | + } |
| 43 | + |
| 44 | + return credentials.NewChainCredentials([]credentials.Provider{ |
| 45 | + &ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(session.New())}, |
| 46 | + &credentials.EnvProvider{}, |
| 47 | + &credentials.SharedCredentialsProvider{}, |
| 48 | + staticCred, |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +// CreateELBClient creates an AWS ELB API client. |
| 53 | +func CreateELBClient(awsCredentials *credentials.Credentials, opt ELBOptions) elbiface.ELBAPI { |
| 54 | + region := opt.Region |
| 55 | + if region == "" { |
| 56 | + region, _ = GetRegion() |
| 57 | + } |
| 58 | + |
| 59 | + log.Infoln("ELB Client in region", region) |
| 60 | + |
| 61 | + return elb.New(session.New(aws.NewConfig(). |
| 62 | + WithRegion(region). |
| 63 | + WithCredentials(awsCredentials). |
| 64 | + WithLogger(getLogger()). |
| 65 | + WithLogLevel(aws.LogDebugWithHTTPBody). |
| 66 | + WithMaxRetries(opt.Retries))) |
| 67 | +} |
| 68 | + |
| 69 | +func (p *elbPlugin) Name() string { |
| 70 | + return p.name |
| 71 | +} |
| 72 | + |
| 73 | +// Routes lists all registered routes. |
| 74 | +func (p *elbPlugin) Routes() ([]loadbalancer.Route, error) { |
| 75 | + output, err := p.client.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{ |
| 76 | + LoadBalancerNames: []*string{aws.String(p.name)}, |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + routes := []loadbalancer.Route{} |
| 83 | + |
| 84 | + if len(output.LoadBalancerDescriptions) > 0 && output.LoadBalancerDescriptions[0].ListenerDescriptions != nil { |
| 85 | + for _, listener := range output.LoadBalancerDescriptions[0].ListenerDescriptions { |
| 86 | + routes = append(routes, loadbalancer.Route{ |
| 87 | + Port: int(*listener.Listener.InstancePort), |
| 88 | + Protocol: loadbalancer.ProtocolFromString(*listener.Listener.Protocol), |
| 89 | + LoadBalancerPort: int(*listener.Listener.LoadBalancerPort), |
| 90 | + Certificate: listener.Listener.SSLCertificateId, |
| 91 | + }) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return routes, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (p *elbPlugin) RegisterBackends(ids []instance.ID) (loadbalancer.Result, error) { |
| 99 | + |
| 100 | + addInstances := []*elb.Instance{} |
| 101 | + for _, instanceID := range ids { |
| 102 | + addInstance := &elb.Instance{} |
| 103 | + strID := string(instanceID) |
| 104 | + addInstance.InstanceId = &strID |
| 105 | + addInstances = append(addInstances, addInstance) |
| 106 | + } |
| 107 | + |
| 108 | + return p.client.RegisterInstancesWithLoadBalancer(&elb.RegisterInstancesWithLoadBalancerInput{ |
| 109 | + Instances: addInstances, |
| 110 | + LoadBalancerName: aws.String(p.name), |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +func (p *elbPlugin) DeregisterBackends(ids []instance.ID) (loadbalancer.Result, error) { |
| 115 | + |
| 116 | + rmInstances := []*elb.Instance{} |
| 117 | + for _, instanceID := range ids { |
| 118 | + rmInstance := &elb.Instance{} |
| 119 | + strID := string(instanceID) |
| 120 | + rmInstance.InstanceId = &strID |
| 121 | + rmInstances = append(rmInstances, rmInstance) |
| 122 | + } |
| 123 | + |
| 124 | + return p.client.DeregisterInstancesFromLoadBalancer(&elb.DeregisterInstancesFromLoadBalancerInput{ |
| 125 | + Instances: rmInstances, |
| 126 | + LoadBalancerName: aws.String(p.name), |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +func (p *elbPlugin) Backends() ([]instance.ID, error) { |
| 131 | + output, err := p.client.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{ |
| 132 | + LoadBalancerNames: []*string{ |
| 133 | + aws.String(p.name), |
| 134 | + }, |
| 135 | + }) |
| 136 | + if err != nil { |
| 137 | + return []instance.ID{}, err |
| 138 | + } |
| 139 | + |
| 140 | + instanceIDs := []instance.ID{} |
| 141 | + |
| 142 | + if len(output.LoadBalancerDescriptions) == 0 { |
| 143 | + return instanceIDs, nil |
| 144 | + } |
| 145 | + |
| 146 | + for _, loadBalancerDescription := range output.LoadBalancerDescriptions { |
| 147 | + for _, lbInstance := range loadBalancerDescription.Instances { |
| 148 | + instanceIDs = append(instanceIDs, instance.ID(*lbInstance.InstanceId)) |
| 149 | + } |
| 150 | + } |
| 151 | + return instanceIDs, nil |
| 152 | +} |
| 153 | + |
| 154 | +func (p *elbPlugin) Publish(route loadbalancer.Route) (loadbalancer.Result, error) { |
| 155 | + |
| 156 | + if route.Protocol == loadbalancer.Invalid { |
| 157 | + return nil, fmt.Errorf("Bad protocol") |
| 158 | + } |
| 159 | + instanceProtocol := aws.String(string(route.Protocol)) |
| 160 | + if route.Protocol == loadbalancer.SSL { |
| 161 | + // SSL needs to point to TCP internally |
| 162 | + instanceProtocol = aws.String(string(loadbalancer.TCP)) |
| 163 | + } else if route.Protocol == loadbalancer.HTTPS { |
| 164 | + // HTTPS has to point to HTTP internally |
| 165 | + instanceProtocol = aws.String(string(loadbalancer.HTTP)) |
| 166 | + } |
| 167 | + |
| 168 | + listener := &elb.Listener{ |
| 169 | + InstancePort: aws.Int64(int64(route.Port)), |
| 170 | + LoadBalancerPort: aws.Int64(int64(route.LoadBalancerPort)), |
| 171 | + Protocol: aws.String(string(route.Protocol)), |
| 172 | + InstanceProtocol: instanceProtocol, |
| 173 | + SSLCertificateId: route.Certificate, |
| 174 | + } |
| 175 | + |
| 176 | + return p.client.CreateLoadBalancerListeners(&elb.CreateLoadBalancerListenersInput{ |
| 177 | + Listeners: []*elb.Listener{listener}, |
| 178 | + LoadBalancerName: aws.String(p.name), |
| 179 | + }) |
| 180 | +} |
| 181 | + |
| 182 | +func (p *elbPlugin) Unpublish(extPort int) (loadbalancer.Result, error) { |
| 183 | + return p.client.DeleteLoadBalancerListeners(&elb.DeleteLoadBalancerListenersInput{ |
| 184 | + LoadBalancerPorts: []*int64{aws.Int64(int64(extPort))}, |
| 185 | + LoadBalancerName: aws.String(p.name), |
| 186 | + }) |
| 187 | +} |
| 188 | + |
| 189 | +func (p *elbPlugin) ConfigureHealthCheck(hc loadbalancer.HealthCheck) (loadbalancer.Result, error) { |
| 190 | + |
| 191 | + return p.client.ConfigureHealthCheck(&elb.ConfigureHealthCheckInput{ |
| 192 | + HealthCheck: &elb.HealthCheck{ |
| 193 | + HealthyThreshold: aws.Int64(int64(hc.Healthy)), |
| 194 | + Interval: aws.Int64(int64(hc.Interval.Seconds())), |
| 195 | + Target: aws.String(fmt.Sprintf("TCP:%d", hc.BackendPort)), |
| 196 | + Timeout: aws.Int64(int64(hc.Timeout.Seconds())), |
| 197 | + UnhealthyThreshold: aws.Int64(int64(hc.Unhealthy)), |
| 198 | + }, |
| 199 | + LoadBalancerName: aws.String(p.name), |
| 200 | + }) |
| 201 | +} |
0 commit comments