Separate persistent config struct from LoadBalancer and make fields private

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
pull/11430/head
Brad Davidson 2024-11-15 01:32:12 +00:00 committed by Brad Davidson
parent 13e9113787
commit 67fd5fa9e5
4 changed files with 31 additions and 15 deletions

View File

@ -7,8 +7,18 @@ import (
"github.com/k3s-io/k3s/pkg/agent/util" "github.com/k3s-io/k3s/pkg/agent/util"
) )
// lbConfig stores loadbalancer state that should be persisted across restarts.
type lbConfig struct {
ServerURL string `json:"ServerURL"`
ServerAddresses []string `json:"ServerAddresses"`
}
func (lb *LoadBalancer) writeConfig() error { func (lb *LoadBalancer) writeConfig() error {
configOut, err := json.MarshalIndent(lb, "", " ") config := &lbConfig{
ServerURL: lb.serverURL,
ServerAddresses: lb.serverAddresses,
}
configOut, err := json.MarshalIndent(config, "", " ")
if err != nil { if err != nil {
return err return err
} }
@ -18,9 +28,9 @@ func (lb *LoadBalancer) writeConfig() error {
func (lb *LoadBalancer) updateConfig() error { func (lb *LoadBalancer) updateConfig() error {
writeConfig := true writeConfig := true
if configBytes, err := os.ReadFile(lb.configFile); err == nil { if configBytes, err := os.ReadFile(lb.configFile); err == nil {
config := &LoadBalancer{} config := &lbConfig{}
if err := json.Unmarshal(configBytes, config); err == nil { if err := json.Unmarshal(configBytes, config); err == nil {
if config.ServerURL == lb.ServerURL { if config.ServerURL == lb.serverURL {
writeConfig = false writeConfig = false
lb.setServers(config.ServerAddresses) lb.setServers(config.ServerAddresses)
} }

View File

@ -45,13 +45,12 @@ type LoadBalancer struct {
localAddress string localAddress string
localServerURL string localServerURL string
defaultServerAddress string defaultServerAddress string
ServerURL string serverURL string
ServerAddresses []string serverAddresses []string
randomServers []string randomServers []string
servers map[string]*server servers map[string]*server
currentServerAddress string currentServerAddress string
nextServerIndex int nextServerIndex int
Listener net.Listener
} }
const RandomPort = 0 const RandomPort = 0
@ -105,7 +104,7 @@ func New(ctx context.Context, dataDir, serviceName, serverURL string, lbServerPo
localServerURL: localServerURL, localServerURL: localServerURL,
defaultServerAddress: defaultServerAddress, defaultServerAddress: defaultServerAddress,
servers: make(map[string]*server), servers: make(map[string]*server),
ServerURL: serverURL, serverURL: serverURL,
} }
lb.setServers([]string{lb.defaultServerAddress}) lb.setServers([]string{lb.defaultServerAddress})
@ -127,7 +126,7 @@ func New(ctx context.Context, dataDir, serviceName, serverURL string, lbServerPo
if err := lb.proxy.Start(); err != nil { if err := lb.proxy.Start(); err != nil {
return nil, err return nil, err
} }
logrus.Infof("Running load balancer %s %s -> %v [default: %s]", serviceName, lb.localAddress, lb.ServerAddresses, lb.defaultServerAddress) logrus.Infof("Running load balancer %s %s -> %v [default: %s]", serviceName, lb.localAddress, lb.serverAddresses, lb.defaultServerAddress)
go lb.runHealthChecks(ctx) go lb.runHealthChecks(ctx)
@ -141,7 +140,7 @@ func (lb *LoadBalancer) Update(serverAddresses []string) {
if !lb.setServers(serverAddresses) { if !lb.setServers(serverAddresses) {
return return
} }
logrus.Infof("Updated load balancer %s server addresses -> %v [default: %s]", lb.serviceName, lb.ServerAddresses, lb.defaultServerAddress) logrus.Infof("Updated load balancer %s server addresses -> %v [default: %s]", lb.serviceName, lb.serverAddresses, lb.defaultServerAddress)
if err := lb.writeConfig(); err != nil { if err := lb.writeConfig(); err != nil {
logrus.Warnf("Error updating load balancer %s config: %s", lb.serviceName, err) logrus.Warnf("Error updating load balancer %s config: %s", lb.serviceName, err)
@ -155,6 +154,13 @@ func (lb *LoadBalancer) LoadBalancerServerURL() string {
return lb.localServerURL return lb.localServerURL
} }
func (lb *LoadBalancer) ServerAddresses() []string {
if lb == nil {
return nil
}
return lb.serverAddresses
}
func (lb *LoadBalancer) dialContext(ctx context.Context, network, _ string) (net.Conn, error) { func (lb *LoadBalancer) dialContext(ctx context.Context, network, _ string) (net.Conn, error) {
lb.mutex.RLock() lb.mutex.RLock()
defer lb.mutex.RUnlock() defer lb.mutex.RUnlock()

View File

@ -24,7 +24,7 @@ func (lb *LoadBalancer) setServers(serverAddresses []string) bool {
defer lb.mutex.Unlock() defer lb.mutex.Unlock()
newAddresses := sets.NewString(serverAddresses...) newAddresses := sets.NewString(serverAddresses...)
curAddresses := sets.NewString(lb.ServerAddresses...) curAddresses := sets.NewString(lb.serverAddresses...)
if newAddresses.Equal(curAddresses) { if newAddresses.Equal(curAddresses) {
return false return false
} }
@ -53,8 +53,8 @@ func (lb *LoadBalancer) setServers(serverAddresses []string) bool {
} }
} }
lb.ServerAddresses = serverAddresses lb.serverAddresses = serverAddresses
lb.randomServers = append([]string{}, lb.ServerAddresses...) lb.randomServers = append([]string{}, lb.serverAddresses...)
rand.Shuffle(len(lb.randomServers), func(i, j int) { rand.Shuffle(len(lb.randomServers), func(i, j int) {
lb.randomServers[i], lb.randomServers[j] = lb.randomServers[j], lb.randomServers[i] lb.randomServers[i], lb.randomServers[j] = lb.randomServers[j], lb.randomServers[i]
}) })
@ -155,7 +155,7 @@ func (lb *LoadBalancer) SetDefault(serverAddress string) {
lb.mutex.Lock() lb.mutex.Lock()
defer lb.mutex.Unlock() defer lb.mutex.Unlock()
hasDefaultServer := slices.Contains(lb.ServerAddresses, lb.defaultServerAddress) hasDefaultServer := slices.Contains(lb.serverAddresses, lb.defaultServerAddress)
// if the old default server is not currently in use, remove it from the server map // if the old default server is not currently in use, remove it from the server map
if server := lb.servers[lb.defaultServerAddress]; server != nil && !hasDefaultServer { if server := lb.servers[lb.defaultServerAddress]; server != nil && !hasDefaultServer {
defer server.closeAll() defer server.closeAll()
@ -211,7 +211,7 @@ func (lb *LoadBalancer) runHealthChecks(ctx context.Context) {
// If there is at least one healthy server, and the default server is not in the server list, // If there is at least one healthy server, and the default server is not in the server list,
// close all the connections to the default server so that clients reconnect and switch over // close all the connections to the default server so that clients reconnect and switch over
// to a preferred server. // to a preferred server.
hasDefaultServer := slices.Contains(lb.ServerAddresses, lb.defaultServerAddress) hasDefaultServer := slices.Contains(lb.serverAddresses, lb.defaultServerAddress)
if healthyServerExists && !hasDefaultServer { if healthyServerExists && !hasDefaultServer {
if server, ok := lb.servers[lb.defaultServerAddress]; ok { if server, ok := lb.servers[lb.defaultServerAddress]; ok {
defer server.closeAll() defer server.closeAll()

View File

@ -51,7 +51,7 @@ func (e *etcdproxy) Update(addresses []string) {
e.etcdLB.Update(addresses) e.etcdLB.Update(addresses)
validEndpoint := map[string]bool{} validEndpoint := map[string]bool{}
for _, address := range e.etcdLB.ServerAddresses { for _, address := range e.etcdLB.ServerAddresses() {
validEndpoint[address] = true validEndpoint[address] = true
if _, ok := e.disconnect[address]; !ok { if _, ok := e.disconnect[address]; !ok {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())