/
cluster.go
307 lines (269 loc) · 9.03 KB
/
cluster.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package cluster
import (
"fmt"
"time"
"gopkg.in/yaml.v3"
)
const (
groupsLabel = "groups"
replicasetsLabel = "replicasets"
instancesLabel = "instances"
defaultEtcdTimeout = 3 * time.Second
)
// InstanceConfig describes an instance configuration.
type InstanceConfig struct {
// RawConfig is a raw configuration of the instance scope.
RawConfig *Config `yaml:"-"`
}
// MakeInstanceConfig creates an InstanceConfig object from a configuration.
func MakeInstanceConfig(config *Config) (InstanceConfig, error) {
var cconfig InstanceConfig
err := yaml.Unmarshal([]byte(config.String()), &cconfig)
return cconfig, err
}
// UnmarshalYAML helps to unmarshal an InstanceConfig object from YAML.
func (config *InstanceConfig) UnmarshalYAML(unmarshal func(any) error) error {
config.RawConfig = NewConfig()
if err := unmarshal(&config.RawConfig); err != nil {
return fmt.Errorf("failed to unmarshal InstanceConfig: %w", err)
}
// unmarshal(c) leads to recursion:
//
// config.UnmarshalYAML()->unmarshal()->...->
// config.UnmarshalYAML()->unmarshal()->...
//
// The `parsed` type helps to break the recursion because the type does
// not have `UnmarshalYAML` call.
type instanceConfig InstanceConfig
temp := instanceConfig(*config)
if err := unmarshal(&temp); err != nil {
return fmt.Errorf("failed to unmarshal InstanceConfig: %w", err)
}
*config = InstanceConfig(temp)
return nil
}
// ReplicasetConfig describes a replicaset configuration.
type ReplicasetConfig struct {
// RawConfig is a raw configuration of the replicaset scope.
RawConfig *Config `yaml:"-"`
// Instances are configurations at an instance scope.
Instances map[string]InstanceConfig
}
// UnmarshalYAML helps to unmarshal a ReplicasetConfig object from YAML.
func (config *ReplicasetConfig) UnmarshalYAML(unmarshal func(any) error) error {
config.RawConfig = NewConfig()
if err := unmarshal(&config.RawConfig); err != nil {
return fmt.Errorf("failed to unmarshal ReplicasetConfig: %w", err)
}
// unmarshal(c) leads to recursion:
//
// config.UnmarshalYAML()->unmarshal()->...->
// config.UnmarshalYAML()->unmarshal()->...
//
// The `parsed` type helps to break the recursion because the type does
// not have `UnmarshalYAML` call.
type replicasetConfig ReplicasetConfig
temp := replicasetConfig(*config)
if err := unmarshal(&temp); err != nil {
return fmt.Errorf("failed to unmarshal ReplicasetConfig: %w", err)
}
*config = ReplicasetConfig(temp)
return nil
}
// GroupConfig describes a group configuration.
type GroupConfig struct {
// RawConfig is a raw configuration of the group scope.
RawConfig *Config `yaml:"-"`
// Replicasets are parsed configurations per a replicaset.
Replicasets map[string]ReplicasetConfig
}
// UnmarshalYAML helps to unmarshal a GroupConfig object from YAML.
func (config *GroupConfig) UnmarshalYAML(unmarshal func(any) error) error {
config.RawConfig = NewConfig()
if err := unmarshal(&config.RawConfig); err != nil {
return fmt.Errorf("failed to unmarshal GroupConfig: %w", err)
}
// unmarshal(c) leads to recursion:
//
// config.UnmarshalYAML()->unmarshal()->...->
// config.UnmarshalYAML()->unmarshal()->...
//
// The `parsed` type helps to break the recursion because the type does
// not have `UnmarshalYAML` call.
type groupConfig GroupConfig
temp := groupConfig(*config)
if err := unmarshal(&temp); err != nil {
return fmt.Errorf("failed to unmarshal GroupConfig: %w", err)
}
*config = GroupConfig(temp)
return nil
}
// ClusterConfig describes a cluster configuration.
type ClusterConfig struct {
Config struct {
Etcd struct {
Endpoints []string `yaml:"endpoints"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Prefix string `yaml:"prefix"`
Ssl struct {
KeyFile string `yaml:"ssl_key"`
CertFile string `yaml:"cert_file"`
CaPath string `yaml:"ca_path"`
CaFile string `yaml:"ca_file"`
VerifyPeer bool `yaml:"verify_peer"`
VerifyHost bool `yaml:"verify_host"`
} `yaml:"ssl"`
Http struct {
Request struct {
Timeout float64 `yaml:"timeout"`
} `yaml:"request"`
} `yaml:"http"`
} `yaml:"etcd"`
Storage struct {
Prefix string `yaml:"prefix"`
Timeout float64 `yaml:"timeout"`
Endpoints []struct {
Uri string `yaml:"uri"`
Login string `yaml:"login"`
Password string `yaml:"password"`
Params struct {
Transport string `yaml:"transport"`
SslKeyFile string `yaml:"ssl_key_file"`
SslCertFile string `yaml:"ssl_cert_file"`
SslCaFile string `yaml:"ssl_ca_file"`
SslCiphers string `yaml:"ssl_ciphers"`
SslPassword string `yaml:"ssl_password"`
SslPasswordFile string `yaml:"ssl_password_file"`
} `yaml:"params"`
} `yaml:"endpoints"`
} `yaml:"storage"`
} `yaml:"config"`
// RawConfig is a configuration of the global scope.
RawConfig *Config `yaml:"-"`
// Groups are parsed configurations per a group.
Groups map[string]GroupConfig
}
// UnmarshalYAML helps to unmarshal a ClusterConfig object from YAML.
func (config *ClusterConfig) UnmarshalYAML(unmarshal func(any) error) error {
config.RawConfig = NewConfig()
if err := unmarshal(&config.RawConfig); err != nil {
return fmt.Errorf("failed to unmarshal ClusterConfig: %w", err)
}
// unmarshal(c) leads to recursion:
//
// config.UnmarshalYAML()->unmarshal()->...->
// config.UnmarshalYAML()->unmarshal()->...
//
// The `parsed` type helps to break the recursion because the type does
// not have `UnmarshalYAML` call.
type parsed ClusterConfig
temp := parsed(*config)
if err := unmarshal(&temp); err != nil {
return fmt.Errorf("failed to unmarshal ClusterConfig: %w", err)
}
*config = ClusterConfig(temp)
return nil
}
// MakeClusterConfig creates a ClusterConfig object from a configuration.
func MakeClusterConfig(config *Config) (ClusterConfig, error) {
cconfig := ClusterConfig{
RawConfig: NewConfig(),
}
err := yaml.Unmarshal([]byte(config.String()), &cconfig)
if err != nil {
err = fmt.Errorf(
"failed to parse a configuration data as a cluster config: %w",
err)
return cconfig, err
}
return cconfig, nil
}
// mergeExclude merges a high priority configuration with a low priority
// configuration exclude some path.
func mergeExclude(high, low *Config, excludePath []string) {
lowCopy := NewConfig()
lowCopy.Merge(low)
lowCopy.Set(excludePath, nil)
high.Merge(lowCopy)
}
// findInstance finds an instance with the name in the config and returns
// it full configuration merged from scopes: global, group, replicaset,
// instance or nil.
func findInstance(cluster ClusterConfig, name string) *Config {
for _, group := range cluster.Groups {
for _, replicaset := range group.Replicasets {
for iname, instance := range replicaset.Instances {
if iname == name {
config := NewConfig()
if instance.RawConfig != nil {
config.Merge(instance.RawConfig)
}
mergeExclude(config, replicaset.RawConfig,
[]string{instancesLabel})
mergeExclude(config, group.RawConfig,
[]string{replicasetsLabel})
mergeExclude(config, cluster.RawConfig,
[]string{groupsLabel})
return config
}
}
}
}
return nil
}
// Instances returns a list of instance names from the cluster config.
func Instances(cluster ClusterConfig) []string {
instances := []string{}
for _, group := range cluster.Groups {
for _, replicaset := range group.Replicasets {
for iname, _ := range replicaset.Instances {
instances = append(instances, iname)
}
}
}
return instances
}
// HasInstance returns true if an instance with the name exists in the config.
func HasInstance(cluster ClusterConfig, name string) bool {
return findInstance(cluster, name) != nil
}
// Instantiate returns a fetched instance config from the cluster config. If
// the cluster config has the instance then it returns a merged config of the
// instance from scopes: global, group, replicaset, instance. If the cluster
// config has not the instance then it returns a global scope of the cluster
// config.
func Instantiate(cluster ClusterConfig, name string) *Config {
iconfig := findInstance(cluster, name)
if iconfig != nil {
return iconfig
}
iconfig = NewConfig()
mergeExclude(iconfig, cluster.RawConfig, []string{groupsLabel})
return iconfig
}
// ReplaceInstanceConfig replaces an instance configuration.
func ReplaceInstanceConfig(cconfig ClusterConfig,
instance string, iconfig *Config) (ClusterConfig, error) {
for gname, group := range cconfig.Groups {
for rname, replicaset := range group.Replicasets {
for iname, _ := range replicaset.Instances {
if instance == iname {
path := []string{groupsLabel, gname,
replicasetsLabel, rname,
instancesLabel, iname,
}
newConfig := NewConfig()
newConfig.Merge(cconfig.RawConfig)
if err := newConfig.Set(path, iconfig); err != nil {
err = fmt.Errorf("failed to set configuration: %w", err)
return cconfig, err
}
return MakeClusterConfig(newConfig)
}
}
}
}
return cconfig,
fmt.Errorf("cluster configuration has not an instance %q", instance)
}