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
|
|
|
)
|
|
|
|
|
|
|
|
func (lb *LoadBalancer) writeConfig() error {
|
|
|
|
configOut, err := json.MarshalIndent(lb, "", " ")
|
|
|
|
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 {
|
|
|
|
writeConfig := true
|
2022-10-08 00:36:57 +00:00
|
|
|
if configBytes, err := os.ReadFile(lb.configFile); err == nil {
|
2019-07-24 07:22:31 +00:00
|
|
|
config := &LoadBalancer{}
|
|
|
|
if err := json.Unmarshal(configBytes, config); err == nil {
|
|
|
|
if config.ServerURL == lb.ServerURL {
|
|
|
|
writeConfig = false
|
|
|
|
lb.setServers(config.ServerAddresses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if writeConfig {
|
|
|
|
if err := lb.writeConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|