-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_config.go
121 lines (93 loc) · 2.94 KB
/
notify_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"errors"
"fmt"
"net/url"
"os"
"time"
yaml "gopkg.in/yaml.v3"
)
const (
defaultLockFileTemplate = "/var/lock/floaty.%s.lock"
defaultLockTimeout = 10 * time.Second
defaultKeepalivedConfigFile = "/etc/keepalived/keepalived.conf"
defaultRefreshInterval = 1 * time.Minute
defaultRefreshTimeout = 10 * time.Second
)
type notifyConfig struct {
LockFileTemplate string `yaml:"lock-file-template"`
LockTimeout time.Duration `yaml:"lock-timeout"`
KeepalivedConfigFile string `yaml:"keepalived-config"`
ManagedAddresses []netAddress `yaml:"managed-addresses"`
RefreshInterval time.Duration `yaml:"refresh-interval"`
RefreshTimeout time.Duration `yaml:"refresh-timeout"`
BackOff backOffConfig `yaml:"back-off"`
Provider string `yaml:"provider"`
Cloudscale cloudscaleNotifyConfig `yaml:"cloudscale"`
Exoscale exoscaleNotifyConfig `yaml:"exoscale"`
}
func newNotifyConfig() notifyConfig {
return notifyConfig{
LockFileTemplate: defaultLockFileTemplate,
LockTimeout: defaultLockTimeout,
KeepalivedConfigFile: defaultKeepalivedConfigFile,
RefreshInterval: defaultRefreshInterval,
RefreshTimeout: defaultRefreshTimeout,
BackOff: newBackOffConfig(),
}
}
// Update configuration from a YAML file
func (c *notifyConfig) ReadFromYAML(path string) error {
configreader, err := os.Open(path)
if err != nil {
return err
}
decoder := yaml.NewDecoder(configreader)
// NOTE(sg): With gopkg.in/yaml.v3, we use the decoder API instead of
// `Unmarshal` so we can ensure that we get errors for unknown fields.
decoder.KnownFields(true)
return decoder.Decode(c)
}
func (c notifyConfig) NewProvider() (elasticIPProvider, error) {
switch c.Provider {
case "":
return nil, errors.New("Missing provider")
case "cloudscale":
return c.Cloudscale.NewProvider()
case "exoscale":
return c.Exoscale.NewProvider()
case "fake":
return NewFakeProvider()
}
return nil, fmt.Errorf("Provider %q not supported", c.Provider)
}
func (c notifyConfig) MakeLockFilePath(name string) string {
return fmt.Sprintf(c.LockFileTemplate, url.PathEscape(name))
}
func (c notifyConfig) getAddresses(vrrpInstanceName string) ([]netAddress, error) {
if len(c.ManagedAddresses) > 0 {
return c.ManagedAddresses, nil
}
return readAddressesFromKeepalivedConfig(c.KeepalivedConfigFile, vrrpInstanceName)
}
func readAddressesFromKeepalivedConfig(path, vrrpInstanceName string) ([]netAddress, error) {
parsed, err := parseKeepalivedConfigFile(path)
if err != nil {
return nil, err
}
vrrpInstance, ok := parsed.vrrpInstances[vrrpInstanceName]
if !ok {
return nil, fmt.Errorf("No VRRP instance named %q", vrrpInstanceName)
}
return vrrpInstance.Addresses, nil
}
func loadConfig(path string, dryRun bool) (notifyConfig, error) {
cfg := newNotifyConfig()
if err := cfg.ReadFromYAML(path); err != nil {
return cfg, err
}
if dryRun {
cfg.Provider = "fake"
}
return cfg, nil
}