2019-07-24 07:22:31 +00:00
|
|
|
package loadbalancer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
|
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
|
|
|
|
if configBytes, err := ioutil.ReadFile(lb.configFile); err == nil {
|
|
|
|
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
|
|
|
|
}
|