2017-12-02 18:30:16 +00:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-10 17:28:03 +00:00
|
|
|
"fmt"
|
2017-12-02 18:30:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/ty/fun"
|
2018-01-10 17:28:03 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"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-03-28 00:13:48 +00:00
|
|
|
func (p *Provider) buildConfigurationV2(services map[string][]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-03-28 00:13:48 +00:00
|
|
|
"getPassHostHeader": label.GetFuncBool(label.TraefikFrontendPassHostHeader, label.DefaultPassHostHeaderBool),
|
|
|
|
"getPassTLSCert": label.GetFuncBool(label.TraefikFrontendPassTLSCert, label.DefaultPassTLSCert),
|
|
|
|
"getPriority": label.GetFuncInt(label.TraefikFrontendPriority, label.DefaultFrontendPriorityInt),
|
|
|
|
"getBasicAuth": label.GetFuncSliceString(label.TraefikFrontendAuthBasic),
|
|
|
|
"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
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) getFrontendRule(i ecsInstance) string {
|
|
|
|
defaultRule := "Host:" + strings.ToLower(strings.Replace(i.Name, "_", "-", -1)) + "." + p.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
|
|
|
}
|
|
|
|
|
|
|
|
func getHost(i ecsInstance) string {
|
2018-01-10 17:28:03 +00:00
|
|
|
return aws.StringValue(i.machine.PrivateIpAddress)
|
2017-12-02 18:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getPort(i ecsInstance) string {
|
2018-03-28 00:13:48 +00:00
|
|
|
if value := label.GetStringValue(i.TraefikLabels, label.TraefikPort, ""); len(value) > 0 {
|
2017-12-02 18:30:16 +00:00
|
|
|
return value
|
|
|
|
}
|
2018-01-10 17:28:03 +00:00
|
|
|
return strconv.FormatInt(aws.Int64Value(i.container.NetworkBindings[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 {
|
|
|
|
_, found := byName[i.Name]
|
|
|
|
if !found {
|
|
|
|
byName[i.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
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-03-28 00:13:48 +00:00
|
|
|
protocol := label.GetStringValue(instance.TraefikLabels, label.TraefikProtocol, label.DefaultProtocol)
|
2018-01-10 17:28:03 +00:00
|
|
|
host := getHost(instance)
|
|
|
|
port := getPort(instance)
|
|
|
|
|
|
|
|
serverName := provider.Normalize(fmt.Sprintf("server-%s-%s", instance.Name, instance.ID))
|
|
|
|
servers[serverName] = types.Server{
|
|
|
|
URL: fmt.Sprintf("%s://%s:%s", protocol, host, port),
|
2018-03-28 00:13:48 +00:00
|
|
|
Weight: label.GetIntValue(instance.TraefikLabels, label.TraefikWeight, 0),
|
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
|
|
|
}
|