traefik/old/provider/ecs/config.go

253 lines
7.5 KiB
Go
Raw Normal View History

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"
"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"
2018-11-14 09:18:03 +00:00
"github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/provider"
"github.com/containous/traefik/old/provider/label"
"github.com/containous/traefik/old/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) {
var ecsFuncMap = template.FuncMap{
// Backend functions
2018-10-29 17:42:03 +00:00
"getHost": getHost,
"getPort": getPort,
"getCircuitBreaker": label.GetCircuitBreaker,
"getLoadBalancer": label.GetLoadBalancer,
"getMaxConn": label.GetMaxConn,
"getHealthCheck": label.GetHealthCheck,
"getBuffering": label.GetBuffering,
"getResponseForwarding": label.GetResponseForwarding,
"getServers": getServers,
2018-01-10 17:28:03 +00:00
// Frontend functions
2018-08-29 09:36:03 +00:00
"filterFrontends": filterFrontends,
"getFrontendRule": p.getFrontendRule,
"getFrontendName": p.getFrontendName,
"getPassHostHeader": label.GetFuncBool(label.TraefikFrontendPassHostHeader, label.DefaultPassHostHeader),
"getPassTLSCert": label.GetFuncBool(label.TraefikFrontendPassTLSCert, label.DefaultPassTLSCert),
"getPassTLSClientCert": label.GetTLSClientCert,
"getPriority": label.GetFuncInt(label.TraefikFrontendPriority, label.DefaultFrontendPriority),
"getBasicAuth": label.GetFuncSliceString(label.TraefikFrontendAuthBasic), // Deprecated
"getAuth": label.GetAuth,
"getEntryPoints": label.GetFuncSliceString(label.TraefikFrontendEntryPoints),
"getRedirect": label.GetRedirect,
"getErrorPages": label.GetErrorPages,
"getRateLimit": label.GetRateLimit,
"getHeaders": label.GetHeaders,
"getWhiteList": label.GetWhiteList,
}
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}
}
}
}
}
return p.GetConfiguration("templates/ecs.tmpl", ecsFuncMap, struct {
Services map[string][]ecsInstance
}{
2018-01-10 17:28:03 +00:00
Services: services,
})
}
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
}
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)
if len(domain) > 0 {
domain = "." + domain
}
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)
}
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)
}
func getHost(i ecsInstance) string {
2018-05-28 16:52:03 +00:00
return i.machine.privateIP
}
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
}
}
2018-07-04 13:08:03 +00:00
return strconv.FormatInt(i.machine.ports[0].hostPort, 10)
}
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]
if !found {
2018-07-04 12:22:03 +00:00
byName[backendName] = 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-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
}
func isEnabled(i ecsInstance, exposedByDefault bool) bool {
2018-03-28 00:13:48 +00:00
return label.GetBoolValue(i.TraefikLabels, label.TraefikEnable, exposedByDefault)
}
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))
}