2019-01-29 16:54:05 +00:00
|
|
|
package marathon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gambol99/go-marathon"
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/config/label"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/provider"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/provider/constraints"
|
2019-01-29 16:54:05 +00:00
|
|
|
)
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
func (p *Provider) buildConfiguration(ctx context.Context, applications *marathon.Applications) *dynamic.Configuration {
|
|
|
|
configurations := make(map[string]*dynamic.Configuration)
|
2019-01-29 16:54:05 +00:00
|
|
|
|
|
|
|
for _, app := range applications.Apps {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx).With().Str("applicationID", app.ID).Logger()
|
|
|
|
ctxApp := logger.WithContext(ctx)
|
2019-01-29 16:54:05 +00:00
|
|
|
|
|
|
|
extraConf, err := p.getConfiguration(app)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Skip application")
|
2019-01-29 16:54:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-06-21 07:24:04 +00:00
|
|
|
labels := stringValueMap(app.Labels)
|
|
|
|
|
|
|
|
if app.Constraints != nil {
|
|
|
|
for i, constraintParts := range *app.Constraints {
|
|
|
|
key := constraints.MarathonConstraintPrefix + "-" + strconv.Itoa(i)
|
|
|
|
labels[key] = strings.Join(constraintParts, ":")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.keepApplication(ctxApp, extraConf, labels) {
|
2019-01-29 16:54:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-06-21 07:24:04 +00:00
|
|
|
confFromLabel, err := label.DecodeConfiguration(labels)
|
2019-01-29 16:54:05 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2019-01-29 16:54:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:24:05 +00:00
|
|
|
var tcpOrUDP bool
|
2019-04-15 16:22:07 +00:00
|
|
|
if len(confFromLabel.TCP.Routers) > 0 || len(confFromLabel.TCP.Services) > 0 {
|
2020-02-20 21:24:05 +00:00
|
|
|
tcpOrUDP = true
|
|
|
|
|
2019-04-15 16:22:07 +00:00
|
|
|
err := p.buildTCPServiceConfiguration(ctxApp, app, extraConf, confFromLabel.TCP)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2019-04-15 16:22:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
provider.BuildTCPRouterConfiguration(ctxApp, confFromLabel.TCP)
|
2020-02-20 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(confFromLabel.UDP.Routers) > 0 || len(confFromLabel.UDP.Services) > 0 {
|
|
|
|
tcpOrUDP = true
|
|
|
|
|
|
|
|
err := p.buildUDPServiceConfiguration(ctxApp, app, extraConf, confFromLabel.UDP)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2020-02-20 21:24:05 +00:00
|
|
|
} else {
|
|
|
|
provider.BuildUDPRouterConfiguration(ctxApp, confFromLabel.UDP)
|
2019-04-15 16:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:24:05 +00:00
|
|
|
if tcpOrUDP && len(confFromLabel.HTTP.Routers) == 0 &&
|
|
|
|
len(confFromLabel.HTTP.Middlewares) == 0 &&
|
|
|
|
len(confFromLabel.HTTP.Services) == 0 {
|
|
|
|
configurations[app.ID] = confFromLabel
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
err = p.buildServiceConfiguration(ctxApp, app, extraConf, confFromLabel.HTTP)
|
2019-01-29 16:54:05 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2019-01-29 16:54:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
model := struct {
|
|
|
|
Name string
|
|
|
|
Labels map[string]string
|
|
|
|
}{
|
|
|
|
Name: app.ID,
|
2019-06-21 07:24:04 +00:00
|
|
|
Labels: labels,
|
2019-01-29 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serviceName := getServiceName(app)
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
provider.BuildRouterConfiguration(ctxApp, confFromLabel.HTTP, serviceName, p.defaultRuleTpl, model)
|
2019-01-29 16:54:05 +00:00
|
|
|
|
|
|
|
configurations[app.ID] = confFromLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider.Merge(ctx, configurations)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getServiceName(app marathon.Application) string {
|
2020-09-15 11:08:03 +00:00
|
|
|
return strings.ReplaceAll(strings.TrimPrefix(app.ID, "/"), "/", "_")
|
2019-01-29 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
func (p *Provider) buildServiceConfiguration(ctx context.Context, app marathon.Application, extraConf configuration, conf *dynamic.HTTPConfiguration) error {
|
2019-01-29 16:54:05 +00:00
|
|
|
appName := getServiceName(app)
|
2022-11-21 17:36:05 +00:00
|
|
|
|
|
|
|
logger := log.Ctx(ctx).With().Str("applicationName", appName).Logger()
|
2019-01-29 16:54:05 +00:00
|
|
|
|
|
|
|
if len(conf.Services) == 0 {
|
2019-07-10 07:26:04 +00:00
|
|
|
conf.Services = make(map[string]*dynamic.Service)
|
2019-08-26 08:30:05 +00:00
|
|
|
lb := &dynamic.ServersLoadBalancer{}
|
2019-01-29 16:54:05 +00:00
|
|
|
lb.SetDefaults()
|
2019-07-10 07:26:04 +00:00
|
|
|
conf.Services[appName] = &dynamic.Service{
|
2019-01-29 16:54:05 +00:00
|
|
|
LoadBalancer: lb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for serviceName, service := range conf.Services {
|
2019-07-10 07:26:04 +00:00
|
|
|
var servers []dynamic.Server
|
2019-01-29 16:54:05 +00:00
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
defaultServer := dynamic.Server{}
|
2019-01-29 16:54:05 +00:00
|
|
|
defaultServer.SetDefaults()
|
|
|
|
|
|
|
|
if len(service.LoadBalancer.Servers) > 0 {
|
|
|
|
defaultServer = service.LoadBalancer.Servers[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, task := range app.Tasks {
|
2020-02-20 21:24:05 +00:00
|
|
|
if !p.taskFilter(ctx, *task, app) {
|
|
|
|
continue
|
2019-01-29 16:54:05 +00:00
|
|
|
}
|
2020-02-20 21:24:05 +00:00
|
|
|
server, err := p.getServer(app, *task, extraConf, defaultServer)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Skip task")
|
2020-02-20 21:24:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
servers = append(servers, server)
|
2019-01-29 16:54:05 +00:00
|
|
|
}
|
|
|
|
if len(servers) == 0 {
|
|
|
|
return fmt.Errorf("no server for the service %s", serviceName)
|
|
|
|
}
|
|
|
|
service.LoadBalancer.Servers = servers
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
func (p *Provider) buildTCPServiceConfiguration(ctx context.Context, app marathon.Application, extraConf configuration, conf *dynamic.TCPConfiguration) error {
|
2019-04-15 16:22:07 +00:00
|
|
|
appName := getServiceName(app)
|
2022-11-21 17:36:05 +00:00
|
|
|
|
|
|
|
logger := log.Ctx(ctx).With().Str("applicationName", appName).Logger()
|
2019-04-15 16:22:07 +00:00
|
|
|
|
|
|
|
if len(conf.Services) == 0 {
|
2019-07-10 07:26:04 +00:00
|
|
|
conf.Services = make(map[string]*dynamic.TCPService)
|
2019-09-13 18:00:06 +00:00
|
|
|
lb := &dynamic.TCPServersLoadBalancer{}
|
2019-09-13 15:46:04 +00:00
|
|
|
lb.SetDefaults()
|
2019-07-10 07:26:04 +00:00
|
|
|
conf.Services[appName] = &dynamic.TCPService{
|
2019-04-15 16:22:07 +00:00
|
|
|
LoadBalancer: lb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for serviceName, service := range conf.Services {
|
2019-07-10 07:26:04 +00:00
|
|
|
var servers []dynamic.TCPServer
|
2019-04-15 16:22:07 +00:00
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
defaultServer := dynamic.TCPServer{}
|
2019-04-15 16:22:07 +00:00
|
|
|
|
|
|
|
if len(service.LoadBalancer.Servers) > 0 {
|
|
|
|
defaultServer = service.LoadBalancer.Servers[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, task := range app.Tasks {
|
|
|
|
if p.taskFilter(ctx, *task, app) {
|
|
|
|
server, err := p.getTCPServer(app, *task, extraConf, defaultServer)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Skip task")
|
2019-04-15 16:22:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
servers = append(servers, server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(servers) == 0 {
|
|
|
|
return fmt.Errorf("no server for the service %s", serviceName)
|
|
|
|
}
|
|
|
|
service.LoadBalancer.Servers = servers
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:24:05 +00:00
|
|
|
func (p *Provider) buildUDPServiceConfiguration(ctx context.Context, app marathon.Application, extraConf configuration, conf *dynamic.UDPConfiguration) error {
|
|
|
|
appName := getServiceName(app)
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx).With().Str("applicationName", appName).Logger()
|
2020-02-20 21:24:05 +00:00
|
|
|
|
|
|
|
if len(conf.Services) == 0 {
|
|
|
|
conf.Services = make(map[string]*dynamic.UDPService)
|
|
|
|
lb := &dynamic.UDPServersLoadBalancer{}
|
|
|
|
|
|
|
|
conf.Services[appName] = &dynamic.UDPService{
|
|
|
|
LoadBalancer: lb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for serviceName, service := range conf.Services {
|
|
|
|
var servers []dynamic.UDPServer
|
|
|
|
|
|
|
|
defaultServer := dynamic.UDPServer{}
|
|
|
|
|
|
|
|
if len(service.LoadBalancer.Servers) > 0 {
|
|
|
|
defaultServer = service.LoadBalancer.Servers[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, task := range app.Tasks {
|
|
|
|
if p.taskFilter(ctx, *task, app) {
|
|
|
|
server, err := p.getUDPServer(app, *task, extraConf, defaultServer)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Skip task")
|
2020-02-20 21:24:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
servers = append(servers, server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(servers) == 0 {
|
|
|
|
return fmt.Errorf("no server for the service %s", serviceName)
|
|
|
|
}
|
|
|
|
service.LoadBalancer.Servers = servers
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-21 07:24:04 +00:00
|
|
|
func (p *Provider) keepApplication(ctx context.Context, extraConf configuration, labels map[string]string) bool {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx)
|
2019-01-29 16:54:05 +00:00
|
|
|
|
|
|
|
// Filter disabled application.
|
|
|
|
if !extraConf.Enable {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Filtering disabled Marathon application")
|
2019-01-29 16:54:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by constraints.
|
2019-11-29 16:16:05 +00:00
|
|
|
matches, err := constraints.MatchLabels(labels, p.Constraints)
|
2019-06-21 07:24:04 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Error matching constraints expression")
|
2019-06-21 07:24:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !matches {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msgf("Marathon application filtered by constraint expression: %q", p.Constraints)
|
2019-01-29 16:54:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) taskFilter(ctx context.Context, task marathon.Task, application marathon.Application) bool {
|
|
|
|
if task.State != string(taskStateRunning) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if ready := p.readyChecker.Do(task, application); !ready {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Ctx(ctx).Info().Msgf("Filtering unready task %s from application %s", task.ID, application.ID)
|
2019-01-29 16:54:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
func (p *Provider) getTCPServer(app marathon.Application, task marathon.Task, extraConf configuration, defaultServer dynamic.TCPServer) (dynamic.TCPServer, error) {
|
2019-04-15 16:22:07 +00:00
|
|
|
host, err := p.getServerHost(task, app, extraConf)
|
|
|
|
if len(host) == 0 {
|
2019-07-10 07:26:04 +00:00
|
|
|
return dynamic.TCPServer{}, err
|
2019-04-15 16:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
port, err := getPort(task, app, defaultServer.Port)
|
|
|
|
if err != nil {
|
2019-07-10 07:26:04 +00:00
|
|
|
return dynamic.TCPServer{}, err
|
2019-04-15 16:22:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
server := dynamic.TCPServer{
|
2019-04-15 16:22:07 +00:00
|
|
|
Address: net.JoinHostPort(host, port),
|
|
|
|
}
|
|
|
|
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:24:05 +00:00
|
|
|
func (p *Provider) getUDPServer(app marathon.Application, task marathon.Task, extraConf configuration, defaultServer dynamic.UDPServer) (dynamic.UDPServer, error) {
|
|
|
|
host, err := p.getServerHost(task, app, extraConf)
|
|
|
|
if len(host) == 0 {
|
|
|
|
return dynamic.UDPServer{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
port, err := getPort(task, app, defaultServer.Port)
|
|
|
|
if err != nil {
|
|
|
|
return dynamic.UDPServer{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server := dynamic.UDPServer{
|
|
|
|
Address: net.JoinHostPort(host, port),
|
|
|
|
}
|
|
|
|
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
func (p *Provider) getServer(app marathon.Application, task marathon.Task, extraConf configuration, defaultServer dynamic.Server) (dynamic.Server, error) {
|
2019-01-29 16:54:05 +00:00
|
|
|
host, err := p.getServerHost(task, app, extraConf)
|
|
|
|
if len(host) == 0 {
|
2019-07-10 07:26:04 +00:00
|
|
|
return dynamic.Server{}, err
|
2019-01-29 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
port, err := getPort(task, app, defaultServer.Port)
|
|
|
|
if err != nil {
|
2019-07-10 07:26:04 +00:00
|
|
|
return dynamic.Server{}, err
|
2019-01-29 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
server := dynamic.Server{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: fmt.Sprintf("%s://%s", defaultServer.Scheme, net.JoinHostPort(host, port)),
|
2019-01-29 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) getServerHost(task marathon.Task, app marathon.Application, extraConf configuration) (string, error) {
|
|
|
|
networks := app.Networks
|
|
|
|
var hostFlag bool
|
|
|
|
|
|
|
|
if networks == nil {
|
|
|
|
hostFlag = app.IPAddressPerTask == nil
|
|
|
|
} else {
|
|
|
|
hostFlag = (*networks)[0].Mode != marathon.ContainerNetworkMode
|
|
|
|
}
|
|
|
|
|
|
|
|
if hostFlag || p.ForceTaskHostname {
|
|
|
|
if len(task.Host) == 0 {
|
|
|
|
return "", fmt.Errorf("host is undefined for task %q app %q", task.ID, app.ID)
|
|
|
|
}
|
|
|
|
return task.Host, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
numTaskIPAddresses := len(task.IPAddresses)
|
|
|
|
switch numTaskIPAddresses {
|
|
|
|
case 0:
|
|
|
|
return "", fmt.Errorf("missing IP address for Marathon application %s on task %s", app.ID, task.ID)
|
|
|
|
case 1:
|
|
|
|
return task.IPAddresses[0].IPAddress, nil
|
|
|
|
default:
|
|
|
|
if extraConf.Marathon.IPAddressIdx == math.MinInt32 {
|
|
|
|
return "", fmt.Errorf("found %d task IP addresses but missing IP address index for Marathon application %s on task %s",
|
|
|
|
numTaskIPAddresses, app.ID, task.ID)
|
|
|
|
}
|
|
|
|
if extraConf.Marathon.IPAddressIdx < 0 || extraConf.Marathon.IPAddressIdx > numTaskIPAddresses {
|
|
|
|
return "", fmt.Errorf("cannot use IP address index to select from %d task IP addresses for Marathon application %s on task %s",
|
|
|
|
numTaskIPAddresses, app.ID, task.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.IPAddresses[extraConf.Marathon.IPAddressIdx].IPAddress, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPort(task marathon.Task, app marathon.Application, serverPort string) (string, error) {
|
|
|
|
port, err := processPorts(app, task, serverPort)
|
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return "", fmt.Errorf("unable to process ports for %s %s: %w", app.ID, task.ID, err)
|
2019-01-29 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.Itoa(port), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// processPorts returns the configured port.
|
|
|
|
// An explicitly specified port is preferred. If none is specified, it selects
|
|
|
|
// one of the available port. The first such found port is returned unless an
|
|
|
|
// optional index is provided.
|
|
|
|
func processPorts(app marathon.Application, task marathon.Task, serverPort string) (int, error) {
|
2020-12-16 11:32:03 +00:00
|
|
|
if len(serverPort) > 0 && !(strings.HasPrefix(serverPort, "index:") || strings.HasPrefix(serverPort, "name:")) {
|
2019-01-29 16:54:05 +00:00
|
|
|
port, err := strconv.Atoi(serverPort)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if port <= 0 {
|
|
|
|
return 0, fmt.Errorf("explicitly specified port %d must be greater than zero", port)
|
|
|
|
} else if port > 0 {
|
|
|
|
return port, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 11:32:03 +00:00
|
|
|
if strings.HasPrefix(serverPort, "name:") {
|
|
|
|
name := strings.TrimPrefix(serverPort, "name:")
|
|
|
|
port := retrieveNamedPort(app, name)
|
|
|
|
|
|
|
|
if port == 0 {
|
|
|
|
return 0, fmt.Errorf("no port with name %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return port, nil
|
|
|
|
}
|
|
|
|
|
2019-01-29 16:54:05 +00:00
|
|
|
ports := retrieveAvailablePorts(app, task)
|
|
|
|
if len(ports) == 0 {
|
|
|
|
return 0, errors.New("no port found")
|
|
|
|
}
|
|
|
|
|
|
|
|
portIndex := 0
|
|
|
|
if strings.HasPrefix(serverPort, "index:") {
|
2020-12-16 11:32:03 +00:00
|
|
|
indexString := strings.TrimPrefix(serverPort, "index:")
|
|
|
|
index, err := strconv.Atoi(indexString)
|
2019-01-29 16:54:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if index < 0 || index > len(ports)-1 {
|
|
|
|
return 0, fmt.Errorf("index %d must be within range (0, %d)", index, len(ports)-1)
|
|
|
|
}
|
2020-12-16 11:32:03 +00:00
|
|
|
|
2019-01-29 16:54:05 +00:00
|
|
|
portIndex = index
|
|
|
|
}
|
2020-12-16 11:32:03 +00:00
|
|
|
|
2019-01-29 16:54:05 +00:00
|
|
|
return ports[portIndex], nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 11:32:03 +00:00
|
|
|
func retrieveNamedPort(app marathon.Application, name string) int {
|
|
|
|
// Using port definition if available
|
|
|
|
if app.PortDefinitions != nil && len(*app.PortDefinitions) > 0 {
|
|
|
|
for _, def := range *app.PortDefinitions {
|
|
|
|
if def.Port != nil && *def.Port > 0 && def.Name == name {
|
|
|
|
return *def.Port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If using IP-per-task using this port definition
|
|
|
|
if app.IPAddressPerTask != nil && app.IPAddressPerTask.Discovery != nil && len(*(app.IPAddressPerTask.Discovery.Ports)) > 0 {
|
|
|
|
for _, def := range *(app.IPAddressPerTask.Discovery.Ports) {
|
|
|
|
if def.Number > 0 && def.Name == name {
|
|
|
|
return def.Number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-01-29 16:54:05 +00:00
|
|
|
func retrieveAvailablePorts(app marathon.Application, task marathon.Task) []int {
|
|
|
|
// Using default port configuration
|
|
|
|
if len(task.Ports) > 0 {
|
|
|
|
return task.Ports
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using port definition if available
|
|
|
|
if app.PortDefinitions != nil && len(*app.PortDefinitions) > 0 {
|
|
|
|
var ports []int
|
|
|
|
for _, def := range *app.PortDefinitions {
|
|
|
|
if def.Port != nil {
|
|
|
|
ports = append(ports, *def.Port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ports
|
|
|
|
}
|
|
|
|
|
|
|
|
// If using IP-per-task using this port definition
|
|
|
|
if app.IPAddressPerTask != nil && app.IPAddressPerTask.Discovery != nil && len(*(app.IPAddressPerTask.Discovery.Ports)) > 0 {
|
|
|
|
var ports []int
|
|
|
|
for _, def := range *(app.IPAddressPerTask.Discovery.Ports) {
|
|
|
|
ports = append(ports, def.Number)
|
|
|
|
}
|
|
|
|
return ports
|
|
|
|
}
|
|
|
|
|
|
|
|
return []int{}
|
|
|
|
}
|