2019-07-24 07:22:31 +00:00
|
|
|
package loadbalancer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-10-08 00:36:57 +00:00
|
|
|
"os"
|
2019-07-24 07:22:31 +00:00
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/agent/util"
|
2019-07-24 07:22:31 +00:00
|
|
|
)
|
|
|
|
|
2024-11-15 01:32:12 +00:00
|
|
|
// lbConfig stores loadbalancer state that should be persisted across restarts.
|
|
|
|
type lbConfig struct {
|
|
|
|
ServerURL string `json:"ServerURL"`
|
|
|
|
ServerAddresses []string `json:"ServerAddresses"`
|
|
|
|
}
|
|
|
|
|
2019-07-24 07:22:31 +00:00
|
|
|
func (lb *LoadBalancer) writeConfig() error {
|
2024-11-15 01:32:12 +00:00
|
|
|
config := &lbConfig{
|
2024-11-15 22:11:47 +00:00
|
|
|
ServerURL: lb.scheme + "://" + lb.servers.getDefaultAddress(),
|
|
|
|
ServerAddresses: lb.servers.getAddresses(),
|
2024-11-15 01:32:12 +00:00
|
|
|
}
|
|
|
|
configOut, err := json.MarshalIndent(config, "", " ")
|
2019-07-24 07:22:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-03 11:24:58 +00:00
|
|
|
return util.WriteFile(lb.configFile, string(configOut))
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lb *LoadBalancer) updateConfig() error {
|
2022-10-08 00:36:57 +00:00
|
|
|
if configBytes, err := os.ReadFile(lb.configFile); err == nil {
|
2024-11-15 01:32:12 +00:00
|
|
|
config := &lbConfig{}
|
2019-07-24 07:22:31 +00:00
|
|
|
if err := json.Unmarshal(configBytes, config); err == nil {
|
2024-11-15 22:11:47 +00:00
|
|
|
// if the default server from the config matches our current default,
|
|
|
|
// load the rest of the addresses as well.
|
|
|
|
if config.ServerURL == lb.scheme+"://"+lb.servers.getDefaultAddress() {
|
|
|
|
lb.Update(config.ServerAddresses)
|
|
|
|
return nil
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-15 22:11:47 +00:00
|
|
|
// config didn't exist or used a different default server, write the current config to disk.
|
|
|
|
return lb.writeConfig()
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|