-
Notifications
You must be signed in to change notification settings - Fork 26
/
pool.go
592 lines (506 loc) · 18.3 KB
/
pool.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
// Copyright 2014 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 facade
import (
"github.com/control-center/serviced/audit"
"github.com/control-center/serviced/datastore"
"github.com/control-center/serviced/domain/addressassignment"
"github.com/control-center/serviced/domain/host"
"github.com/control-center/serviced/domain/pool"
"github.com/control-center/serviced/validation"
"github.com/zenoss/glog"
"errors"
"fmt"
"time"
)
const (
beforePoolUpdate = beforeEvent("BeforePoolUpdate")
afterPoolUpdate = afterEvent("AfterPoolUpdate")
beforePoolAdd = beforeEvent("BeforePoolAdd")
afterPoolAdd = afterEvent("AfterPoolAdd")
beforePoolDelete = beforeEvent("BeforePoolDelete")
afterPoolDelete = afterEvent("AfterPoolDelete")
)
var (
ErrPoolExists = errors.New("facade: resource pool exists")
ErrPoolNotExists = errors.New("facade: resource pool does not exist")
ErrIPExists = errors.New("facade: ip exists in resource pool")
ErrIPNotExists = errors.New("facade: ip does not exist in resource pool")
ErrDefaultPool = errors.New("facade: cannot delete default resource pool")
)
// AddResourcePool adds a new resource pool
func (f *Facade) AddResourcePool(ctx datastore.Context, entity *pool.ResourcePool) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.AddResourcePool"))
glog.Infof("Adding Resource Pool %s", entity.ID)
alog := f.auditLogger.Message(ctx, "Adding Resource Pool").
Action(audit.Add).Entity(entity)
if err := f.DFSLock(ctx).LockWithTimeout("add resource pool", userLockTimeout); err != nil {
glog.Warningf("Cannot add resource pool: %s", err)
return alog.Error(err)
}
defer f.DFSLock(ctx).Unlock()
return alog.Error(f.addResourcePool(ctx, entity))
}
func (f *Facade) addResourcePool(ctx datastore.Context, entity *pool.ResourcePool) error {
if pool, err := f.GetResourcePool(ctx, entity.ID); err != nil {
return err
} else if pool != nil {
return ErrPoolExists
}
vips := entity.VirtualIPs
entity.VirtualIPs = []pool.VirtualIP{}
// TODO: Get rid of me when we have front-end functionality of pool realms
if entity.Realm == "" {
entity.Realm = defaultRealm
}
now := time.Now()
entity.CreatedAt = now
entity.UpdatedAt = now
// Add the pool
evtctx := newEventCtx()
err := f.beforeEvent(beforePoolAdd, evtctx, entity)
defer f.afterEvent(afterPoolAdd, evtctx, entity, err)
if err != nil {
return err
} else if err = f.poolStore.Put(ctx, pool.Key(entity.ID), entity); err != nil {
return err
} else if err = f.zzk.UpdateResourcePool(entity); err != nil {
return err
}
if vips != nil && len(vips) > 0 {
entity.VirtualIPs = vips
return f.updateResourcePool(ctx, entity)
}
f.poolCache.SetDirty()
return nil
}
// UpdateResourcePool updates an existing resource pool
func (f *Facade) UpdateResourcePool(ctx datastore.Context, entity *pool.ResourcePool) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.UpdateResourcePool"))
alog := f.auditLogger.Message(ctx, "Updating Resource Pool").
Action(audit.Update).Entity(entity)
if err := f.DFSLock(ctx).LockWithTimeout("update resource pool", userLockTimeout); err != nil {
glog.Warningf("Cannot update resource pool: %s", err)
return alog.Error(err)
}
defer f.DFSLock(ctx).Unlock()
return alog.Error(f.updateResourcePool(ctx, entity))
}
func (f *Facade) updateResourcePool(ctx datastore.Context, entity *pool.ResourcePool) error {
current, err := f.GetResourcePool(ctx, entity.ID)
if err != nil {
return err
} else if current == nil {
return ErrPoolNotExists
}
currentVIPs := make(map[string]pool.VirtualIP)
for _, vip := range current.VirtualIPs {
currentVIPs[vip.IP] = vip
}
vips := []pool.VirtualIP{}
// Add the virtual ips that do not already exist
for _, vip := range entity.VirtualIPs {
if _, ok := currentVIPs[vip.IP]; !ok {
if err := f.addVirtualIP(ctx, &vip); err != nil {
glog.Warningf("Could not add virtual ip %s: %s", vip.IP, err)
continue
}
} else {
delete(currentVIPs, vip.IP)
}
vips = append(vips, vip)
}
// Delete the remaining virtual ips
for _, vip := range currentVIPs {
if err := f.removeVirtualIP(ctx, vip.PoolID, vip.IP); err != nil {
glog.Warningf("Could not remove virtual ip %s: %s", vip.IP, err)
vips = append(vips, vip)
}
}
entity.VirtualIPs = vips
entity.UpdatedAt = time.Now()
evtctx := newEventCtx()
err = f.beforeEvent(beforePoolUpdate, evtctx, entity)
defer f.afterEvent(afterPoolUpdate, evtctx, entity, err)
if err != nil {
return err
} else if err = f.poolStore.Put(ctx, pool.Key(entity.ID), entity); err != nil {
return err
} else if err = f.zzk.UpdateResourcePool(entity); err != nil {
return err
}
// Update dfs permissions of the hosts that belong to the pool if
// the pool's dfs permissions have changed
if entity.HasDfsAccess() != current.HasDfsAccess() {
dfsClients, err := f.FindHostsInPool(ctx, entity.ID)
if err != nil {
msg := "Error retrieving pool's hosts"
plog.WithError(err).WithField("poolID", entity.ID).Warning(msg)
} else {
var clientsError error
if entity.HasDfsAccess() { // Enable dfs access
plog.WithField("poolID", entity.ID).Debug("DFS Access enabled for pool")
clientsError = f.zzk.RegisterDfsClients(dfsClients...)
} else { // Disable dfs access
plog.WithField("poolID", entity.ID).Debug("DFS Access disabled for pool")
clientsError = f.zzk.UnregisterDfsClients(dfsClients...)
}
if clientsError != nil {
msg := "Could not update dfs clients in zk after changing pool permissions"
plog.WithError(err).WithField("poolID", entity.ID).Warning(msg)
}
}
}
f.poolCache.SetDirty()
return nil
}
// RestoreResourcePools restores a bulk of resource pools, usually from a backup.
func (f *Facade) RestoreResourcePools(ctx datastore.Context, pools []pool.ResourcePool) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.RestoreResourcePools"))
// Do not DFSLock here, ControlPlaneDao does that
var alog audit.Logger
for _, pool := range pools {
alog = f.auditLogger.Message(ctx, "Adding ResourcePool").Action(audit.Add).Entity(&pool)
pool.SetPrimaryTerm(0)
pool.SetSeqNo(0)
if err := f.addResourcePool(ctx, &pool); err != nil {
if err == ErrPoolExists {
if err := f.updateResourcePool(ctx, &pool); err != nil {
glog.Errorf("Could not restore resource pool %s via update: %s", pool.ID, err)
return alog.Error(err)
}
} else {
glog.Errorf("Could not restore resource pool %s via add: %s", pool.ID, err)
return alog.Error(err)
}
}
alog.Succeeded()
}
return nil
}
// HasIP checks if a pool uses a particular IP address
func (f *Facade) HasIP(ctx datastore.Context, poolID string, ipAddr string) (bool, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.HasIP"))
if exists, err := f.poolStore.HasVirtualIP(ctx, poolID, ipAddr); err != nil {
glog.Errorf("Could not look up ip %s for pool %s: %s", ipAddr, poolID, err)
return false, err
} else if exists {
return true, nil
}
if host, err := f.GetHostByIP(ctx, ipAddr); err != nil {
glog.Errorf("Could not look up static host by ip %s: %s", ipAddr, err)
return false, err
} else if host != nil && host.PoolID == poolID {
return true, nil
}
return false, nil
}
// AddVirtualIP adds a virtual IP to a pool
func (f *Facade) AddVirtualIP(ctx datastore.Context, vip pool.VirtualIP) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.AddVirtualIP"))
entity, err := f.GetResourcePool(ctx, vip.PoolID)
alog := f.auditLogger.Message(ctx, "Adding VirtualIP").
Action(audit.Update).WithField("virtualip", vip.IP)
if err != nil {
return alog.Error(err)
} else if entity == nil {
return alog.Error(ErrPoolNotExists)
}
alog = alog.Entity(entity)
if err := f.addVirtualIP(ctx, &vip); err != nil {
return alog.Error(err)
}
entity.VirtualIPs = append(entity.VirtualIPs, vip)
entity.UpdatedAt = time.Now()
evtctx := newEventCtx()
err = f.beforeEvent(beforePoolUpdate, evtctx, entity)
defer f.afterEvent(afterPoolUpdate, evtctx, entity, err)
if err != nil {
return alog.Error(err)
} else if err = f.poolStore.Put(ctx, pool.Key(entity.ID), entity); err != nil {
return alog.Error(err)
} else if err = f.zzk.UpdateResourcePool(entity); err != nil {
return alog.Error(err)
}
alog.Succeeded()
return nil
}
func (f *Facade) addVirtualIP(ctx datastore.Context, vip *pool.VirtualIP) error {
pool, err := f.GetResourcePool(ctx, vip.PoolID)
if err != nil {
return err
} else if pool == nil {
return ErrPoolNotExists
}
if err := validation.IsIP(vip.IP); err != nil {
return err
} else if err := validation.IsIP(vip.Netmask); err != nil {
return err
} else if err := validation.NotEmpty("Bind Interface", vip.BindInterface); err != nil {
return err
} else if err := validation.ValidVirtualIP(vip.BindInterface); err != nil {
return err
}
if exists, err := f.HasIP(ctx, vip.PoolID, vip.IP); err != nil {
return err
} else if exists {
return ErrIPExists
}
return nil
}
// RemoveVirtualIP removes a virtual IP from a pool
func (f *Facade) RemoveVirtualIP(ctx datastore.Context, vip pool.VirtualIP) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.RemoveVirtualIP"))
alog := f.auditLogger.Message(ctx, "Removing VirtualIP").Action(audit.Update).
WithField("virtualip", vip.IP)
entity, err := f.GetResourcePool(ctx, vip.PoolID)
if err != nil {
return alog.Error(err)
} else if entity == nil {
return alog.Error(ErrPoolNotExists)
}
alog = alog.Entity(entity)
if err := f.removeVirtualIP(ctx, vip.PoolID, vip.IP); err != nil {
return alog.Error(err)
}
for i, currentVIP := range entity.VirtualIPs {
if currentVIP.IP == vip.IP {
entity.VirtualIPs = append(entity.VirtualIPs[:i], entity.VirtualIPs[i+1:]...)
break
}
}
// grab all services that are assigned to that virtual ip
query := []string{fmt.Sprintf("Endpoints.AddressAssignment.IPAddr:%s", vip.IP)}
services, err := f.GetTaggedServices(ctx, query)
if err != nil {
glog.Errorf("Failed to grab services with endpoints assigned to ip %s: %s", vip.IP, err)
return alog.Error(err)
}
evtctx := newEventCtx()
err = f.beforeEvent(beforePoolUpdate, evtctx, entity)
defer f.afterEvent(afterPoolUpdate, evtctx, entity, err)
if err != nil {
return alog.Error(err)
} else if err = f.poolStore.Put(ctx, pool.Key(entity.ID), entity); err != nil {
return alog.Error(err)
} else if err = f.zzk.UpdateResourcePool(entity); err != nil {
return alog.Error(err)
}
// update address assignments
for _, svc := range services {
request := addressassignment.AssignmentRequest{
ServiceID: svc.ID,
IPAddress: "",
AutoAssignment: true,
}
if err = f.AssignIPs(ctx, request); err != nil {
glog.Warningf("Failed assigning another ip to service %s: %s", svc.ID, err)
alog.Error(err)
}
}
alog.Succeeded()
return nil
}
func (f *Facade) removeVirtualIP(ctx datastore.Context, poolID, ipAddr string) error {
if exists, err := f.poolStore.HasVirtualIP(ctx, poolID, ipAddr); err != nil {
return err
} else if !exists {
return ErrIPNotExists
}
return nil
}
// RemoveResourcePool removes a resource pool
func (f *Facade) RemoveResourcePool(ctx datastore.Context, id string) error {
alog := f.auditLogger.
Message(ctx, "Removing Resource Pool").
Action(audit.Remove).ID(id).Type(pool.GetType())
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.RemoveResourcePool"))
glog.V(2).Infof("Facade.RemoveResourcePool: %s", id)
if err := f.DFSLock(ctx).LockWithTimeout("remove resource pool", userLockTimeout); err != nil {
glog.Warningf("Cannot remove resource pool: %s", err)
return alog.Error(err)
}
defer f.DFSLock(ctx).Unlock()
// CC-2024: do not delete the default resource pool
if id == "default" {
glog.Errorf("Cannot delete default resource pool")
return alog.Error(ErrDefaultPool)
}
if hosts, err := f.FindHostsInPool(ctx, id); err != nil {
return alog.Error(fmt.Errorf("could not verify hosts in pool %s: %s", id, err))
} else if count := len(hosts); count > 0 {
return alog.Error(fmt.Errorf("cannot delete pool %s: found %d hosts", id, count))
}
if svcs, err := f.GetServicesByPool(ctx, id); err != nil {
return alog.Error(fmt.Errorf("could not verify services in pool %s: %s", id, err))
} else if count := len(svcs); count > 0 {
return alog.Error(fmt.Errorf("cannot delete pool %s: found %d services", id, count))
}
if err := f.delete(ctx, f.poolStore, pool.Key(id), beforePoolDelete, afterPoolDelete); err != nil {
return alog.Error(err)
}
f.poolCache.SetDirty()
return alog.Error(f.zzk.RemoveResourcePool(id))
}
// GetResourcePools returns a list of all resource pools
func (f *Facade) GetResourcePools(ctx datastore.Context) ([]pool.ResourcePool, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetResourcePools"))
pools, err := f.poolStore.GetResourcePools(ctx)
if err != nil {
return nil, fmt.Errorf("Could not load pools: %v", err)
}
for i := range pools {
f.calcPoolCapacity(ctx, &pools[i])
f.calcPoolCommitment(ctx, &pools[i])
}
return pools, err
}
// GetResourcePoolsByRealm returns a list of all resource pools by Realm
func (f *Facade) GetResourcePoolsByRealm(ctx datastore.Context, realm string) ([]pool.ResourcePool, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetResourcePoolsByRealm"))
pools, err := f.poolStore.GetResourcePoolsByRealm(ctx, realm)
if err != nil {
return nil, fmt.Errorf("Could not load pools: %v", err)
}
for i := range pools {
f.calcPoolCapacity(ctx, &pools[i])
f.calcPoolCommitment(ctx, &pools[i])
}
return pools, err
}
// GetResourcePool returns a resource pool, or nil if not found
func (f *Facade) GetResourcePool(ctx datastore.Context, id string) (*pool.ResourcePool, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetResourcePool"))
glog.V(2).Infof("Facade.GetResourcePool: id=%s", id)
var entity pool.ResourcePool
err := f.poolStore.Get(ctx, pool.Key(id), &entity)
if datastore.IsErrNoSuchEntity(err) {
return nil, nil
}
if err != nil {
return nil, err
}
f.calcPoolCapacity(ctx, &entity)
return &entity, nil
}
// CreateDefaultPool creates the default pool if it does not exist. It is idempotent.
func (f *Facade) CreateDefaultPool(ctx datastore.Context, id string) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.CreateDefaultPool"))
alog := f.auditLogger.Message(ctx, "Adding ResourcePool").
ID(id).Type(pool.GetType()).Action(audit.Add)
entity, err := f.GetResourcePool(ctx, id)
if err != nil {
alog.Error(err)
return fmt.Errorf("could not create default pool %s: %v", id, err)
}
if entity != nil {
glog.V(4).Infof("'%s' resource pool already exists", id)
alog.Succeeded()
return nil
}
glog.V(4).Infof("'%s' resource pool not found; creating...", id)
entity = pool.New(id)
entity.Realm = defaultRealm
entity.Description = "Default Pool"
entity.Permissions = pool.DFSAccess + pool.AdminAccess
if err := f.AddResourcePool(ctx, entity); err != nil {
return alog.Error(err)
}
alog.Succeeded()
return nil
}
func (f *Facade) calcPoolCapacity(ctx datastore.Context, pool *pool.ResourcePool) error {
hosts, err := f.hostStore.FindHostsWithPoolID(ctx, pool.ID)
if err != nil {
glog.Errorf("Unable to find hosts in %s: %v", pool.ID, err)
return err
}
coreCapacity := 0
memCapacity := uint64(0)
for _, host := range hosts {
coreCapacity = coreCapacity + host.Cores
memCapacity = memCapacity + host.Memory
}
pool.CoreCapacity = coreCapacity
pool.MemoryCapacity = memCapacity
return nil
}
func (f *Facade) calcPoolCommitment(ctx datastore.Context, pool *pool.ResourcePool) error {
services, err := f.serviceStore.GetServicesByPool(ctx, pool.ID)
if err != nil {
glog.Errorf("Unable to find services on %s: %v", pool.ID, err)
return err
}
memCommitment := uint64(0)
for _, service := range services {
memCommitment = memCommitment + service.RAMCommitment.Value
}
pool.MemoryCommitment = memCommitment
return nil
}
// GetPoolIPs gets all IPs available to a resource pool
func (f *Facade) GetPoolIPs(ctx datastore.Context, poolID string) (*pool.PoolIPs, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetPoolIPs"))
glog.V(0).Infof("Facade.GetPoolIPs: %+v", poolID)
hosts, err := f.FindHostsInPool(ctx, poolID)
if err != nil {
return nil, err
}
glog.V(0).Infof("Facade.GetPoolIPs: found hosts %v", hosts)
// save off the static IP addresses
hostIPs := make([]host.HostIPResource, 0)
for _, h := range hosts {
hostIPs = append(hostIPs, h.IPs...)
}
// save off the virtual IP addresses
myPool, err := f.GetResourcePool(ctx, poolID)
if err != nil {
glog.Errorf("Unable to load resource pool: %v", poolID)
return nil, err
} else if myPool == nil {
msg := fmt.Sprintf("Pool ID: %v could not be found", poolID)
return nil, errors.New(msg)
}
virtualIPs := make([]pool.VirtualIP, 0)
virtualIPs = append(virtualIPs, myPool.VirtualIPs...)
return &pool.PoolIPs{PoolID: poolID, HostIPs: hostIPs, VirtualIPs: virtualIPs}, nil
}
var defaultRealm = "default"
// GetReadPools returns a list of simplified resource pools
func (f *Facade) GetReadPools(ctx datastore.Context) ([]pool.ReadPool, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetReadPools"))
var getPoolsFunc GetPoolsFunc = func() ([]pool.ReadPool, error) {
pools, err := f.poolStore.GetResourcePools(ctx)
if err != nil {
return nil, fmt.Errorf("Could not load pools: %v", err)
}
readPools := []pool.ReadPool{}
for i := range pools {
f.calcPoolCapacity(ctx, &pools[i])
f.calcPoolCommitment(ctx, &pools[i])
readPools = append(readPools, pool.ReadPool{
ID: pools[i].ID,
Description: pools[i].Description,
CreatedAt: pools[i].CreatedAt,
UpdatedAt: pools[i].UpdatedAt,
CoreCapacity: pools[i].CoreCapacity,
MemoryCapacity: pools[i].MemoryCapacity,
MemoryCommitment: pools[i].MemoryCommitment,
ConnectionTimeout: pools[i].ConnectionTimeout,
Permissions: pools[i].Permissions,
})
}
return readPools, nil
}
return f.poolCache.GetPools(getPoolsFunc)
}