k3s/pkg/agent/loadbalancer/config.go

43 lines
1.2 KiB
Go
Raw Normal View History

2019-07-24 07:22:31 +00:00
package loadbalancer
import (
"encoding/json"
"os"
2019-07-24 07:22:31 +00:00
"github.com/k3s-io/k3s/pkg/agent/util"
2019-07-24 07:22:31 +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 {
config := &lbConfig{
ServerURL: lb.scheme + "://" + lb.servers.getDefaultAddress(),
ServerAddresses: lb.servers.getAddresses(),
}
configOut, err := json.MarshalIndent(config, "", " ")
2019-07-24 07:22:31 +00:00
if err != nil {
return err
}
return util.WriteFile(lb.configFile, string(configOut))
2019-07-24 07:22:31 +00:00
}
func (lb *LoadBalancer) updateConfig() error {
if configBytes, err := os.ReadFile(lb.configFile); err == nil {
config := &lbConfig{}
2019-07-24 07:22:31 +00:00
if err := json.Unmarshal(configBytes, config); err == nil {
// 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
}
}
}
// 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
}