-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
407 lines (347 loc) · 8.69 KB
/
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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package lazy
import (
"encoding/json"
"errors"
"fmt"
"github.com/go-ini/ini"
"log"
"net"
"os"
"path/filepath"
"strings"
)
type iniConfig ini.File
func loadINIConfig(file string) (*iniConfig, error) {
cfg, err := ini.Load(file)
if err != nil {
log.Println(err)
return nil, err
}
cfg.NameMapper = ini.TitleUnderscore
return (*iniConfig)(cfg), nil
}
func (cfg *iniConfig) newConfigFromSection(s string, v interface{}) (interface{}, error) {
iniFile := (*ini.File)(cfg)
sec, err := iniFile.GetSection(s)
if err != nil {
return v, nil
}
err = sec.MapTo(v)
if err != nil {
if len(s) == 0 {
s = "DEFAULT"
}
log.Println("Parse session", s, "fail")
return nil, err
}
return v, nil
}
func (cfg *iniConfig) newDefaultConfig() (*DefaultConfig, error) {
v, err := cfg.newConfigFromSection("", &DefaultConfig{})
if err != nil {
return nil, err
}
return v.(*DefaultConfig), nil
}
func (cfg *iniConfig) newNodes(ids []string) ([]*Node, error) {
nodes := make([]*Node, 0, len(ids))
for _, id := range ids {
if len(id) == 0 {
continue
}
n, err := cfg.newConfigFromSection(id, &NodeConfig{})
if err != nil {
log.Println("New node config", id, "failed:", err)
continue
}
nodes = append(nodes, &Node{NodeConfig: n.(*NodeConfig)})
}
return nodes, nil
}
func (cfg *iniConfig) newContainerConfig() (*ContainerConfig, error) {
v, err := cfg.newConfigFromSection("container", &ContainerConfig{})
if err != nil {
return nil, err
}
n := v.(*ContainerConfig)
return n, nil
}
func (cfg *iniConfig) newNetworkConfig() (*NetworkConfig, error) {
v, err := cfg.newConfigFromSection("network", &NetworkConfig{})
if err != nil {
return nil, err
}
n := v.(*NetworkConfig)
if len(n.InterfaceBase) == 0 {
n.InterfaceBase = "eth"
}
return n, nil
}
func (cfg *iniConfig) newMatchboxConfig() (*MatchboxConfig, error) {
v, err := cfg.newConfigFromSection("matchbox", &MatchboxConfig{})
if err != nil {
return nil, err
}
return v.(*MatchboxConfig), nil
}
func (cfg *iniConfig) newDNSConfig() (*DNSConfig, error) {
v, err := cfg.newConfigFromSection("dns", &DNSConfig{})
if err != nil {
return nil, err
}
return v.(*DNSConfig), nil
}
func (cfg *iniConfig) newDHCPConfig() (*DHCPConfig, error) {
v, err := cfg.newConfigFromSection("dns", &DHCPConfig{})
if err != nil {
return nil, err
}
return v.(*DHCPConfig), nil
}
func (cfg *iniConfig) newVIPConfig() (*VIPConfig, error) {
v, err := cfg.newConfigFromSection("vip", &VIPConfig{})
if err != nil {
return nil, err
}
return v.(*VIPConfig), nil
}
type Config struct {
*DefaultConfig
C *ContainerConfig
N *NetworkConfig
M *MatchboxConfig
D *DNSConfig
DHCP *DHCPConfig
V *VIPConfig
Nodes []*Node
Cls *Cluster
}
type DefaultConfig struct {
Version string `ini:"version"`
Channel string `ini:"channel"`
DomainBase string `ini:"domain_base"`
NodeIDs []string `ini:"nodes"`
Keys []string `ini:"keys"`
}
type MatchboxConfig struct {
URL string `ini:"url"`
IP string `ini:"ip"`
Domain string `ini:"domain"`
}
type NodeConfig struct {
MAC []string `ini:"mac"`
Role string `ini:"role"`
IP []string `ini:"ip"`
Profile string `ini:"profile"`
}
type ContainerConfig struct {
Registries []string `ini:"registries"`
}
type NetworkConfig struct {
Gateway string `ini:"gateway"`
IPs []string `ini:"ips"`
DHCP_keep int `ini:"dhcp_keep"`
InterfaceBase string `ini:"interface_base"`
}
type DNSConfig struct {
DNS []string `ini:"dns"`
Driver string `ini:"driver"`
}
type DHCPConfig struct {
Enable bool `ini:"enable"`
Interface string `ini:"interface"`
}
type VIPConfig struct {
Enable bool `ini:"enable"`
VIP string `ini:"vip"`
Domain string `ini:"domain"`
}
func Load(file string) (*Config, error) {
cfg, err := loadINIConfig(file)
if err != nil {
log.Println("Load ini config failed:", err)
return nil, err
}
c := &Config{}
if c.DefaultConfig, err = cfg.newDefaultConfig(); err != nil {
log.Println("Load config failed:", err)
return nil, err
}
if c.C, err = cfg.newContainerConfig(); err != nil {
log.Println("Load container config failed:", err)
return nil, err
}
if c.N, err = cfg.newNetworkConfig(); err != nil {
log.Println("Load network config failed:", err)
return nil, err
}
if c.M, err = cfg.newMatchboxConfig(); err != nil {
log.Println("Load matchbox config failed:", err)
return nil, err
}
if c.D, err = cfg.newDNSConfig(); err != nil {
log.Println("Load dns config failed:", err)
return nil, err
}
if c.DHCP, err = cfg.newDHCPConfig(); err != nil {
log.Println("Load dhcp config failed:", err)
return nil, err
}
if c.V, err = cfg.newVIPConfig(); err != nil {
log.Println("Load vip config failed:", err)
return nil, err
}
if c.Nodes, err = cfg.newNodes(c.NodeIDs); err != nil {
log.Println("Load nodes failed:", err)
return nil, err
}
c.Cls = &Cluster{
M: c.M,
}
if err = c.analyze(); err != nil {
log.Println(err)
return nil, err
}
return c, nil
}
func (c *Config) analyze() (err error) {
if err = c.analyzeNetwork(); err != nil {
return errors.New("Analyze network failed: " + err.Error())
}
if err = c.analyzeMatchbox(); err != nil {
return errors.New("Analyze matchbox failed: " + err.Error())
}
if err = c.analyzeNodes(); err != nil {
return errors.New("Analyze nodes failed: " + err.Error())
}
if err = c.analyzeVIP(); err != nil {
return errors.New("Analyze VIP failed: " + err.Error())
}
if err = c.analyzeCluster(); err != nil {
return errors.New("Analyze cluster failed: " + err.Error())
}
return nil
}
func (c *Config) analyzeNetwork() error {
n, err := newNetwork(c.N)
if err != nil {
return err
}
c.Cls.Network = n
return nil
}
func (c *Config) analyzeMatchbox() error {
// TODO: Implement network
return nil
}
func (c *Config) analyzeNodes() error {
for i, node := range c.Nodes {
node.ID = c.NodeIDs[i]
node.Domain = node.ID
if len(c.DomainBase) != 0 {
node.Domain = node.Domain + "." + c.DomainBase
}
if len(node.Profile) == 0 {
node.Profile = "node"
}
nics, err := node.makeInterfaces(c)
if err != nil {
log.Println("Make interfaces failed: ", err)
return err
}
node.Nics = nics
node.Cluster = c.Cls
}
return nil
}
func (c *Config) analyzeVIP() error {
if c.V == nil || !c.V.Enable {
return nil
}
vip := c.V.VIP
if !validateIPv4(vip) {
return errors.New("VIP format is not correct: " + vip)
}
for _, np := range c.Cls.Network.pools {
if !np.Contains(net.ParseIP(vip)) {
continue
}
for _, node := range c.Nodes {
for _, nic := range node.Nics {
if !np.Contains(net.ParseIP(nic.IP)) {
continue
}
node.VIP = &NodeInterface{
IP: vip,
Interface: nic.Interface,
}
}
}
return nil
}
return errors.New("VIP can not find relative network pool: " + vip)
}
func (c *Config) analyzeCluster() error {
var controllerEndpoint string
initialCluster := make([]string, 0, len(c.Nodes))
endpoints := make([]string, 0, len(c.Nodes))
for _, n := range c.Nodes {
if n.Role == "master" {
initialCluster = append(initialCluster, fmt.Sprintf("%s=http://%s:2380", n.ID, n.Domain))
endpoints = append(endpoints, fmt.Sprintf("http://%s:2379", n.Domain))
if len(controllerEndpoint) == 0 {
controllerEndpoint = fmt.Sprintf("https://%s", n.Domain)
}
}
}
if c.V != nil && c.V.Enable {
controllerEndpoint = fmt.Sprintf("https://%s", c.V.Domain)
}
bs, err := json.Marshal(c.Keys)
if err != nil {
log.Println("Convert keys into json array failed: ", err)
bs = []byte("[]")
}
c.Cls.InitialCluster = strings.Join(initialCluster, ",")
c.Cls.Endpoints = strings.Join(endpoints, ",")
c.Cls.ControllerEndpoint = controllerEndpoint
c.Cls.AuthorizedKeys = string(bs)
c.Cls.Registries = c.C.Registries
return nil
}
func (c *Config) Generate(outputPath string) error {
err := os.MkdirAll(outputPath, 0744)
if err != nil {
log.Fatal("Make output path ", outputPath, "fail")
}
err = writeTemplateToFile(OS_INSTALL_TMPL, "install",
filepath.Join(outputPath, "install.json"), c)
if err != nil {
log.Println("Write template install failed: ", err)
}
for _, n := range c.Nodes {
var tmpl, name string
switch n.Role {
case "master":
tmpl = K8S_CONTROLLER_TMPL
name = "controller"
case "minion":
tmpl = K8S_WORKER_TMPL
name = "worker"
default:
tmpl = NODE_TMPL
name = "Node " + n.ID
}
err = writeTemplateToFile(tmpl, name,
filepath.Join(outputPath, n.ID+".json"), n)
if err != nil {
log.Println("Write template install failed: ", err)
}
}
err = writeTemplateToFile(DNSMASQ_TMPL, "dnsmasq",
filepath.Join(outputPath, "dnsmasq.conf"), c)
if err != nil {
log.Println("Write dnsmasq config failed: ", err)
}
return nil
}