2019-07-24 07:22:31 +00:00
|
|
|
package loadbalancer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/google/tcpproxy"
|
2020-05-05 22:09:04 +00:00
|
|
|
"github.com/rancher/k3s/pkg/version"
|
2019-07-24 07:22:31 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LoadBalancer struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
dialer *net.Dialer
|
|
|
|
proxy *tcpproxy.Proxy
|
|
|
|
|
|
|
|
configFile string
|
|
|
|
localAddress string
|
|
|
|
localServerURL string
|
|
|
|
originalServerAddress string
|
|
|
|
ServerURL string
|
|
|
|
ServerAddresses []string
|
|
|
|
randomServers []string
|
|
|
|
currentServerAddress string
|
|
|
|
nextServerIndex int
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
var (
|
|
|
|
SupervisorServiceName = version.Program + "-agent-load-balancer"
|
|
|
|
APIServerServiceName = version.Program + "-api-server-agent-load-balancer"
|
2019-07-24 07:22:31 +00:00
|
|
|
)
|
|
|
|
|
2020-04-28 22:00:30 +00:00
|
|
|
func New(dataDir, serviceName, serverURL string) (_lb *LoadBalancer, _err error) {
|
2019-07-24 07:22:31 +00:00
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
defer func() {
|
|
|
|
if _err != nil {
|
|
|
|
logrus.Warnf("Error starting load balancer: %s", _err)
|
|
|
|
if listener != nil {
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
localAddress := listener.Addr().String()
|
|
|
|
|
2020-04-28 22:00:30 +00:00
|
|
|
originalServerAddress, localServerURL, err := parseURL(serverURL, localAddress)
|
2019-07-24 07:22:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lb := &LoadBalancer{
|
|
|
|
dialer: &net.Dialer{},
|
2020-04-28 22:00:30 +00:00
|
|
|
configFile: filepath.Join(dataDir, "etc", serviceName+".json"),
|
2019-07-24 07:22:31 +00:00
|
|
|
localAddress: localAddress,
|
|
|
|
localServerURL: localServerURL,
|
|
|
|
originalServerAddress: originalServerAddress,
|
2020-04-28 22:00:30 +00:00
|
|
|
ServerURL: serverURL,
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lb.setServers([]string{lb.originalServerAddress})
|
|
|
|
|
|
|
|
lb.proxy = &tcpproxy.Proxy{
|
|
|
|
ListenFunc: func(string, string) (net.Listener, error) {
|
|
|
|
return listener, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
lb.proxy.AddRoute(serviceName, &tcpproxy.DialProxy{
|
|
|
|
Addr: serviceName,
|
|
|
|
DialContext: lb.dialContext,
|
2019-08-08 17:48:11 +00:00
|
|
|
OnDialError: onDialError,
|
2019-07-24 07:22:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err := lb.updateConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := lb.proxy.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logrus.Infof("Running load balancer %s -> %v", lb.localAddress, lb.randomServers)
|
|
|
|
|
|
|
|
return lb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lb *LoadBalancer) Update(serverAddresses []string) {
|
|
|
|
if lb == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !lb.setServers(serverAddresses) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logrus.Infof("Updating load balancer server addresses -> %v", lb.randomServers)
|
|
|
|
|
|
|
|
if err := lb.writeConfig(); err != nil {
|
|
|
|
logrus.Warnf("Error updating load balancer config: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lb *LoadBalancer) LoadBalancerServerURL() string {
|
|
|
|
if lb == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return lb.localServerURL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lb *LoadBalancer) dialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
startIndex := lb.nextServerIndex
|
|
|
|
for {
|
|
|
|
targetServer := lb.currentServerAddress
|
|
|
|
|
|
|
|
conn, err := lb.dialer.DialContext(ctx, network, targetServer)
|
|
|
|
if err == nil {
|
|
|
|
return conn, nil
|
|
|
|
}
|
2019-08-08 17:48:11 +00:00
|
|
|
logrus.Debugf("Dial error from load balancer: %s", err)
|
2019-07-24 07:22:31 +00:00
|
|
|
|
|
|
|
newServer, err := lb.nextServer(targetServer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if targetServer != newServer {
|
2019-08-08 17:48:11 +00:00
|
|
|
logrus.Debugf("Dial server in load balancer failed over to %s", newServer)
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
maxIndex := len(lb.randomServers)
|
|
|
|
if startIndex > maxIndex {
|
|
|
|
startIndex = maxIndex
|
|
|
|
}
|
|
|
|
if lb.nextServerIndex == startIndex {
|
|
|
|
return nil, errors.New("all servers failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 17:48:11 +00:00
|
|
|
|
|
|
|
func onDialError(src net.Conn, dstDialErr error) {
|
|
|
|
logrus.Debugf("Incoming conn %v, error dialing load balancer servers: %v", src.RemoteAddr().String(), dstDialErr)
|
|
|
|
src.Close()
|
|
|
|
}
|