2019-03-14 08:30:04 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
"github.com/containous/traefik/pkg/config/dynamic"
|
2019-03-15 08:42:03 +00:00
|
|
|
"github.com/containous/traefik/pkg/log"
|
|
|
|
"github.com/containous/traefik/pkg/server/internal"
|
|
|
|
"github.com/containous/traefik/pkg/tcp"
|
2019-03-14 08:30:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Manager is the TCPHandlers factory
|
|
|
|
type Manager struct {
|
2019-07-10 07:26:04 +00:00
|
|
|
configs map[string]*dynamic.TCPServiceInfo
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager creates a new manager
|
2019-07-10 07:26:04 +00:00
|
|
|
func NewManager(conf *dynamic.RuntimeConfiguration) *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))
|
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
// FIXME Check if the service is declared multiple times with different types
|
2019-05-09 12:30:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
if conf.LoadBalancer == nil {
|
2019-05-16 08:58:06 +00:00
|
|
|
conf.Err = fmt.Errorf("the service %q doesn't have any TCP load balancer", serviceQualifiedName)
|
|
|
|
return nil, conf.Err
|
2019-05-09 12:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger := log.FromContext(ctx)
|
|
|
|
|
|
|
|
loadBalancer := tcp.NewRRLoadBalancer()
|
|
|
|
|
2019-06-11 13:12:04 +00:00
|
|
|
for name, server := range conf.LoadBalancer.Servers {
|
2019-05-16 08:58:06 +00:00
|
|
|
if _, _, err := net.SplitHostPort(server.Address); err != nil {
|
|
|
|
logger.Errorf("In service %q: %v", serviceQualifiedName, err)
|
2019-05-09 12:30:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, err := tcp.NewProxy(server.Address)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("In service %q server %q: %v", serviceQualifiedName, server.Address, err)
|
|
|
|
continue
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2019-05-09 12:30:06 +00:00
|
|
|
|
|
|
|
loadBalancer.AddServer(handler)
|
2019-06-11 13:12:04 +00:00
|
|
|
logger.WithField(log.ServerName, name).Debugf("Creating TCP server %d at %s", name, server.Address)
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2019-05-09 12:30:06 +00:00
|
|
|
return loadBalancer, nil
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|