traefik/provider/mesos/config.go

239 lines
7.4 KiB
Go
Raw Normal View History

package mesos
import (
"fmt"
"math"
2018-06-13 08:08:03 +00:00
"net"
"strconv"
"strings"
"text/template"
"github.com/containous/traefik/log"
"github.com/containous/traefik/provider"
"github.com/containous/traefik/provider/label"
"github.com/containous/traefik/types"
"github.com/mesosphere/mesos-dns/records/state"
)
2018-03-28 22:01:24 +00:00
type taskData struct {
state.Task
TraefikLabels map[string]string
}
func (p *Provider) buildConfigurationV2(tasks []state.Task) *types.Configuration {
var mesosFuncMap = template.FuncMap{
2018-03-28 22:01:24 +00:00
"getDomain": label.GetFuncString(label.TraefikDomain, p.Domain),
2018-01-10 03:05:33 +00:00
"getID": getID,
// Backend functions
2018-01-10 20:54:46 +00:00
"getBackendName": getBackendName,
2018-03-28 22:01:24 +00:00
"getCircuitBreaker": label.GetCircuitBreaker,
"getLoadBalancer": label.GetLoadBalancer,
"getMaxConn": label.GetMaxConn,
"getHealthCheck": label.GetHealthCheck,
"getBuffering": label.GetBuffering,
2018-01-10 20:54:46 +00:00
"getServers": p.getServers,
"getHost": p.getHost,
"getServerPort": p.getServerPort,
2018-01-10 03:05:33 +00:00
// Frontend functions
"getFrontEndName": getFrontendName,
2018-03-28 22:01:24 +00:00
"getEntryPoints": label.GetFuncSliceString(label.TraefikFrontendEntryPoints),
"getBasicAuth": label.GetFuncSliceString(label.TraefikFrontendAuthBasic),
2018-04-11 14:30:04 +00:00
"getPriority": label.GetFuncInt(label.TraefikFrontendPriority, label.DefaultFrontendPriority),
"getPassHostHeader": label.GetFuncBool(label.TraefikFrontendPassHostHeader, label.DefaultPassHostHeader),
2018-03-28 22:01:24 +00:00
"getPassTLSCert": label.GetFuncBool(label.TraefikFrontendPassTLSCert, label.DefaultPassTLSCert),
"getFrontendRule": p.getFrontendRule,
2018-03-28 22:01:24 +00:00
"getRedirect": label.GetRedirect,
"getErrorPages": label.GetErrorPages,
"getRateLimit": label.GetRateLimit,
"getHeaders": label.GetHeaders,
"getWhiteList": label.GetWhiteList,
}
// filter tasks
2018-03-28 22:01:24 +00:00
appsTasks := make(map[string][]taskData)
for _, task := range tasks {
data := taskData{
Task: task,
TraefikLabels: extractLabels(task),
2018-01-10 03:05:33 +00:00
}
2018-03-28 22:01:24 +00:00
if taskFilter(data, p.ExposedByDefault) {
if _, ok := appsTasks[task.DiscoveryInfo.Name]; !ok {
appsTasks[task.DiscoveryInfo.Name] = []taskData{data}
} else {
appsTasks[task.DiscoveryInfo.Name] = append(appsTasks[task.DiscoveryInfo.Name], data)
}
2018-01-10 20:54:46 +00:00
}
}
templateObjects := struct {
2018-03-28 22:01:24 +00:00
ApplicationsTasks map[string][]taskData
2018-01-10 20:54:46 +00:00
Domain string
}{
2018-01-10 20:54:46 +00:00
ApplicationsTasks: appsTasks,
Domain: p.Domain,
}
configuration, err := p.GetConfiguration("templates/mesos.tmpl", mesosFuncMap, templateObjects)
if err != nil {
log.Error(err)
}
2018-03-28 22:01:24 +00:00
return configuration
}
2018-03-28 22:01:24 +00:00
func taskFilter(task taskData, exposedByDefaultFlag bool) bool {
if len(task.DiscoveryInfo.Ports.DiscoveryPorts) == 0 {
log.Debugf("Filtering Mesos task without port %s", task.Name)
return false
}
2018-01-10 03:05:33 +00:00
if !isEnabled(task, exposedByDefaultFlag) {
log.Debugf("Filtering disabled Mesos task %s", task.DiscoveryInfo.Name)
return false
}
// filter indeterminable task port
2018-03-28 22:01:24 +00:00
portIndexLabel := label.GetStringValue(task.TraefikLabels, label.TraefikPortIndex, "")
portValueLabel := label.GetStringValue(task.TraefikLabels, label.TraefikPort, "")
if portIndexLabel != "" && portValueLabel != "" {
log.Debugf("Filtering Mesos task %s specifying both %q' and %q labels", task.Name, label.TraefikPortIndex, label.TraefikPort)
return false
}
if portIndexLabel != "" {
index, err := strconv.Atoi(portIndexLabel)
if err != nil || index < 0 || index > len(task.DiscoveryInfo.Ports.DiscoveryPorts)-1 {
log.Debugf("Filtering Mesos task %s with unexpected value for %q label", task.Name, label.TraefikPortIndex)
return false
}
}
if portValueLabel != "" {
port, err := strconv.Atoi(portValueLabel)
if err != nil {
log.Debugf("Filtering Mesos task %s with unexpected value for %q label", task.Name, label.TraefikPort)
return false
}
var foundPort bool
for _, exposedPort := range task.DiscoveryInfo.Ports.DiscoveryPorts {
if port == exposedPort.Number {
foundPort = true
break
}
}
if !foundPort {
log.Debugf("Filtering Mesos task %s without a matching port for %q label", task.Name, label.TraefikPort)
return false
}
}
2018-01-10 03:05:33 +00:00
// filter healthChecks
if task.Statuses != nil && len(task.Statuses) > 0 && task.Statuses[0].Healthy != nil && !*task.Statuses[0].Healthy {
log.Debugf("Filtering Mesos task %s with bad healthCheck", task.DiscoveryInfo.Name)
return false
}
return true
}
2018-03-28 22:01:24 +00:00
func getID(task taskData) string {
return provider.Normalize(task.ID)
}
2018-03-28 22:01:24 +00:00
func getBackendName(task taskData) string {
return label.GetStringValue(task.TraefikLabels, label.TraefikBackend, provider.Normalize(task.DiscoveryInfo.Name))
}
2018-03-28 22:01:24 +00:00
func getFrontendName(task taskData) string {
2018-01-10 20:54:46 +00:00
// TODO task.ID -> task.Name + task.ID
return provider.Normalize(task.ID)
}
func (p *Provider) getSubDomain(name string) string {
if p.GroupsAsSubDomains {
splitedName := strings.Split(strings.TrimPrefix(name, "/"), "/")
provider.ReverseStringSlice(&splitedName)
reverseName := strings.Join(splitedName, ".")
return reverseName
}
return strings.Replace(strings.TrimPrefix(name, "/"), "/", "-", -1)
}
2018-03-28 22:01:24 +00:00
// getFrontendRule returns the frontend rule for the specified application, using it's label.
// It returns a default one (Host) if the label is not present.
func (p *Provider) getFrontendRule(task taskData) string {
if v := label.GetStringValue(task.TraefikLabels, label.TraefikFrontendRule, ""); len(v) > 0 {
return v
2018-01-31 14:32:04 +00:00
}
domain := label.GetStringValue(task.TraefikLabels, label.TraefikDomain, p.Domain)
return "Host:" + strings.ToLower(strings.Replace(p.getSubDomain(task.DiscoveryInfo.Name), "_", "-", -1)) + "." + domain
2018-01-31 14:32:04 +00:00
}
2018-03-28 22:01:24 +00:00
func (p *Provider) getServers(tasks []taskData) map[string]types.Server {
2018-01-10 20:54:46 +00:00
var servers map[string]types.Server
for _, task := range tasks {
if servers == nil {
servers = make(map[string]types.Server)
}
2018-03-28 22:01:24 +00:00
protocol := label.GetStringValue(task.TraefikLabels, label.TraefikProtocol, label.DefaultProtocol)
2018-01-10 20:54:46 +00:00
host := p.getHost(task)
port := p.getServerPort(task)
serverName := "server-" + getID(task)
servers[serverName] = types.Server{
2018-06-13 08:08:03 +00:00
URL: fmt.Sprintf("%s://%s", protocol, net.JoinHostPort(host, port)),
2018-04-11 14:30:04 +00:00
Weight: getIntValue(task.TraefikLabels, label.TraefikWeight, label.DefaultWeight, math.MaxInt32),
2018-01-10 20:54:46 +00:00
}
}
return servers
}
2018-03-28 22:01:24 +00:00
func (p *Provider) getHost(task taskData) string {
return task.IP(strings.Split(p.IPSources, ",")...)
2018-01-10 20:54:46 +00:00
}
2018-03-28 22:01:24 +00:00
func (p *Provider) getServerPort(task taskData) string {
plv := getIntValue(task.TraefikLabels, label.TraefikPortIndex, math.MinInt32, len(task.DiscoveryInfo.Ports.DiscoveryPorts)-1)
if plv >= 0 {
return strconv.Itoa(task.DiscoveryInfo.Ports.DiscoveryPorts[plv].Number)
2018-01-10 20:54:46 +00:00
}
2018-03-28 22:01:24 +00:00
if pv := label.GetStringValue(task.TraefikLabels, label.TraefikPort, ""); len(pv) > 0 {
return pv
2018-01-10 20:54:46 +00:00
}
2018-03-28 22:01:24 +00:00
for _, port := range task.DiscoveryInfo.Ports.DiscoveryPorts {
return strconv.Itoa(port.Number)
2018-01-10 20:54:46 +00:00
}
2018-03-28 22:01:24 +00:00
return ""
2018-01-10 20:54:46 +00:00
}
2018-03-28 22:01:24 +00:00
func isEnabled(task taskData, exposedByDefault bool) bool {
return label.GetBoolValue(task.TraefikLabels, label.TraefikEnable, exposedByDefault)
2018-01-10 20:54:46 +00:00
}
// Label functions
2018-03-28 22:01:24 +00:00
func getIntValue(labels map[string]string, labelName string, defaultValue int, maxValue int) int {
value := label.GetIntValue(labels, labelName, defaultValue)
if value <= maxValue {
return value
2018-01-10 20:54:46 +00:00
}
2018-03-28 22:01:24 +00:00
log.Warnf("The value %q for %q exceed the max authorized value %q, falling back to %v.", value, labelName, maxValue, defaultValue)
2018-01-10 20:54:46 +00:00
return defaultValue
}
2018-03-28 22:01:24 +00:00
func extractLabels(task state.Task) map[string]string {
2018-01-10 20:54:46 +00:00
labels := make(map[string]string)
for _, lbl := range task.Labels {
labels[lbl.Key] = lbl.Value
}
return labels
}