2019-03-14 08:30:04 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-13 18:00:06 +00:00
|
|
|
"errors"
|
2019-03-14 08:30:04 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-09-13 15:46:04 +00:00
|
|
|
"time"
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/runtime"
|
|
|
|
"github.com/containous/traefik/v2/pkg/log"
|
|
|
|
"github.com/containous/traefik/v2/pkg/server/internal"
|
|
|
|
"github.com/containous/traefik/v2/pkg/tcp"
|
2019-03-14 08:30:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Manager is the TCPHandlers factory
|
|
|
|
type Manager struct {
|
2019-07-15 15:04:04 +00:00
|
|
|
configs map[string]*runtime.TCPServiceInfo
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager creates a new manager
|
2019-07-15 15:04:04 +00:00
|
|
|
func NewManager(conf *runtime.Configuration) *Manager {
|
2019-03-14 08:30:04 +00:00
|
|
|
return &Manager{
|
2019-05-16 08:58:06 +00:00
|
|
|
configs: conf.TCPServices,
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildTCP Creates a tcp.Handler for a service configuration.
|
|
|
|
func (m *Manager) BuildTCP(rootCtx context.Context, serviceName string) (tcp.Handler, error) {
|
2019-05-09 12:30:06 +00:00
|
|
|
serviceQualifiedName := internal.GetQualifiedName(rootCtx, serviceName)
|
|
|
|
ctx := internal.AddProviderInContext(rootCtx, serviceQualifiedName)
|
|
|
|
ctx = log.With(ctx, log.Str(log.ServiceName, serviceName))
|
|
|
|
|
|
|
|
conf, ok := m.configs[serviceQualifiedName]
|
|
|
|
if !ok {
|
2019-05-16 08:58:06 +00:00
|
|
|
return nil, fmt.Errorf("the service %q does not exist", serviceQualifiedName)
|
2019-05-09 12:30:06 +00:00
|
|
|
}
|
2019-09-13 18:00:06 +00:00
|
|
|
|
|
|
|
if conf.LoadBalancer != nil && conf.Weighted != nil {
|
|
|
|
err := errors.New("cannot create service: multi-types service not supported, consider declaring two different pieces of service instead")
|
2019-07-19 14:42:04 +00:00
|
|
|
conf.AddError(err, true)
|
|
|
|
return nil, err
|
2019-05-09 12:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger := log.FromContext(ctx)
|
2019-09-13 18:00:06 +00:00
|
|
|
switch {
|
|
|
|
case conf.LoadBalancer != nil:
|
|
|
|
loadBalancer := tcp.NewWRRLoadBalancer()
|
2019-05-09 12:30:06 +00:00
|
|
|
|
2019-09-13 18:00:06 +00:00
|
|
|
if conf.LoadBalancer.TerminationDelay == nil {
|
|
|
|
defaultTerminationDelay := 100
|
|
|
|
conf.LoadBalancer.TerminationDelay = &defaultTerminationDelay
|
|
|
|
}
|
2019-11-26 20:38:03 +00:00
|
|
|
duration := time.Duration(*conf.LoadBalancer.TerminationDelay) * time.Millisecond
|
2019-05-09 12:30:06 +00:00
|
|
|
|
2019-09-13 18:00:06 +00:00
|
|
|
for name, server := range conf.LoadBalancer.Servers {
|
|
|
|
if _, _, err := net.SplitHostPort(server.Address); err != nil {
|
|
|
|
logger.Errorf("In service %q: %v", serviceQualifiedName, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-09-13 15:46:04 +00:00
|
|
|
|
2019-09-13 18:00:06 +00:00
|
|
|
handler, err := tcp.NewProxy(server.Address, duration)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("In service %q server %q: %v", serviceQualifiedName, server.Address, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-05-09 12:30:06 +00:00
|
|
|
|
2019-09-13 18:00:06 +00:00
|
|
|
loadBalancer.AddServer(handler)
|
|
|
|
logger.WithField(log.ServerName, name).Debugf("Creating TCP server %d at %s", name, server.Address)
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2019-09-13 18:00:06 +00:00
|
|
|
return loadBalancer, nil
|
|
|
|
case conf.Weighted != nil:
|
|
|
|
loadBalancer := tcp.NewWRRLoadBalancer()
|
|
|
|
for _, service := range conf.Weighted.Services {
|
|
|
|
handler, err := m.BuildTCP(rootCtx, service.Name)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("In service %q: %v", serviceQualifiedName, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
loadBalancer.AddWeightServer(handler, service.Weight)
|
|
|
|
}
|
|
|
|
return loadBalancer, nil
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("the service %q doesn't have any TCP load balancer", serviceQualifiedName)
|
|
|
|
conf.AddError(err, true)
|
|
|
|
return nil, err
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
}
|