2017-04-17 10:50:02 +00:00
|
|
|
package mesos
|
2016-07-20 09:56:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-12-30 08:21:13 +00:00
|
|
|
"fmt"
|
2016-07-20 09:56:14 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2016-11-28 13:59:08 +00:00
|
|
|
"time"
|
|
|
|
|
2016-07-20 09:56:14 +00:00
|
|
|
"github.com/BurntSushi/ty/fun"
|
2016-09-19 17:08:39 +00:00
|
|
|
"github.com/cenk/backoff"
|
|
|
|
"github.com/containous/traefik/job"
|
2016-09-23 16:27:01 +00:00
|
|
|
"github.com/containous/traefik/log"
|
2017-04-17 10:50:02 +00:00
|
|
|
"github.com/containous/traefik/provider"
|
2016-07-20 09:56:14 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
"github.com/mesos/mesos-go/detector"
|
2016-12-30 08:21:13 +00:00
|
|
|
// Register mesos zoo the detector
|
|
|
|
_ "github.com/mesos/mesos-go/detector/zoo"
|
2016-07-20 09:56:14 +00:00
|
|
|
"github.com/mesosphere/mesos-dns/detect"
|
|
|
|
"github.com/mesosphere/mesos-dns/logging"
|
|
|
|
"github.com/mesosphere/mesos-dns/records"
|
|
|
|
"github.com/mesosphere/mesos-dns/records/state"
|
|
|
|
"github.com/mesosphere/mesos-dns/util"
|
|
|
|
)
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
var _ provider.Provider = (*Provider)(nil)
|
2016-08-16 17:13:18 +00:00
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
//Provider holds configuration of the provider.
|
|
|
|
type Provider struct {
|
|
|
|
provider.BaseProvider
|
2016-07-20 09:56:14 +00:00
|
|
|
Endpoint string `description:"Mesos server endpoint. You can also specify multiple endpoint for Mesos"`
|
|
|
|
Domain string `description:"Default domain used"`
|
2017-10-02 08:32:02 +00:00
|
|
|
ExposedByDefault bool `description:"Expose Mesos apps by default" export:"true"`
|
|
|
|
GroupsAsSubDomains bool `description:"Convert Mesos groups to subdomains" export:"true"`
|
|
|
|
ZkDetectionTimeout int `description:"Zookeeper timeout (in seconds)" export:"true"`
|
|
|
|
RefreshSeconds int `description:"Polling interval (in seconds)" export:"true"`
|
|
|
|
IPSources string `description:"IPSources (e.g. host, docker, mesos, rkt)" export:"true"`
|
|
|
|
StateTimeoutSecond int `description:"HTTP Timeout (in seconds)" export:"true"`
|
2016-07-20 09:56:14 +00:00
|
|
|
Masters []string
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provide allows the mesos provider to provide configurations to traefik
|
2016-07-20 09:56:14 +00:00
|
|
|
// using the given configuration channel.
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
|
2016-07-20 09:56:14 +00:00
|
|
|
operation := func() error {
|
|
|
|
|
|
|
|
// initialize logging
|
|
|
|
logging.SetupLogs()
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
log.Debugf("%s", p.IPSources)
|
2016-07-20 09:56:14 +00:00
|
|
|
|
|
|
|
var zk string
|
|
|
|
var masters []string
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
if strings.HasPrefix(p.Endpoint, "zk://") {
|
|
|
|
zk = p.Endpoint
|
2016-07-20 09:56:14 +00:00
|
|
|
} else {
|
2017-04-17 10:50:02 +00:00
|
|
|
masters = strings.Split(p.Endpoint, ",")
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errch := make(chan error)
|
|
|
|
|
|
|
|
changed := detectMasters(zk, masters)
|
2017-04-17 10:50:02 +00:00
|
|
|
reload := time.NewTicker(time.Second * time.Duration(p.RefreshSeconds))
|
|
|
|
zkTimeout := time.Second * time.Duration(p.ZkDetectionTimeout)
|
2016-07-20 09:56:14 +00:00
|
|
|
timeout := time.AfterFunc(zkTimeout, func() {
|
|
|
|
if zkTimeout > 0 {
|
|
|
|
errch <- fmt.Errorf("master detection timed out after %s", zkTimeout)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
defer reload.Stop()
|
|
|
|
defer util.HandleCrash()
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
if !p.Watch {
|
2016-07-20 09:56:14 +00:00
|
|
|
reload.Stop()
|
|
|
|
timeout.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-reload.C:
|
2017-04-17 10:50:02 +00:00
|
|
|
configuration := p.loadMesosConfig()
|
2016-07-20 09:56:14 +00:00
|
|
|
if configuration != nil {
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "mesos",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case masters := <-changed:
|
|
|
|
if len(masters) == 0 || masters[0] == "" {
|
|
|
|
// no leader
|
|
|
|
timeout.Reset(zkTimeout)
|
|
|
|
} else {
|
|
|
|
timeout.Stop()
|
|
|
|
}
|
|
|
|
log.Debugf("new masters detected: %v", masters)
|
2017-04-17 10:50:02 +00:00
|
|
|
p.Masters = masters
|
|
|
|
configuration := p.loadMesosConfig()
|
2016-07-20 09:56:14 +00:00
|
|
|
if configuration != nil {
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "mesos",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case err := <-errch:
|
|
|
|
log.Errorf("%s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notify := func(err error, time time.Duration) {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Errorf("Mesos connection error %+v, retrying in %s", err, time)
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
2016-12-08 12:32:12 +00:00
|
|
|
err := backoff.RetryNotify(safe.OperationWithRecover(operation), job.NewBackOff(backoff.NewExponentialBackOff()), notify)
|
2016-07-20 09:56:14 +00:00
|
|
|
if err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Errorf("Cannot connect to Mesos server %+v", err)
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) loadMesosConfig() *types.Configuration {
|
2016-07-20 09:56:14 +00:00
|
|
|
var mesosFuncMap = template.FuncMap{
|
2017-04-17 10:50:02 +00:00
|
|
|
"getBackend": p.getBackend,
|
|
|
|
"getPort": p.getPort,
|
|
|
|
"getHost": p.getHost,
|
|
|
|
"getWeight": p.getWeight,
|
|
|
|
"getDomain": p.getDomain,
|
|
|
|
"getProtocol": p.getProtocol,
|
|
|
|
"getPassHostHeader": p.getPassHostHeader,
|
|
|
|
"getPriority": p.getPriority,
|
|
|
|
"getEntryPoints": p.getEntryPoints,
|
|
|
|
"getFrontendRule": p.getFrontendRule,
|
|
|
|
"getFrontendBackend": p.getFrontendBackend,
|
|
|
|
"getID": p.getID,
|
|
|
|
"getFrontEndName": p.getFrontEndName,
|
|
|
|
}
|
|
|
|
|
|
|
|
t := records.NewRecordGenerator(time.Duration(p.StateTimeoutSecond) * time.Second)
|
|
|
|
sj, err := t.FindMaster(p.Masters...)
|
2016-07-20 09:56:14 +00:00
|
|
|
if err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Errorf("Failed to create a client for Mesos, error: %s", err)
|
2016-07-20 09:56:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
tasks := p.taskRecords(sj)
|
2016-07-20 09:56:14 +00:00
|
|
|
|
|
|
|
//filter tasks
|
|
|
|
filteredTasks := fun.Filter(func(task state.Task) bool {
|
2017-04-17 10:50:02 +00:00
|
|
|
return mesosTaskFilter(task, p.ExposedByDefault)
|
2016-07-20 09:56:14 +00:00
|
|
|
}, tasks).([]state.Task)
|
|
|
|
|
|
|
|
filteredApps := []state.Task{}
|
|
|
|
for _, value := range filteredTasks {
|
|
|
|
if !taskInSlice(value, filteredApps) {
|
|
|
|
filteredApps = append(filteredApps, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
templateObjects := struct {
|
|
|
|
Applications []state.Task
|
|
|
|
Tasks []state.Task
|
|
|
|
Domain string
|
|
|
|
}{
|
|
|
|
filteredApps,
|
|
|
|
filteredTasks,
|
2017-04-17 10:50:02 +00:00
|
|
|
p.Domain,
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
configuration, err := p.GetConfiguration("templates/mesos.tmpl", mesosFuncMap, templateObjects)
|
2016-07-20 09:56:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
return configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskInSlice(a state.Task, list []state.Task) bool {
|
|
|
|
for _, b := range list {
|
|
|
|
if b.DiscoveryInfo.Name == a.DiscoveryInfo.Name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// labels returns all given Status.[]Labels' values whose keys are equal
|
|
|
|
// to the given key
|
|
|
|
func labels(task state.Task, key string) string {
|
|
|
|
for _, l := range task.Labels {
|
|
|
|
if l.Key == key {
|
|
|
|
return l.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func mesosTaskFilter(task state.Task, exposedByDefaultFlag bool) bool {
|
|
|
|
if len(task.DiscoveryInfo.Ports.DiscoveryPorts) == 0 {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debugf("Filtering Mesos task without port %s", task.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !isMesosApplicationEnabled(task, exposedByDefaultFlag) {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debugf("Filtering disabled Mesos task %s", task.DiscoveryInfo.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
//filter indeterminable task port
|
2017-07-10 14:58:12 +00:00
|
|
|
portIndexLabel := labels(task, types.LabelPortIndex)
|
|
|
|
portValueLabel := labels(task, types.LabelPort)
|
2016-07-20 09:56:14 +00:00
|
|
|
if portIndexLabel != "" && portValueLabel != "" {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debugf("Filtering Mesos task %s specifying both traefik.portIndex and traefik.port labels", task.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if portIndexLabel != "" {
|
2017-07-10 14:58:12 +00:00
|
|
|
index, err := strconv.Atoi(labels(task, types.LabelPortIndex))
|
2016-07-20 09:56:14 +00:00
|
|
|
if err != nil || index < 0 || index > len(task.DiscoveryInfo.Ports.DiscoveryPorts)-1 {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debugf("Filtering Mesos task %s with unexpected value for traefik.portIndex label", task.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if portValueLabel != "" {
|
2017-07-10 14:58:12 +00:00
|
|
|
port, err := strconv.Atoi(labels(task, types.LabelPort))
|
2016-07-20 09:56:14 +00:00
|
|
|
if err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debugf("Filtering Mesos task %s with unexpected value for traefik.port label", task.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var foundPort bool
|
|
|
|
for _, exposedPort := range task.DiscoveryInfo.Ports.DiscoveryPorts {
|
|
|
|
if port == exposedPort.Number {
|
|
|
|
foundPort = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !foundPort {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debugf("Filtering Mesos task %s without a matching port for traefik.port label", task.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//filter healthchecks
|
|
|
|
if task.Statuses != nil && len(task.Statuses) > 0 && task.Statuses[0].Healthy != nil && !*task.Statuses[0].Healthy {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debugf("Filtering Mesos task %s with bad healthcheck", task.DiscoveryInfo.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMesos(task state.Task, apps []state.Task) (state.Task, error) {
|
|
|
|
for _, application := range apps {
|
|
|
|
if application.DiscoveryInfo.Name == task.DiscoveryInfo.Name {
|
|
|
|
return application, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return state.Task{}, errors.New("Application not found: " + task.DiscoveryInfo.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isMesosApplicationEnabled(task state.Task, exposedByDefault bool) bool {
|
2017-07-10 14:58:12 +00:00
|
|
|
return exposedByDefault && labels(task, types.LabelEnable) != "false" || labels(task, types.LabelEnable) == "true"
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getLabel(task state.Task, label string) (string, error) {
|
2016-07-20 09:56:14 +00:00
|
|
|
for _, tmpLabel := range task.Labels {
|
|
|
|
if tmpLabel.Key == label {
|
|
|
|
return tmpLabel.Value, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("Label not found:" + label)
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getPort(task state.Task, applications []state.Task) string {
|
2016-07-20 09:56:14 +00:00
|
|
|
application, err := getMesos(task, applications)
|
|
|
|
if err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Errorf("Unable to get Mesos application from task %s", task.DiscoveryInfo.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-07-10 14:58:12 +00:00
|
|
|
if portIndexLabel, err := p.getLabel(application, types.LabelPortIndex); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
if index, err := strconv.Atoi(portIndexLabel); err == nil {
|
|
|
|
return strconv.Itoa(task.DiscoveryInfo.Ports.DiscoveryPorts[index].Number)
|
|
|
|
}
|
|
|
|
}
|
2017-07-10 14:58:12 +00:00
|
|
|
if portValueLabel, err := p.getLabel(application, types.LabelPort); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
return portValueLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range task.DiscoveryInfo.Ports.DiscoveryPorts {
|
|
|
|
return strconv.Itoa(port.Number)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getWeight(task state.Task, applications []state.Task) string {
|
2016-07-20 09:56:14 +00:00
|
|
|
application, errApp := getMesos(task, applications)
|
|
|
|
if errApp != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Errorf("Unable to get Mesos application from task %s", task.DiscoveryInfo.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
2017-07-10 14:58:12 +00:00
|
|
|
if label, err := p.getLabel(application, types.LabelWeight); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getDomain(task state.Task) string {
|
2017-07-10 14:58:12 +00:00
|
|
|
if label, err := p.getLabel(task, types.LabelDomain); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
return label
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
return p.Domain
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getProtocol(task state.Task, applications []state.Task) string {
|
2016-07-20 09:56:14 +00:00
|
|
|
application, errApp := getMesos(task, applications)
|
|
|
|
if errApp != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Errorf("Unable to get Mesos application from task %s", task.DiscoveryInfo.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return "http"
|
|
|
|
}
|
2017-07-10 14:58:12 +00:00
|
|
|
if label, err := p.getLabel(application, types.LabelProtocol); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getPassHostHeader(task state.Task) string {
|
2017-07-10 14:58:12 +00:00
|
|
|
if passHostHeader, err := p.getLabel(task, types.LabelFrontendPassHostHeader); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
return passHostHeader
|
|
|
|
}
|
|
|
|
return "false"
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getPriority(task state.Task) string {
|
2017-07-10 14:58:12 +00:00
|
|
|
if priority, err := p.getLabel(task, types.LabelFrontendPriority); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
return priority
|
|
|
|
}
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getEntryPoints(task state.Task) []string {
|
2017-07-10 14:58:12 +00:00
|
|
|
if entryPoints, err := p.getLabel(task, types.LabelFrontendEntryPoints); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
return strings.Split(entryPoints, ",")
|
|
|
|
}
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getFrontendRule(task state.Task) string {
|
2017-07-10 14:58:12 +00:00
|
|
|
if label, err := p.getLabel(task, types.LabelFrontendRule); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
return label
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
return "Host:" + strings.ToLower(strings.Replace(p.getSubDomain(task.DiscoveryInfo.Name), "_", "-", -1)) + "." + p.Domain
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getBackend(task state.Task, applications []state.Task) string {
|
2016-07-20 09:56:14 +00:00
|
|
|
application, errApp := getMesos(task, applications)
|
|
|
|
if errApp != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Errorf("Unable to get Mesos application from task %s", task.DiscoveryInfo.Name)
|
2016-07-20 09:56:14 +00:00
|
|
|
return ""
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
return p.getFrontendBackend(application)
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getFrontendBackend(task state.Task) string {
|
2017-07-10 14:58:12 +00:00
|
|
|
if label, err := p.getLabel(task, types.LabelBackend); err == nil {
|
2016-07-20 09:56:14 +00:00
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "-" + cleanupSpecialChars(task.DiscoveryInfo.Name)
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getHost(task state.Task) string {
|
|
|
|
return task.IP(strings.Split(p.IPSources, ",")...)
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getID(task state.Task) string {
|
2016-07-20 09:56:14 +00:00
|
|
|
return cleanupSpecialChars(task.ID)
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getFrontEndName(task state.Task) string {
|
2016-07-20 09:56:14 +00:00
|
|
|
return strings.Replace(cleanupSpecialChars(task.ID), "/", "-", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupSpecialChars(s string) string {
|
|
|
|
return strings.Replace(strings.Replace(strings.Replace(s, ".", "-", -1), ":", "-", -1), "_", "-", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func detectMasters(zk string, masters []string) <-chan []string {
|
|
|
|
changed := make(chan []string, 1)
|
|
|
|
if zk != "" {
|
|
|
|
log.Debugf("Starting master detector for ZK ", zk)
|
|
|
|
if md, err := detector.New(zk); err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Errorf("Failed to create master detector: %v", err)
|
2016-07-20 09:56:14 +00:00
|
|
|
} else if err := md.Detect(detect.NewMasters(masters, changed)); err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Errorf("Failed to initialize master detector: %v", err)
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
changed <- masters
|
|
|
|
}
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) taskRecords(sj state.State) []state.Task {
|
|
|
|
var tasks []state.Task // == nil
|
2016-07-20 09:56:14 +00:00
|
|
|
for _, f := range sj.Frameworks {
|
|
|
|
for _, task := range f.Tasks {
|
|
|
|
for _, slave := range sj.Slaves {
|
|
|
|
if task.SlaveID == slave.ID {
|
|
|
|
task.SlaveIP = slave.Hostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// only do running and discoverable tasks
|
|
|
|
if task.State == "TASK_RUNNING" {
|
2017-04-17 10:50:02 +00:00
|
|
|
tasks = append(tasks, task)
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
return tasks
|
2016-07-20 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorFunction A function definition that returns an error
|
|
|
|
// to be passed to the Ignore or Panic error handler
|
|
|
|
type ErrorFunction func() error
|
|
|
|
|
|
|
|
// Ignore Calls an ErrorFunction, and ignores the result.
|
|
|
|
// This allows us to be more explicit when there is no error
|
|
|
|
// handling to be done, for example in defers
|
|
|
|
func Ignore(f ErrorFunction) {
|
|
|
|
_ = f()
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getSubDomain(name string) string {
|
|
|
|
if p.GroupsAsSubDomains {
|
2016-07-20 09:56:14 +00:00
|
|
|
splitedName := strings.Split(strings.TrimPrefix(name, "/"), "/")
|
2017-04-17 10:50:02 +00:00
|
|
|
provider.ReverseStringSlice(&splitedName)
|
2016-07-20 09:56:14 +00:00
|
|
|
reverseName := strings.Join(splitedName, ".")
|
|
|
|
return reverseName
|
|
|
|
}
|
|
|
|
return strings.Replace(strings.TrimPrefix(name, "/"), "/", "-", -1)
|
|
|
|
}
|