2019-07-24 07:22:31 +00:00
|
|
|
package loadbalancer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-15 00:31:49 +00:00
|
|
|
"fmt"
|
2019-07-24 07:22:31 +00:00
|
|
|
"net"
|
2024-11-15 22:11:47 +00:00
|
|
|
"net/url"
|
2021-08-17 23:02:30 +00:00
|
|
|
"os"
|
2019-07-24 07:22:31 +00:00
|
|
|
"path/filepath"
|
2024-11-15 22:11:47 +00:00
|
|
|
"strings"
|
2019-07-24 07:22:31 +00:00
|
|
|
|
2024-10-09 16:55:19 +00:00
|
|
|
"github.com/inetaf/tcpproxy"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2019-07-24 07:22:31 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-03-31 20:51:27 +00:00
|
|
|
// LoadBalancer holds data for a local listener which forwards connections to a
|
|
|
|
// pool of remote servers. It is not a proper load-balancer in that it does not
|
|
|
|
// actually balance connections, but instead fails over to a new server only
|
|
|
|
// when a connection attempt to the currently selected server fails.
|
2019-07-24 07:22:31 +00:00
|
|
|
type LoadBalancer struct {
|
2024-11-15 22:11:47 +00:00
|
|
|
serviceName string
|
|
|
|
configFile string
|
|
|
|
scheme string
|
|
|
|
localAddress string
|
|
|
|
servers serverList
|
|
|
|
proxy *tcpproxy.Proxy
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 15:35:57 +00:00
|
|
|
const RandomPort = 0
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
var (
|
|
|
|
SupervisorServiceName = version.Program + "-agent-load-balancer"
|
|
|
|
APIServerServiceName = version.Program + "-api-server-agent-load-balancer"
|
2021-02-12 15:35:57 +00:00
|
|
|
ETCDServerServiceName = version.Program + "-etcd-server-load-balancer"
|
2019-07-24 07:22:31 +00:00
|
|
|
)
|
|
|
|
|
2023-03-31 20:51:27 +00:00
|
|
|
// New contstructs a new LoadBalancer instance. The default server URL, and
|
|
|
|
// currently active servers, are stored in a file within the dataDir.
|
2024-11-15 22:11:47 +00:00
|
|
|
func New(ctx context.Context, dataDir, serviceName, defaultServerURL string, lbServerPort int, isIPv6 bool) (_lb *LoadBalancer, _err error) {
|
2021-03-08 22:10:00 +00:00
|
|
|
config := net.ListenConfig{Control: reusePort}
|
2022-04-15 00:31:49 +00:00
|
|
|
var localAddress string
|
2022-03-31 09:49:30 +00:00
|
|
|
if isIPv6 {
|
2022-04-15 00:31:49 +00:00
|
|
|
localAddress = fmt.Sprintf("[::1]:%d", lbServerPort)
|
|
|
|
} else {
|
|
|
|
localAddress = fmt.Sprintf("127.0.0.1:%d", lbServerPort)
|
2022-03-31 09:49:30 +00:00
|
|
|
}
|
2022-04-15 00:31:49 +00:00
|
|
|
listener, err := config.Listen(ctx, "tcp", localAddress)
|
2019-07-24 07:22:31 +00:00
|
|
|
defer func() {
|
|
|
|
if _err != nil {
|
|
|
|
logrus.Warnf("Error starting load balancer: %s", _err)
|
|
|
|
if listener != nil {
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-15 00:31:49 +00:00
|
|
|
|
2024-11-15 22:11:47 +00:00
|
|
|
serverURL, err := url.Parse(defaultServerURL)
|
2019-07-24 07:22:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-11-15 22:11:47 +00:00
|
|
|
// Set explicit port from scheme
|
|
|
|
if serverURL.Port() == "" {
|
|
|
|
if strings.ToLower(serverURL.Scheme) == "http" {
|
|
|
|
serverURL.Host += ":80"
|
|
|
|
}
|
|
|
|
if strings.ToLower(serverURL.Scheme) == "https" {
|
|
|
|
serverURL.Host += ":443"
|
|
|
|
}
|
2021-03-06 10:29:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 07:22:31 +00:00
|
|
|
lb := &LoadBalancer{
|
2024-11-15 22:11:47 +00:00
|
|
|
serviceName: serviceName,
|
|
|
|
configFile: filepath.Join(dataDir, "etc", serviceName+".json"),
|
|
|
|
scheme: serverURL.Scheme,
|
|
|
|
localAddress: listener.Addr().String(),
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 22:11:47 +00:00
|
|
|
// if starting pointing at ourselves, don't set a default server address,
|
|
|
|
// which will cause all dials to fail until servers are added.
|
|
|
|
if serverURL.Host == lb.localAddress {
|
|
|
|
logrus.Debugf("Initial server URL for load balancer %s points at local server URL - starting with empty default server address", serviceName)
|
|
|
|
} else {
|
|
|
|
lb.servers.setDefaultAddress(lb.serviceName, serverURL.Host)
|
|
|
|
}
|
2019-07-24 07:22:31 +00:00
|
|
|
|
|
|
|
lb.proxy = &tcpproxy.Proxy{
|
|
|
|
ListenFunc: func(string, string) (net.Listener, error) {
|
|
|
|
return listener, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
lb.proxy.AddRoute(serviceName, &tcpproxy.DialProxy{
|
|
|
|
Addr: serviceName,
|
2024-11-15 22:11:47 +00:00
|
|
|
DialContext: lb.servers.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
|
|
|
|
}
|
2024-11-15 22:11:47 +00:00
|
|
|
logrus.Infof("Running load balancer %s %s -> %v [default: %s]", serviceName, lb.localAddress, lb.servers.getAddresses(), lb.servers.getDefaultAddress())
|
2019-07-24 07:22:31 +00:00
|
|
|
|
2024-11-15 22:11:47 +00:00
|
|
|
go lb.servers.runHealthChecks(ctx, lb.serviceName)
|
2023-03-31 20:51:27 +00:00
|
|
|
|
2024-03-19 22:01:36 +00:00
|
|
|
return lb, nil
|
2022-02-16 22:19:58 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 22:11:47 +00:00
|
|
|
// Update updates the list of server addresses to contain only the listed servers.
|
2019-07-24 07:22:31 +00:00
|
|
|
func (lb *LoadBalancer) Update(serverAddresses []string) {
|
2024-11-15 22:11:47 +00:00
|
|
|
if !lb.servers.setAddresses(lb.serviceName, serverAddresses) {
|
2019-07-24 07:22:31 +00:00
|
|
|
return
|
|
|
|
}
|
2024-11-15 22:11:47 +00:00
|
|
|
|
|
|
|
logrus.Infof("Updated load balancer %s server addresses -> %v [default: %s]", lb.serviceName, lb.servers.getAddresses(), lb.servers.getDefaultAddress())
|
2019-07-24 07:22:31 +00:00
|
|
|
|
|
|
|
if err := lb.writeConfig(); err != nil {
|
2022-02-16 22:19:58 +00:00
|
|
|
logrus.Warnf("Error updating load balancer %s config: %s", lb.serviceName, err)
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 22:11:47 +00:00
|
|
|
// SetDefault sets the selected address as the default / fallback address
|
|
|
|
func (lb *LoadBalancer) SetDefault(serverAddress string) {
|
|
|
|
lb.servers.setDefaultAddress(lb.serviceName, serverAddress)
|
|
|
|
|
|
|
|
if err := lb.writeConfig(); err != nil {
|
|
|
|
logrus.Warnf("Error updating load balancer %s config: %s", lb.serviceName, err)
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 22:11:47 +00:00
|
|
|
// SetHealthCheck adds a health-check callback to an address, replacing the default no-op function.
|
|
|
|
func (lb *LoadBalancer) SetHealthCheck(address string, healthCheck HealthCheckFunc) {
|
|
|
|
if err := lb.servers.setHealthCheck(address, healthCheck); err != nil {
|
|
|
|
logrus.Errorf("Failed to set health check for load balancer %s: %v", lb.serviceName, err)
|
|
|
|
} else {
|
|
|
|
logrus.Debugf("Set health check for load balancer %s: %s", lb.serviceName, address)
|
2024-11-15 01:32:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 22:11:47 +00:00
|
|
|
func (lb *LoadBalancer) LocalURL() string {
|
|
|
|
return lb.scheme + "://" + lb.localAddress
|
|
|
|
}
|
2019-07-24 07:22:31 +00:00
|
|
|
|
2024-11-15 22:11:47 +00:00
|
|
|
func (lb *LoadBalancer) ServerAddresses() []string {
|
|
|
|
return lb.servers.getAddresses()
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
2019-08-08 17:48:11 +00:00
|
|
|
|
|
|
|
func onDialError(src net.Conn, dstDialErr error) {
|
2023-03-31 20:51:27 +00:00
|
|
|
logrus.Debugf("Incoming conn %s, error dialing load balancer servers: %v", src.RemoteAddr(), dstDialErr)
|
2019-08-08 17:48:11 +00:00
|
|
|
src.Close()
|
|
|
|
}
|
2021-08-17 23:02:30 +00:00
|
|
|
|
2022-02-16 22:19:58 +00:00
|
|
|
// ResetLoadBalancer will delete the local state file for the load balancer on disk
|
2024-11-15 22:11:47 +00:00
|
|
|
func ResetLoadBalancer(dataDir, serviceName string) {
|
2021-08-17 23:02:30 +00:00
|
|
|
stateFile := filepath.Join(dataDir, "etc", serviceName+".json")
|
2024-11-15 22:11:47 +00:00
|
|
|
if err := os.Remove(stateFile); err != nil && !os.IsNotExist(err) {
|
2021-08-17 23:02:30 +00:00
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
|
|
|
}
|