mirror of https://github.com/k3s-io/k3s
Separate persistent config struct from LoadBalancer and make fields private
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>pull/11430/head
parent
13e9113787
commit
67fd5fa9e5
|
@ -7,8 +7,18 @@ import (
|
|||
"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 {
|
||||
configOut, err := json.MarshalIndent(lb, "", " ")
|
||||
config := &lbConfig{
|
||||
ServerURL: lb.serverURL,
|
||||
ServerAddresses: lb.serverAddresses,
|
||||
}
|
||||
configOut, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -18,9 +28,9 @@ func (lb *LoadBalancer) writeConfig() error {
|
|||
func (lb *LoadBalancer) updateConfig() error {
|
||||
writeConfig := true
|
||||
if configBytes, err := os.ReadFile(lb.configFile); err == nil {
|
||||
config := &LoadBalancer{}
|
||||
config := &lbConfig{}
|
||||
if err := json.Unmarshal(configBytes, config); err == nil {
|
||||
if config.ServerURL == lb.ServerURL {
|
||||
if config.ServerURL == lb.serverURL {
|
||||
writeConfig = false
|
||||
lb.setServers(config.ServerAddresses)
|
||||
}
|
||||
|
|
|
@ -45,13 +45,12 @@ type LoadBalancer struct {
|
|||
localAddress string
|
||||
localServerURL string
|
||||
defaultServerAddress string
|
||||
ServerURL string
|
||||
ServerAddresses []string
|
||||
serverURL string
|
||||
serverAddresses []string
|
||||
randomServers []string
|
||||
servers map[string]*server
|
||||
currentServerAddress string
|
||||
nextServerIndex int
|
||||
Listener net.Listener
|
||||
}
|
||||
|
||||
const RandomPort = 0
|
||||
|
@ -105,7 +104,7 @@ func New(ctx context.Context, dataDir, serviceName, serverURL string, lbServerPo
|
|||
localServerURL: localServerURL,
|
||||
defaultServerAddress: defaultServerAddress,
|
||||
servers: make(map[string]*server),
|
||||
ServerURL: serverURL,
|
||||
serverURL: serverURL,
|
||||
}
|
||||
|
||||
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 {
|
||||
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)
|
||||
|
||||
|
@ -141,7 +140,7 @@ func (lb *LoadBalancer) Update(serverAddresses []string) {
|
|||
if !lb.setServers(serverAddresses) {
|
||||
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 {
|
||||
logrus.Warnf("Error updating load balancer %s config: %s", lb.serviceName, err)
|
||||
|
@ -155,6 +154,13 @@ func (lb *LoadBalancer) LoadBalancerServerURL() string {
|
|||
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) {
|
||||
lb.mutex.RLock()
|
||||
defer lb.mutex.RUnlock()
|
||||
|
|
|
@ -24,7 +24,7 @@ func (lb *LoadBalancer) setServers(serverAddresses []string) bool {
|
|||
defer lb.mutex.Unlock()
|
||||
|
||||
newAddresses := sets.NewString(serverAddresses...)
|
||||
curAddresses := sets.NewString(lb.ServerAddresses...)
|
||||
curAddresses := sets.NewString(lb.serverAddresses...)
|
||||
if newAddresses.Equal(curAddresses) {
|
||||
return false
|
||||
}
|
||||
|
@ -53,8 +53,8 @@ func (lb *LoadBalancer) setServers(serverAddresses []string) bool {
|
|||
}
|
||||
}
|
||||
|
||||
lb.ServerAddresses = serverAddresses
|
||||
lb.randomServers = append([]string{}, lb.ServerAddresses...)
|
||||
lb.serverAddresses = serverAddresses
|
||||
lb.randomServers = append([]string{}, lb.serverAddresses...)
|
||||
rand.Shuffle(len(lb.randomServers), func(i, j int) {
|
||||
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()
|
||||
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 server := lb.servers[lb.defaultServerAddress]; server != nil && !hasDefaultServer {
|
||||
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,
|
||||
// close all the connections to the default server so that clients reconnect and switch over
|
||||
// to a preferred server.
|
||||
hasDefaultServer := slices.Contains(lb.ServerAddresses, lb.defaultServerAddress)
|
||||
hasDefaultServer := slices.Contains(lb.serverAddresses, lb.defaultServerAddress)
|
||||
if healthyServerExists && !hasDefaultServer {
|
||||
if server, ok := lb.servers[lb.defaultServerAddress]; ok {
|
||||
defer server.closeAll()
|
||||
|
|
|
@ -51,7 +51,7 @@ func (e *etcdproxy) Update(addresses []string) {
|
|||
e.etcdLB.Update(addresses)
|
||||
|
||||
validEndpoint := map[string]bool{}
|
||||
for _, address := range e.etcdLB.ServerAddresses {
|
||||
for _, address := range e.etcdLB.ServerAddresses() {
|
||||
validEndpoint[address] = true
|
||||
if _, ok := e.disconnect[address]; !ok {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
|
Loading…
Reference in New Issue