2017-12-02 18:30:16 +00:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-08-27 14:32:05 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2018-01-10 17:28:03 +00:00
|
|
|
"fmt"
|
2018-06-13 08:08:03 +00:00
|
|
|
"net"
|
2017-12-02 18:30:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/ty/fun"
|
2018-04-11 10:26:03 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/containous/traefik/log"
|
2018-01-10 17:28:03 +00:00
|
|
|
"github.com/containous/traefik/provider"
|
2017-12-02 18:30:16 +00:00
|
|
|
"github.com/containous/traefik/provider/label"
|
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// buildConfiguration fills the config template with the given instances
|
2018-07-23 09:56:02 +00:00
|
|
|
func (p *Provider) buildConfiguration(instances []ecsInstance) (*types.Configuration, error) {
|
2017-12-02 18:30:16 +00:00
|
|
|
var ecsFuncMap = template.FuncMap{
|
2017-12-20 21:14:51 +00:00
|
|
|
// Backend functions
|
2018-01-10 17:28:03 +00:00
|
|
|
"getHost": getHost,
|
|
|
|
"getPort": getPort,
|
2018-03-28 00:13:48 +00:00
|
|
|
"getCircuitBreaker": label.GetCircuitBreaker,
|
|
|
|
"getLoadBalancer": label.GetLoadBalancer,
|
|
|
|
"getMaxConn": label.GetMaxConn,
|
|
|
|
"getHealthCheck": label.GetHealthCheck,
|
|
|
|
"getBuffering": label.GetBuffering,
|
2018-01-10 17:28:03 +00:00
|
|
|
"getServers": getServers,
|
|
|
|
|
2017-12-20 21:14:51 +00:00
|
|
|
// Frontend functions
|
2018-03-23 16:40:04 +00:00
|
|
|
"filterFrontends": filterFrontends,
|
|
|
|
"getFrontendRule": p.getFrontendRule,
|
2018-08-27 14:32:05 +00:00
|
|
|
"getFrontendName": p.getFrontendName,
|
2018-04-11 14:30:04 +00:00
|
|
|
"getPassHostHeader": label.GetFuncBool(label.TraefikFrontendPassHostHeader, label.DefaultPassHostHeader),
|
2018-03-28 00:13:48 +00:00
|
|
|
"getPassTLSCert": label.GetFuncBool(label.TraefikFrontendPassTLSCert, label.DefaultPassTLSCert),
|
2018-04-11 14:30:04 +00:00
|
|
|
"getPriority": label.GetFuncInt(label.TraefikFrontendPriority, label.DefaultFrontendPriority),
|
2018-07-06 14:52:04 +00:00
|
|
|
"getBasicAuth": label.GetFuncSliceString(label.TraefikFrontendAuthBasic), // Deprecated
|
|
|
|
"getAuth": label.GetAuth,
|
2018-03-28 00:13:48 +00:00
|
|
|
"getEntryPoints": label.GetFuncSliceString(label.TraefikFrontendEntryPoints),
|
|
|
|
"getRedirect": label.GetRedirect,
|
|
|
|
"getErrorPages": label.GetErrorPages,
|
|
|
|
"getRateLimit": label.GetRateLimit,
|
|
|
|
"getHeaders": label.GetHeaders,
|
|
|
|
"getWhiteList": label.GetWhiteList,
|
2017-12-02 18:30:16 +00:00
|
|
|
}
|
2018-03-28 00:13:48 +00:00
|
|
|
|
2018-08-27 14:32:05 +00:00
|
|
|
services := make(map[string][]ecsInstance)
|
|
|
|
for _, instance := range instances {
|
|
|
|
segmentProperties := label.ExtractTraefikLabels(instance.TraefikLabels)
|
|
|
|
|
|
|
|
for segmentName, labels := range segmentProperties {
|
|
|
|
instance.SegmentLabels = labels
|
|
|
|
instance.SegmentName = segmentName
|
|
|
|
|
|
|
|
backendName := getBackendName(instance)
|
|
|
|
if p.filterInstance(instance) {
|
|
|
|
if serviceInstances, ok := services[backendName]; ok {
|
|
|
|
services[backendName] = append(serviceInstances, instance)
|
|
|
|
} else {
|
|
|
|
services[backendName] = []ecsInstance{instance}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:30:16 +00:00
|
|
|
return p.GetConfiguration("templates/ecs.tmpl", ecsFuncMap, struct {
|
|
|
|
Services map[string][]ecsInstance
|
|
|
|
}{
|
2018-01-10 17:28:03 +00:00
|
|
|
Services: services,
|
2017-12-02 18:30:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-11 10:26:03 +00:00
|
|
|
func (p *Provider) filterInstance(i ecsInstance) bool {
|
2018-05-28 16:52:03 +00:00
|
|
|
if i.machine == nil {
|
|
|
|
log.Debug("Filtering ecs instance with nil machine")
|
2018-04-11 10:26:03 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-04 13:08:03 +00:00
|
|
|
if labelPort := label.GetStringValue(i.TraefikLabels, label.TraefikPort, ""); len(i.machine.ports) == 0 && labelPort == "" {
|
2018-05-28 16:52:03 +00:00
|
|
|
log.Debugf("Filtering ecs instance without port %s (%s)", i.Name, i.ID)
|
2018-04-11 10:26:03 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-05-28 16:52:03 +00:00
|
|
|
if strings.ToLower(i.machine.state) != ec2.InstanceStateNameRunning {
|
|
|
|
log.Debugf("Filtering ecs instance with an incorrect state %s (%s) (state = %s)", i.Name, i.ID, i.machine.state)
|
2018-04-11 10:26:03 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-05-28 16:52:03 +00:00
|
|
|
if len(i.machine.privateIP) == 0 {
|
2018-04-11 10:26:03 +00:00
|
|
|
log.Debugf("Filtering ecs instance without an ip address %s (%s)", i.Name, i.ID)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isEnabled(i, p.ExposedByDefault) {
|
|
|
|
log.Debugf("Filtering disabled ecs instance %s (%s)", i.Name, i.ID)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-06-29 22:14:03 +00:00
|
|
|
constraintTags := label.GetSliceStringValue(i.TraefikLabels, label.TraefikTags)
|
|
|
|
if ok, failingConstraint := p.MatchConstraints(constraintTags); !ok {
|
|
|
|
if failingConstraint != nil {
|
|
|
|
log.Debugf("Filtering ecs instance pruned by constraint %s (%s) (constraint = %q)", i.Name, i.ID, failingConstraint.String())
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-11 10:26:03 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:22:03 +00:00
|
|
|
func getBackendName(i ecsInstance) string {
|
2018-08-27 14:32:05 +00:00
|
|
|
if len(i.SegmentName) > 0 {
|
|
|
|
return getSegmentBackendName(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getDefaultBackendName(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSegmentBackendName(i ecsInstance) string {
|
|
|
|
if value := label.GetStringValue(i.SegmentLabels, label.TraefikBackend, ""); len(value) > 0 {
|
|
|
|
return provider.Normalize(i.Name + "-" + value)
|
2018-07-04 12:22:03 +00:00
|
|
|
}
|
2018-08-27 14:32:05 +00:00
|
|
|
|
|
|
|
return provider.Normalize(i.Name + "-" + i.SegmentName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDefaultBackendName(i ecsInstance) string {
|
|
|
|
if value := label.GetStringValue(i.SegmentLabels, label.TraefikBackend, ""); len(value) != 0 {
|
|
|
|
return provider.Normalize(value)
|
2018-07-04 12:22:03 +00:00
|
|
|
}
|
2018-08-27 14:32:05 +00:00
|
|
|
|
|
|
|
return provider.Normalize(i.Name)
|
2018-07-04 12:22:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-02 18:30:16 +00:00
|
|
|
func (p *Provider) getFrontendRule(i ecsInstance) string {
|
2018-08-27 14:32:05 +00:00
|
|
|
if value := label.GetStringValue(i.SegmentLabels, label.TraefikFrontendRule, ""); len(value) != 0 {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := label.GetStringValue(i.SegmentLabels, label.TraefikDomain, p.Domain)
|
2018-04-17 18:58:24 +00:00
|
|
|
defaultRule := "Host:" + strings.ToLower(strings.Replace(i.Name, "_", "-", -1)) + "." + domain
|
|
|
|
|
2018-03-28 00:13:48 +00:00
|
|
|
return label.GetStringValue(i.TraefikLabels, label.TraefikFrontendRule, defaultRule)
|
2017-12-02 18:30:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 14:32:05 +00:00
|
|
|
func (p *Provider) getFrontendName(instance ecsInstance) string {
|
|
|
|
name := getBackendName(instance)
|
|
|
|
if len(instance.SegmentName) > 0 {
|
|
|
|
name = instance.SegmentName + "-" + name
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider.Normalize(name)
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:30:16 +00:00
|
|
|
func getHost(i ecsInstance) string {
|
2018-05-28 16:52:03 +00:00
|
|
|
return i.machine.privateIP
|
2017-12-02 18:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getPort(i ecsInstance) string {
|
2018-08-27 14:32:05 +00:00
|
|
|
value := label.GetStringValue(i.SegmentLabels, label.TraefikPort, "")
|
|
|
|
|
|
|
|
if len(value) == 0 {
|
|
|
|
value = label.GetStringValue(i.TraefikLabels, label.TraefikPort, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 0 {
|
2018-07-04 13:08:03 +00:00
|
|
|
port, err := strconv.ParseInt(value, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
for _, mapping := range i.machine.ports {
|
|
|
|
if port == mapping.hostPort || port == mapping.containerPort {
|
|
|
|
return strconv.FormatInt(mapping.hostPort, 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
2017-12-02 18:30:16 +00:00
|
|
|
}
|
2018-07-04 13:08:03 +00:00
|
|
|
return strconv.FormatInt(i.machine.ports[0].hostPort, 10)
|
2017-12-02 18:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func filterFrontends(instances []ecsInstance) []ecsInstance {
|
|
|
|
byName := make(map[string]struct{})
|
|
|
|
|
|
|
|
return fun.Filter(func(i ecsInstance) bool {
|
2018-07-04 12:22:03 +00:00
|
|
|
backendName := getBackendName(i)
|
2018-08-27 14:32:05 +00:00
|
|
|
if len(i.SegmentName) > 0 {
|
|
|
|
backendName = backendName + "-" + i.SegmentName
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:22:03 +00:00
|
|
|
_, found := byName[backendName]
|
2017-12-02 18:30:16 +00:00
|
|
|
if !found {
|
2018-07-04 12:22:03 +00:00
|
|
|
byName[backendName] = struct{}{}
|
2017-12-02 18:30:16 +00:00
|
|
|
}
|
|
|
|
return !found
|
|
|
|
}, instances).([]ecsInstance)
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:28:03 +00:00
|
|
|
func getServers(instances []ecsInstance) map[string]types.Server {
|
|
|
|
var servers map[string]types.Server
|
|
|
|
|
|
|
|
for _, instance := range instances {
|
|
|
|
if servers == nil {
|
|
|
|
servers = make(map[string]types.Server)
|
|
|
|
}
|
|
|
|
|
2018-08-27 14:32:05 +00:00
|
|
|
protocol := label.GetStringValue(instance.SegmentLabels, label.TraefikProtocol, label.DefaultProtocol)
|
2018-01-10 17:28:03 +00:00
|
|
|
host := getHost(instance)
|
|
|
|
port := getPort(instance)
|
|
|
|
|
2018-08-27 14:32:05 +00:00
|
|
|
serverURL := fmt.Sprintf("%s://%s", protocol, net.JoinHostPort(host, port))
|
|
|
|
serverName := getServerName(instance, serverURL)
|
|
|
|
|
|
|
|
if _, exist := servers[serverName]; exist {
|
|
|
|
log.Debugf("Skipping server %q with the same URL.", serverName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:28:03 +00:00
|
|
|
servers[serverName] = types.Server{
|
2018-08-27 14:32:05 +00:00
|
|
|
URL: serverURL,
|
|
|
|
Weight: label.GetIntValue(instance.SegmentLabels, label.TraefikWeight, label.DefaultWeight),
|
2018-01-10 17:28:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return servers
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:30:16 +00:00
|
|
|
func isEnabled(i ecsInstance, exposedByDefault bool) bool {
|
2018-03-28 00:13:48 +00:00
|
|
|
return label.GetBoolValue(i.TraefikLabels, label.TraefikEnable, exposedByDefault)
|
2017-12-02 18:30:16 +00:00
|
|
|
}
|
2018-08-27 14:32:05 +00:00
|
|
|
|
|
|
|
func getServerName(instance ecsInstance, url string) string {
|
|
|
|
hash := md5.New()
|
|
|
|
_, err := hash.Write([]byte(url))
|
|
|
|
if err != nil {
|
|
|
|
// Impossible case
|
|
|
|
log.Errorf("Fail to hash server URL %q", url)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(instance.SegmentName) > 0 {
|
|
|
|
return provider.Normalize(fmt.Sprintf("server-%s-%s-%s", instance.Name, instance.ID, hex.EncodeToString(hash.Sum(nil))))
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider.Normalize(fmt.Sprintf("server-%s-%s", instance.Name, instance.ID))
|
|
|
|
}
|