/
poolService.go
665 lines (644 loc) · 23.7 KB
/
poolService.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package netservice
import (
"encoding/json"
"fmt"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
types "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi/netservice"
"net"
"path/filepath"
"strconv"
"time"
)
func getAllHosts(cidr string) ([]string, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
var ips []string
for checkip := ip.Mask(ipnet.Mask); ipnet.Contains(checkip); inc(checkip) {
ips = append(ips, checkip.String())
}
// remove network address and broadcast address
return ips[1 : len(ips)-1], nil
}
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func stringInSlice(str string, strs []string) bool {
for _, item := range strs {
if str == item {
return true
}
}
return false
}
//AddPool construct pool info
func (srv *NetService) AddPool(pool *types.NetPool) error {
started := time.Now()
//check & construct data
pool.Created = time.Now().Format("2006-01-02 15:04:05")
pool.Update = pool.Created
poolPath := filepath.Join(defaultPoolInfoPath, pool.GetKey())
blog.Info("try to Add pool %s", poolPath)
//creat netpool node with pool data
poolData, jerr := json.Marshal(pool)
if jerr != nil {
blog.Errorf("Pool %s json err: %s", pool.GetKey(), jerr.Error())
reportMetrics("addpool", stateJSONFailure, started)
return jerr
}
if exist, _ := srv.store.Exist(poolPath); exist {
blog.Errorf("Pool %s is exist, skip create", pool.GetKey())
reportMetrics("addpool", stateNonExistFailure, started)
return fmt.Errorf("pool %s exist, create failed", pool.GetKey())
}
if err := srv.initPoolPath(pool, poolPath, poolData); err != nil {
reportMetrics("addpool", stateLogicFailure, started)
return err
}
for _, ip := range pool.Reserved {
inst := &types.IPInst{
IPAddr: ip,
Pool: pool.Net,
Mask: pool.Mask,
Gateway: pool.Gateway,
LastStatus: "created",
Status: types.IPStatus_RESERVED,
Update: time.Now().Format("2006-01-02 15:04:05"),
Container: "",
Host: "",
}
instData, _ := json.Marshal(inst)
instPath := filepath.Join(poolPath, "reserved", inst.GetKey())
if err := srv.store.Add(instPath, instData); err != nil {
blog.Errorf("Pool create reserved node %s err: %v", inst.GetKey(), err)
reportMetrics("addpool", stateLogicFailure, started)
return err
}
blog.Info("Pool create reserved node %s success.", inst.GetKey())
}
//create available node
if err := srv.store.Add(poolPath+"/available", []byte("available")); err != nil {
blog.Errorf("Pool %s create reserved node err: %s", pool.GetKey(), err)
reportMetrics("addpool", stateLogicFailure, started)
return err
}
//create available ip instance with certain ip
blog.Info("Pool create available ip instance with certain ip")
for _, ip := range pool.Available {
inst := &types.IPInst{
IPAddr: ip,
Pool: pool.Net,
Mask: pool.Mask,
Gateway: pool.Gateway,
LastStatus: "created",
Status: types.IPStatus_AVAILABLE,
Update: time.Now().Format("2006-01-02 15:04:05"),
Container: "",
Host: "",
}
instData, _ := json.Marshal(inst)
instPath := filepath.Join(poolPath, "available", inst.GetKey())
if err := srv.store.Add(instPath, instData); err != nil {
blog.Errorf("Pool create available node %s err: %v", inst.GetKey(), err)
reportMetrics("addpool", stateLogicFailure, started)
return err
}
blog.Info("Pool create available node %s success.", inst.GetKey())
}
//如果Reserved和Available都没有填,默认就是整个网段都是Available
if len(pool.Reserved) == 0 && len(pool.Available) == 0 {
blog.Info("Pool create available ip instance with net pool")
allIps, _ := getAllHosts(pool.Net + "/" + strconv.Itoa(pool.Mask))
for _, ip := range allIps {
//fix(DeveloperJim): clean gateway in available ip address
if ip == pool.Gateway {
continue
}
if stringInSlice(ip, pool.Reserved) {
continue
}
inst := &types.IPInst{
IPAddr: ip,
Pool: pool.Net,
Mask: pool.Mask,
Gateway: pool.Gateway,
LastStatus: "created",
Status: types.IPStatus_AVAILABLE,
Update: time.Now().Format("2006-01-02 15:04:05"),
Container: "",
Host: "",
}
instData, _ := json.Marshal(inst)
instPath := filepath.Join(poolPath, "available", inst.GetKey())
if err := srv.store.Add(instPath, instData); err != nil {
blog.Errorf("Pool create available node %s err: %v", inst.GetKey(), err)
reportMetrics("addpool", stateLogicFailure, started)
return err
}
blog.Info("Pool create available node %s success.", inst.GetKey())
}
}
//create host node for query
for _, host := range pool.Hosts {
pHostPath := filepath.Join(poolPath, "hosts", host)
hostInfo := &types.HostInfo{
IPAddr: host,
Pool: pool.Net,
Cluster: pool.Cluster,
Created: time.Now().Format("2006-01-02 15:04:05"),
Update: time.Now().Format("2006-01-02 15:04:05"),
}
hostData, _ := json.Marshal(hostInfo)
if err := srv.store.Add(pHostPath, hostData); err != nil {
blog.Errorf("Pool create query host node %s err: %v", host, err)
reportMetrics("addpool", stateLogicFailure, started)
return err
}
hostPath := filepath.Join(defaultHostInfoPath, host)
if err := srv.store.Add(hostPath, hostData); err != nil {
blog.Errorf("Create Host Node %s err: %v", hostPath, err)
} else {
blog.Info("Create Host node %s under pool %s success", host, pool.GetKey())
}
}
reportMetrics("addpool", stateSuccess, started)
return nil
}
func (srv *NetService) initPoolPath(pool *types.NetPool, poolPath string, poolData []byte) error {
if err := srv.store.Add(poolPath, poolData); err != nil {
blog.Errorf("Pool %s create data node %v err: %s", pool.GetKey(), pool, err.Error())
return err
}
if err := srv.store.Add(poolPath+"/active", []byte("active")); err != nil {
blog.Errorf("Pool %s create active node err: %s", pool.GetKey(), err)
return err
}
if err := srv.store.Add(poolPath+"/hosts", []byte("hosts")); err != nil {
blog.Errorf("Pool %s create hosts node err: %s", pool.GetKey(), err)
return err
}
//creat reserved node
if err := srv.store.Add(poolPath+"/reserved", []byte("reserved")); err != nil {
blog.Errorf("Pool %s create reserved node err: %s", pool.GetKey(), err)
return err
}
return nil
}
//DeletePool update Pool info
func (srv *NetService) DeletePool(netKey string) error {
started := time.Now()
//check pool existence
poolPath := filepath.Join(defaultPoolInfoPath, netKey)
blog.Info("try to delete pool %s", poolPath)
if exist, _ := srv.store.Exist(poolPath); !exist {
blog.Errorf("Delete pool %s error, no such Pool Info", netKey)
reportMetrics("deletePool", stateNonExistFailure, started)
return fmt.Errorf("pool %s do not exist", netKey)
}
//try to lock
lockPath := filepath.Join(defaultLockerPath, netKey)
distLocker, err := srv.store.GetLocker(lockPath)
if err != nil {
blog.Errorf("create pool %s lock err in delete pool, %v", netKey, err)
reportMetrics("deletePool", stateLogicFailure, started)
return fmt.Errorf("create pool %s lock err, %v", lockPath, err)
}
defer distLocker.Unlock()
if err := distLocker.Lock(); err != nil {
blog.Errorf("try to lock pool %s error, %v", netKey, err)
reportMetrics("deletePool", stateLogicFailure, started)
return fmt.Errorf("lock pool %s err, %v", netKey, err)
}
blog.Info("Lock pool %s in deletion success.", lockPath)
//check active node is empty
activepath := filepath.Join(defaultPoolInfoPath, netKey, "active")
activeIPList, listErr := srv.store.List(activepath)
if listErr != nil {
blog.Errorf("check pool %s active ip address info in deletion err, %v", netKey, listErr)
reportMetrics("deletePool", stateLogicFailure, started)
return fmt.Errorf("check active ip list err, %v", listErr)
}
//check delete permision
if len(activeIPList) != 0 {
blog.Errorf("pool %s has %d ip in active, delete is deny", netKey, len(activeIPList))
reportMetrics("deletePool", stateLogicFailure, started)
return fmt.Errorf("delete pool %s is deny, pool is still active", netKey)
}
//clean active node
if _, err := srv.store.Delete(activepath); err != nil {
blog.Errorf("Delete pool %s active path err, %v", activepath, err)
reportMetrics("deletePool", stateLogicFailure, started)
return fmt.Errorf("delete pool %s active path err, %s", netKey, err.Error())
}
blog.Info("delete pool %s active path %s success", lockPath, activepath)
//clean reserved node
reservedpath := filepath.Join(defaultPoolInfoPath, netKey, "reserved")
if err := srv.cleanChildrenNode(reservedpath); err != nil {
blog.Errorf("delete pool %s reserved node err, %v", netKey, err)
reportMetrics("deletePool", stateLogicFailure, started)
return fmt.Errorf("delete pool %s reserved err, %s", netKey, err.Error())
}
//clean available node
availablepath := filepath.Join(defaultPoolInfoPath, netKey, "available")
if err := srv.cleanChildrenNode(availablepath); err != nil {
blog.Errorf("delete pool %s available node err, %v", netKey, err)
reportMetrics("deletePool", stateLogicFailure, started)
return fmt.Errorf("delete pool %s available err, %s", netKey, err.Error())
}
//clean host query node
hostpath := filepath.Join(defaultPoolInfoPath, netKey, "hosts")
if err := srv.cleanChildrenNode(hostpath); err != nil {
blog.Errorf("delete pool %s host node err, %v", netKey, err)
reportMetrics("deletePool", stateLogicFailure, started)
return fmt.Errorf("delete pool %s host err, %s", netKey, err.Error())
}
//clean pool node
if _, err := srv.store.Delete(poolPath); err != nil {
blog.Errorf("delete pool %s node err, %v", netKey, err)
reportMetrics("deletePool", stateLogicFailure, started)
return fmt.Errorf("delete pool %s err, %s", netKey, err.Error())
}
reportMetrics("deletePool", stateSuccess, started)
return nil
}
//UpdatePool update Pool info
func (srv *NetService) UpdatePool(pool *types.NetPool, netKey string) error {
started := time.Now()
//check & construct data
poolPath := filepath.Join(defaultPoolInfoPath, pool.GetKey())
blog.Infof("try to Update pool %s", poolPath)
//update netpool node with pool data
if exist, _ := srv.store.Exist(poolPath); !exist {
blog.Errorf("Pool %s is not exist, can not update", pool.GetKey())
reportMetrics("updatePool", stateNonExistFailure, started)
return fmt.Errorf("pool %s not exist, can not update", pool.GetKey())
}
if len(pool.Active) > 0 {
blog.Errorf("Error Update Request, can not update Pool %s with Active Info", pool.GetKey())
reportMetrics("updatePool", stateLogicFailure, started)
return fmt.Errorf("can not update pool %s with Active info", pool.GetKey())
}
//try to lock
lockPath := filepath.Join(defaultLockerPath, netKey)
distLocker, lockErr := srv.store.GetLocker(lockPath)
if lockErr != nil {
blog.Errorf("create pool %s lock err in delete pool, %v", netKey, lockErr)
reportMetrics("updatePool", stateLogicFailure, started)
return fmt.Errorf("create pool %s lock err, %v", lockPath, lockErr)
}
defer distLocker.Unlock()
if err := distLocker.Lock(); err != nil {
blog.Errorf("try to lock pool %s error, %v", netKey, err)
reportMetrics("updatePool", stateStorageFailure, started)
return fmt.Errorf("lock pool %s err, %v", netKey, err)
}
blog.Infof("Lock pool %s in update success.", lockPath)
//Get pool info
oldPool, poolErr := srv.ListPoolByKey(netKey)
if poolErr != nil {
blog.Errorf("get old pool %s info failed, %v", netKey, poolErr)
reportMetrics("updatePool", stateStorageFailure, started)
return fmt.Errorf("check old pool info failed, %v", poolErr)
}
//filter duplicated ips, pool is left data for add,
//oldPool is full data for update
if err := srv.filterDuplicateIP(pool, oldPool); err != nil {
blog.Errorf("filtDuplicateIP failed: %v", err)
reportMetrics("updatePool", stateLogicFailure, started)
return fmt.Errorf("filter info for %s failed, %v", netKey, err)
}
if len(pool.Available) == 0 && len(pool.Reserved) == 0 {
blog.Warnf("No IP address can be update for pool %s, skip update work flow", netKey)
reportMetrics("updatePool", stateSuccess, started)
return nil
}
//append Reserved info
for _, ip := range pool.Reserved {
inst := &types.IPInst{
IPAddr: ip,
Pool: pool.Net,
Mask: pool.Mask,
Gateway: pool.Gateway,
LastStatus: "created",
Status: types.IPStatus_RESERVED,
Update: time.Now().Format("2006-01-02 15:04:05"),
Cluster: pool.Cluster,
Container: "",
Host: "",
}
instData, _ := json.Marshal(inst)
instPath := filepath.Join(poolPath, "reserved", inst.GetKey())
if err := srv.store.Add(instPath, instData); err != nil {
blog.Errorf("Pool %s create reserved node %s err: %v", pool.GetKey(), inst.GetKey(), err)
reportMetrics("updatePool", stateStorageFailure, started)
return err
}
blog.Infof("Pool %s append reserved node %s success.", pool.GetKey(), inst.GetKey())
}
for _, ip := range pool.Available {
inst := &types.IPInst{
IPAddr: ip,
Pool: pool.Net,
Mask: pool.Mask,
Gateway: pool.Gateway,
LastStatus: "created",
Status: types.IPStatus_AVAILABLE,
Update: time.Now().Format("2006-01-02 15:04:05"),
Container: "",
Host: "",
}
instData, _ := json.Marshal(inst)
instPath := filepath.Join(poolPath, "available", inst.GetKey())
if err := srv.store.Add(instPath, instData); err != nil {
blog.Errorf("Pool %s create available node %s err: %v", pool.GetKey(), inst.GetKey(), err)
reportMetrics("updatePool", stateStorageFailure, started)
return err
}
blog.Infof("Pool %s append available node %s success.", pool.GetKey(), inst.GetKey())
}
//todo here create host node for query
for _, host := range pool.Hosts {
hostPath := filepath.Join(defaultHostInfoPath, host)
exist, eErr := srv.store.Exist(hostPath)
if eErr != nil {
blog.Errorf("Pool %s check host %s failed, %v", pool.GetKey(), hostPath, eErr)
reportMetrics("updatePool", stateStorageFailure, started)
return fmt.Errorf("ip append success, but host %s check failed, %v", host, eErr)
}
if exist {
blog.Errorf("Host %s exist, return", hostPath)
reportMetrics("updatePool", stateLogicFailure, started)
return fmt.Errorf("Host %s exist", host)
}
pHostPath := filepath.Join(poolPath, "hosts", host)
hostInfo := &types.HostInfo{
IPAddr: host,
Pool: pool.Net,
Cluster: pool.Cluster,
Created: time.Now().Format("2006-01-02 15:04:05"),
Update: time.Now().Format("2006-01-02 15:04:05"),
}
hostData, _ := json.Marshal(hostInfo)
if err := srv.store.Add(hostPath, hostData); err != nil {
blog.Errorf("Create Host Node %s err: %v", hostPath, err)
reportMetrics("updatePool", stateStorageFailure, started)
return err
}
if err := srv.store.Add(pHostPath, hostData); err != nil {
blog.Errorf("Pool %s append host %s err: %v", pool.GetKey(), host, err)
reportMetrics("updatePool", stateStorageFailure, started)
return err
}
blog.Infof("Create Host node %s under pool %s success", host, pool.GetKey())
}
//update pool node
newPoolData, _ := json.Marshal(oldPool)
if err := srv.store.Update(poolPath, newPoolData); err != nil {
blog.Errorf("Update Pool %s value failed:%v", pool.GetKey(), err)
reportMetrics("updatePool", stateStorageFailure, started)
return err
}
reportMetrics("updatePool", stateSuccess, started)
return nil
}
func (srv *NetService) filterDuplicateIP(update *types.NetPool, old *types.NetPool) error {
existIPs := make(map[string]bool)
for _, a := range old.Active {
existIPs[a] = true
}
for _, ava := range old.Available {
existIPs[ava] = true
}
for _, r := range old.Reserved {
existIPs[r] = true
}
existHosts := make(map[string]bool)
for _, h := range old.Hosts {
existHosts[h] = true
}
var hostList []string
for _, host := range update.Hosts {
if _, ok := existHosts[host]; ok {
blog.Warnf("Update pool %s host %s using now, skip", update.GetKey(), host)
continue
}
hostList = append(hostList, host)
}
update.Hosts = hostList
//check reserved ip info duplicated or not
var tempReservedList []string
for _, ip := range update.Reserved {
if _, ok := existIPs[ip]; ok {
blog.Warnf("Update pool %s ip %s in ReservedList are using now, skip", update.GetKey(), ip)
continue
}
old.Reserved = append(old.Reserved, ip)
tempReservedList = append(tempReservedList, ip)
}
//filter out reserved
update.Reserved = tempReservedList
var availableList []string
for _, ip := range update.Available {
if _, ok := existIPs[ip]; ok {
blog.Warnf("Update pool %s ip %s in Available List are using now, skip", update.GetKey(), ip)
continue
}
old.Available = append(old.Available, ip)
availableList = append(availableList, ip)
}
update.Available = availableList
return nil
}
//ListPool update Pool info
func (srv *NetService) ListPool() ([]*types.NetPool, error) {
started := time.Now()
//list all pool from defaultPoolPath
clusters, err := srv.store.List(defaultPoolInfoPath)
if err != nil {
blog.Errorf("list all net pool err, %v", err)
reportMetrics("updatePool", stateStorageFailure, started)
return nil, fmt.Errorf("list all pool err, %s", err.Error())
}
var pools []*types.NetPool
if len(clusters) == 0 {
blog.Info("No Cluseter here now")
reportMetrics("updatePool", stateSuccess, started)
return pools, nil
}
for _, c := range clusters {
list, err := srv.ListPoolByCluster(c)
if err != nil {
reportMetrics("updatePool", stateStorageFailure, started)
return nil, err
}
if len(list) == 0 {
continue
}
pools = append(pools, list...)
}
reportMetrics("updatePool", stateSuccess, started)
return pools, nil
}
//ListPoolByKey Get pool info by net
func (srv *NetService) ListPoolByKey(net string) (*types.NetPool, error) {
started := time.Now()
blog.Info("try to get pool %s info.", net)
poolpath := filepath.Join(defaultPoolInfoPath, net)
poolData, gerr := srv.store.Get(poolpath)
if gerr != nil {
blog.Errorf("Get pool %s by key err, %v", net, gerr)
reportMetrics("listPoolByKey", stateStorageFailure, started)
return nil, fmt.Errorf("get pool %s by key err, %s", net, gerr.Error())
}
pool := &types.NetPool{}
if err := json.Unmarshal(poolData, pool); err != nil {
blog.Errorf("Pool %s decode json data err: %v, origin data: %s", net, err, string(poolData))
reportMetrics("listPoolByKey", stateJSONFailure, started)
return nil, fmt.Errorf("decode pool %s data err, %s", net, err.Error())
}
//get hosts
hostpath := filepath.Join(defaultPoolInfoPath, net, "hosts")
hosts, err := srv.store.List(hostpath)
if err != nil {
blog.Errorf("list pool %s path %s failed, %v", net, hostpath, err)
reportMetrics("listPoolByKey", stateStorageFailure, started)
return nil, fmt.Errorf("list pool %s host path failed, %v", net, err)
}
pool.Hosts = hosts
//get reserved list
rpath := filepath.Join(defaultPoolInfoPath, net, "reserved")
reserved, err := srv.store.List(rpath)
if err != nil {
blog.Errorf("list pool %s path %s err, %v", net, rpath, err)
reportMetrics("listPoolByKey", stateStorageFailure, started)
return nil, fmt.Errorf("list pool %s reserved path err, %s", net, err.Error())
}
pool.Reserved = reserved
apath := filepath.Join(defaultPoolInfoPath, net, "available")
available, err := srv.store.List(apath)
if err != nil {
blog.Errorf("list pool %s path %s err, %v", net, apath, err)
reportMetrics("listPoolByKey", stateStorageFailure, started)
return nil, fmt.Errorf("list pool %s available path err, %s", net, err.Error())
}
pool.Available = available
activepath := filepath.Join(defaultPoolInfoPath, net, "active")
active, err := srv.store.List(activepath)
if err != nil {
blog.Errorf("list pool %s path %s err, %v", net, activepath, err)
reportMetrics("listPoolByKey", stateStorageFailure, started)
return nil, fmt.Errorf("list pool %s active path err, %s", net, err.Error())
}
pool.Active = active
blog.Info("Get pool %s info success", net)
reportMetrics("listPoolByKey", stateSuccess, started)
return pool, nil
}
//ListPoolByCluster list all pool under cluster
func (srv *NetService) ListPoolByCluster(cluster string) ([]*types.NetPool, error) {
started := time.Now()
//list all pool from defaultPoolPath
clusterpath := filepath.Join(defaultPoolInfoPath, cluster)
ok, err := srv.store.Exist(clusterpath)
if err != nil {
blog.Errorf("check cluster %s exist failed, %v", clusterpath, err)
reportMetrics("listPoolByCluster", stateStorageFailure, started)
return nil, err
}
if !ok {
blog.Warnf("####No Cluster %s creatd###", cluster)
reportMetrics("listPoolByCluster", stateSuccess, started)
return nil, nil
}
nets, err := srv.store.List(clusterpath)
if err != nil {
blog.Errorf("list all net pool under %s err, %v", cluster, err)
reportMetrics("listPoolByCluster", stateStorageFailure, started)
return nil, fmt.Errorf("list all pool err, %s", err.Error())
}
var pools []*types.NetPool
if len(nets) == 0 {
blog.Infof("No netpool under cluster %s now", cluster)
reportMetrics("listPoolByCluster", stateSuccess, started)
return pools, nil
}
for _, net := range nets {
pool, err := srv.ListPoolByKey(cluster + "/" + net)
if err != nil {
blog.Errorf("Get pool %s/%s info err, %v", cluster, net, err)
reportMetrics("listPoolByCluster", stateStorageFailure, started)
return nil, err
}
pools = append(pools, pool)
}
reportMetrics("listPoolByCluster", stateSuccess, started)
return pools, nil
}
//cleanChildrenNode clean all children node and node itself
func (srv *NetService) cleanChildrenNode(nodepath string) error {
nodeList, err := srv.store.List(nodepath)
if err != nil {
blog.Errorf("List %s all sub node err, %s", nodepath, err.Error())
return err
}
for _, node := range nodeList {
sub := filepath.Join(nodepath, node)
if _, err := srv.store.Delete(sub); err != nil {
blog.Errorf("Delete sub %s err, %v", sub, err)
return err
}
}
//clean self node
if _, err := srv.store.Delete(nodepath); err != nil {
blog.Errorf("Delete node %s err, %v", nodepath, err)
return err
}
return nil
}
//GetPoolAvailable get pool available ip list only
func (srv *NetService) GetPoolAvailable(net string) (*types.NetPool, error) {
started := time.Now()
blog.Info("try to get pool %s info.", net)
poolpath := filepath.Join(defaultPoolInfoPath, net)
poolData, getErr := srv.store.Get(poolpath)
if getErr != nil {
blog.Errorf("Get pool %s by key err, %v", net, getErr)
reportMetrics("getPoolAvailable", stateStorageFailure, started)
return nil, fmt.Errorf("get pool %s by key err, %s", net, getErr.Error())
}
pool := &types.NetPool{}
if err := json.Unmarshal(poolData, pool); err != nil {
blog.Errorf("Pool %s decode json data err: %v, origin data: %s", net, err, string(poolData))
reportMetrics("getPoolAvailable", stateJSONFailure, started)
return nil, fmt.Errorf("decode pool %s data err, %s", net, err.Error())
}
//get available node
apath := filepath.Join(defaultPoolInfoPath, net, "available")
available, err := srv.store.List(apath)
if err != nil {
blog.Errorf("list pool %s path %s err, %v", net, apath, err)
reportMetrics("getPoolAvailable", stateStorageFailure, started)
return nil, fmt.Errorf("list pool %s available path err, %s", net, err.Error())
}
pool.Available = available
reportMetrics("getPoolAvailable", stateSuccess, started)
return pool, nil
}