forked from akash-network/provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
617 lines (520 loc) · 16.3 KB
/
manager.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
package cluster
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/tendermint/tendermint/libs/log"
"github.com/boz/go-lifecycle"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
manifest "github.com/akash-network/node/manifest/v2beta1"
"github.com/akash-network/node/pubsub"
sdlutil "github.com/akash-network/node/sdl/util"
mtypes "github.com/akash-network/node/x/market/types/v1beta2"
kubeclienterrors "github.com/akash-network/provider/cluster/kube/errors"
clustertypes "github.com/akash-network/provider/cluster/types/v1beta2"
"github.com/akash-network/provider/cluster/util"
clusterutil "github.com/akash-network/provider/cluster/util"
"github.com/akash-network/provider/event"
"github.com/akash-network/provider/session"
"github.com/avast/retry-go"
)
const (
dsDeployActive deploymentState = "deploy-active"
dsDeployPending deploymentState = "deploy-pending"
dsDeployComplete deploymentState = "deploy-complete"
dsTeardownActive deploymentState = "teardown-active"
dsTeardownPending deploymentState = "teardown-pending"
dsTeardownComplete deploymentState = "teardown-complete"
)
const uncleanShutdownGracePeriod = 30 * time.Second
type deploymentState string
var (
deploymentCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "provider_deployment",
}, []string{"action", "result"})
monitorCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "provider_deployment_monitor",
}, []string{"action"})
ErrLeaseInactive = errors.New("inactive Lease")
)
type deploymentManager struct {
bus pubsub.Bus
client Client
session session.Session
state deploymentState
lease mtypes.LeaseID
mgroup *manifest.Group
monitor *deploymentMonitor
wg sync.WaitGroup
updatech chan *manifest.Group
teardownch chan struct{}
currentHostnames map[string]struct{}
log log.Logger
lc lifecycle.Lifecycle
hostnameService clustertypes.HostnameServiceClient
config Config
serviceShuttingDown <-chan struct{}
}
func newDeploymentManager(s *service, lease mtypes.LeaseID, mgroup *manifest.Group, isNewLease bool) *deploymentManager {
logger := s.log.With("cmp", "deployment-manager", "lease", lease, "manifest-group", mgroup.Name)
dm := &deploymentManager{
bus: s.bus,
client: s.client,
session: s.session,
state: dsDeployActive,
lease: lease,
mgroup: mgroup,
wg: sync.WaitGroup{},
updatech: make(chan *manifest.Group),
teardownch: make(chan struct{}),
log: logger,
lc: lifecycle.New(),
hostnameService: s.HostnameService(),
config: s.config,
serviceShuttingDown: s.lc.ShuttingDown(),
currentHostnames: make(map[string]struct{}),
}
go dm.lc.WatchChannel(s.lc.ShuttingDown())
go dm.run(context.Background())
go func() {
<-dm.lc.Done()
dm.log.Debug("sending manager into channel")
s.managerch <- dm
}()
err := s.bus.Publish(event.LeaseAddFundsMonitor{LeaseID: lease, IsNewLease: isNewLease})
if err != nil {
s.log.Error("unable to publish LeaseAddFundsMonitor event", "error", err, "lease", lease)
}
return dm
}
func (dm *deploymentManager) update(mgroup *manifest.Group) error {
select {
case dm.updatech <- mgroup:
return nil
case <-dm.lc.ShuttingDown():
return ErrNotRunning
}
}
func (dm *deploymentManager) teardown() error {
select {
case dm.teardownch <- struct{}{}:
return nil
case <-dm.lc.ShuttingDown():
return ErrNotRunning
}
}
func (dm *deploymentManager) handleUpdate(ctx context.Context) <-chan error {
switch dm.state {
case dsDeployActive:
dm.state = dsDeployPending
case dsDeployComplete:
// start update
return dm.startDeploy(ctx)
case dsDeployPending, dsTeardownActive, dsTeardownPending, dsTeardownComplete:
// do nothing
}
return nil
}
func (dm *deploymentManager) run(ctx context.Context) {
defer dm.lc.ShutdownCompleted()
var shutdownErr error
runch := dm.startDeploy(ctx)
defer func() {
err := dm.hostnameService.ReleaseHostnames(dm.lease)
if err != nil {
dm.log.Error("failed releasing hostnames", "err", err)
}
dm.log.Debug("hostnames released")
}()
var teardownErr error
loop:
for {
select {
case shutdownErr = <-dm.lc.ShutdownRequest():
break loop
case mgroup := <-dm.updatech:
dm.mgroup = mgroup
newch := dm.handleUpdate(ctx)
if newch != nil {
runch = newch
}
case result := <-runch:
runch = nil
if result != nil {
dm.log.Error("execution error", "state", dm.state, "err", result)
}
switch dm.state {
case dsDeployActive:
if result != nil {
// Run the teardown code to get rid of anything created that might be hanging out
runch = dm.startTeardown()
} else {
dm.log.Debug("deploy complete")
dm.state = dsDeployComplete
dm.startMonitor()
}
case dsDeployPending:
if result != nil {
break loop
}
// start update
runch = dm.startDeploy(ctx)
case dsDeployComplete:
panic(fmt.Sprintf("INVALID STATE: runch read on %v", dm.state))
case dsTeardownActive:
teardownErr = result
dm.state = dsTeardownComplete
dm.log.Debug("teardown complete")
break loop
case dsTeardownPending:
// start teardown
runch = dm.startTeardown()
case dsTeardownComplete:
panic(fmt.Sprintf("INVALID STATE: runch read on %v", dm.state))
}
case <-dm.teardownch:
dm.log.Debug("teardown request")
dm.stopMonitor()
switch dm.state {
case dsDeployActive:
dm.state = dsTeardownPending
case dsDeployPending:
dm.state = dsTeardownPending
case dsDeployComplete:
// start teardown
runch = dm.startTeardown()
case dsTeardownActive, dsTeardownPending, dsTeardownComplete:
}
}
}
dm.log.Debug("shutting down")
dm.lc.ShutdownInitiated(shutdownErr)
if runch != nil {
<-runch
dm.log.Debug("read from runch during shutdown")
}
dm.log.Debug("waiting on dm.wg")
dm.wg.Wait()
if dm.state != dsDeployComplete {
dm.log.Info("shutting down unclean, running teardown now")
ctx, cancel := context.WithTimeout(context.Background(), uncleanShutdownGracePeriod)
defer cancel()
teardownErr = dm.doTeardown(ctx)
}
if teardownErr != nil {
dm.log.Error("lease teardown failed", "err", teardownErr)
}
dm.log.Info("shutdown complete")
}
func (dm *deploymentManager) startMonitor() {
dm.wg.Add(1)
dm.monitor = newDeploymentMonitor(dm)
monitorCounter.WithLabelValues("start").Inc()
go func(m *deploymentMonitor) {
defer dm.wg.Done()
<-m.done()
}(dm.monitor)
}
func (dm *deploymentManager) stopMonitor() {
if dm.monitor != nil {
monitorCounter.WithLabelValues("stop").Inc()
dm.monitor.shutdown()
}
}
func (dm *deploymentManager) startDeploy(ctx context.Context) <-chan error {
dm.stopMonitor()
dm.state = dsDeployActive
chErr := make(chan error, 1)
go func() {
hostnames, endpoints, err := dm.doDeploy(ctx)
if err != nil {
chErr <- err
return
}
if len(hostnames) != 0 {
// Some hostnames have been withheld
dm.log.Info("hostnames withheld from deployment", "cnt", len(hostnames), "lease", dm.lease)
}
if len(endpoints) != 0 {
// Some endpoints have been withheld
dm.log.Info("endpoints withheld from deployment", "cnt", len(endpoints), "lease", dm.lease)
}
groupCopy := *dm.mgroup
ev := event.ClusterDeployment{
LeaseID: dm.lease,
Group: &groupCopy,
Status: event.ClusterDeploymentUpdated,
}
err = dm.bus.Publish(ev)
if err != nil {
dm.log.Error("failed publishing event", "err", err)
}
close(chErr)
}()
return chErr
}
func (dm *deploymentManager) startTeardown() <-chan error {
dm.stopMonitor()
dm.state = dsTeardownActive
return dm.do(func() error {
// Don't use a context tied to the lifecycle, as we don't want to cancel Kubernetes operations
return dm.doTeardown(context.Background())
})
}
type serviceExposeWithServiceName struct {
expose manifest.ServiceExpose
name string
}
func (dm *deploymentManager) doDeploy(ctx context.Context) ([]string, []string, error) {
cleanupHelper := newDeployCleanupHelper(dm.lease, dm.client, dm.log)
var err error
ctx, cancel := context.WithCancel(context.Background())
// Weird hack to tie this context to the lifecycle of the parent service, so this doesn't
// block forever or anything like that
go func() {
select {
case <-dm.serviceShuttingDown:
cancel()
case <-ctx.Done():
}
}()
defer func() {
// TODO - run on an isolated context
cleanupHelper.purgeAll(ctx)
cancel()
}()
if err = dm.checkLeaseActive(ctx); err != nil {
return nil, nil, err
}
currentIPs, err := dm.client.GetDeclaredIPs(ctx, dm.lease)
if err != nil {
return nil, nil, err
}
// Either reserve the hostnames, or confirm that they already are held
allHostnames := sdlutil.AllHostnamesOfManifestGroup(*dm.mgroup)
withheldHostnames, err := dm.hostnameService.ReserveHostnames(ctx, allHostnames, dm.lease)
if err != nil {
deploymentCounter.WithLabelValues("reserve-hostnames", "err").Inc()
dm.log.Error("deploy hostname reservation error", "state", dm.state, "err", err)
return nil, nil, err
}
deploymentCounter.WithLabelValues("reserve-hostnames", "success").Inc()
dm.log.Info("hostnames withheld", "cnt", len(withheldHostnames))
hostnamesInThisRequest := make(map[string]struct{})
for _, hostname := range allHostnames {
hostnamesInThisRequest[hostname] = struct{}{}
}
// Figure out what hostnames were removed from the manifest if any
for hostnameInUse := range dm.currentHostnames {
_, stillInUse := hostnamesInThisRequest[hostnameInUse]
if !stillInUse {
cleanupHelper.addHostname(hostnameInUse)
}
}
// Don't use a context tied to the lifecycle, as we don't want to cancel Kubernetes operations
deployCtx := util.ApplyToContext(context.Background(), dm.config.ClusterSettings)
err = dm.client.Deploy(deployCtx, dm.lease, dm.mgroup)
label := "success"
if err != nil {
label = "fail"
}
deploymentCounter.WithLabelValues("deploy", label).Inc()
// Figure out what hostnames to declare
blockedHostnames := make(map[string]struct{})
for _, hostname := range withheldHostnames {
blockedHostnames[hostname] = struct{}{}
}
hosts := make(map[string]manifest.ServiceExpose)
leasedIPs := make([]serviceExposeWithServiceName, 0)
hostToServiceName := make(map[string]string)
ipsInThisRequest := make(map[string]serviceExposeWithServiceName)
// clear this out so it gets repopulated
dm.currentHostnames = make(map[string]struct{})
// Iterate over each entry, extracting the ingress services & leased IPs
for _, service := range dm.mgroup.Services {
for _, expose := range service.Expose {
if sdlutil.ShouldBeIngress(expose) {
if dm.config.DeploymentIngressStaticHosts {
uid := sdlutil.IngressHost(dm.lease, service.Name)
host := fmt.Sprintf("%s.%s", uid, dm.config.DeploymentIngressDomain)
hosts[host] = expose
hostToServiceName[host] = service.Name
}
for _, host := range expose.Hosts {
_, blocked := blockedHostnames[host]
if !blocked {
dm.currentHostnames[host] = struct{}{}
hosts[host] = expose
hostToServiceName[host] = service.Name
}
}
}
if expose.Global && len(expose.IP) != 0 {
v := serviceExposeWithServiceName{expose: expose, name: service.Name}
leasedIPs = append(leasedIPs, v)
sharingKey := clusterutil.MakeIPSharingKey(dm.lease, expose.IP)
ipsInThisRequest[sharingKey] = v
}
}
}
for _, currentIP := range currentIPs {
// Check if the IP exists in the compute cluster but not in the presently used set of IPs
_, stillInUse := ipsInThisRequest[currentIP.SharingKey]
if !stillInUse {
proto, err := manifest.ParseServiceProtocol(currentIP.Protocol)
if err != nil {
return withheldHostnames, nil, err
}
cleanupHelper.addIP(currentIP.ServiceName, currentIP.ExternalPort, proto)
}
}
for host, serviceExpose := range hosts {
externalPort := uint32(sdlutil.ExposeExternalPort(serviceExpose))
err = dm.client.DeclareHostname(ctx, dm.lease, host, hostToServiceName[host], externalPort)
if err != nil {
// TODO - counter
return withheldHostnames, nil, err
}
}
withheldEndpoints := make([]string, 0)
for _, serviceExpose := range leasedIPs {
endpointName := serviceExpose.expose.IP
sharingKey := clusterutil.MakeIPSharingKey(dm.lease, endpointName)
externalPort := sdlutil.ExposeExternalPort(serviceExpose.expose)
port := serviceExpose.expose.Port
err = dm.client.DeclareIP(ctx, dm.lease, serviceExpose.name, uint32(port), uint32(externalPort), serviceExpose.expose.Proto, sharingKey, false)
if err != nil {
if !errors.Is(err, kubeclienterrors.ErrAlreadyExists) {
dm.log.Error("failed adding IP declaration", "service", serviceExpose.name, "port", externalPort, "endpoint", serviceExpose.expose.IP, "err", err)
return withheldHostnames, nil, err
}
dm.log.Info("IP declaration already exists", "service", serviceExpose.name, "port", externalPort, "endpoint", serviceExpose.expose.IP, "err", err)
withheldEndpoints = append(withheldEndpoints, sharingKey)
} else {
dm.log.Debug("added IP declaration", "service", serviceExpose.name, "port", externalPort, "endpoint", serviceExpose.expose.IP)
}
}
return withheldHostnames, withheldEndpoints, nil
}
func (dm *deploymentManager) getCleanupRetryOpts(ctx context.Context) []retry.Option {
retryFn := func(err error) bool {
isCanceled := errors.Is(err, context.Canceled)
isDeadlineExceeeded := errors.Is(err, context.DeadlineExceeded)
return !isCanceled && !isDeadlineExceeeded
}
return []retry.Option{
retry.Attempts(50),
retry.Delay(100 * time.Millisecond),
retry.MaxDelay(3000 * time.Millisecond),
retry.DelayType(retry.BackOffDelay),
retry.LastErrorOnly(true),
retry.RetryIf(retryFn),
retry.Context(ctx),
}
}
func (dm *deploymentManager) doTeardown(ctx context.Context) error {
const teardownActivityCount = 3
teardownResults := make(chan error, teardownActivityCount)
go func() {
result := retry.Do(func() error {
err := dm.client.TeardownLease(ctx, dm.lease)
if err != nil {
dm.log.Error("lease teardown failed", "err", err)
}
return err
}, dm.getCleanupRetryOpts(ctx)...)
label := "success"
if result != nil {
label = "fail"
}
deploymentCounter.WithLabelValues("teardown", label).Inc()
teardownResults <- result
}()
go func() {
result := retry.Do(func() error {
err := dm.client.PurgeDeclaredHostnames(ctx, dm.lease)
if err != nil {
dm.log.Error("purge declared hostname failure", "err", err)
}
return err
}, dm.getCleanupRetryOpts(ctx)...)
// TODO - counter
if result == nil {
dm.log.Debug("purged hostnames")
}
teardownResults <- result
}()
go func() {
result := retry.Do(func() error {
err := dm.client.PurgeDeclaredIPs(ctx, dm.lease)
if err != nil {
dm.log.Error("purge declared ips failure", "err", err)
}
return err
}, dm.getCleanupRetryOpts(ctx)...)
// TODO - counter
if result == nil {
dm.log.Debug("purged ips")
}
teardownResults <- result
}()
var firstError error
for i := 0; i != teardownActivityCount; i++ {
select {
case err := <-teardownResults:
if err != nil && firstError == nil {
firstError = err
}
case <-ctx.Done():
return ctx.Err()
}
}
return firstError
}
func (dm *deploymentManager) checkLeaseActive(ctx context.Context) error {
var lease *mtypes.QueryLeaseResponse
err := retry.Do(func() error {
var err error
lease, err = dm.session.Client().Query().Lease(ctx, &mtypes.QueryLeaseRequest{
ID: dm.lease,
})
if err != nil {
dm.log.Error("lease query failed", "err")
}
return err
},
retry.Attempts(50),
retry.Delay(100*time.Millisecond),
retry.MaxDelay(3000*time.Millisecond),
retry.DelayType(retry.BackOffDelay),
retry.LastErrorOnly(true))
if err != nil {
return err
}
if lease.GetLease().State != mtypes.LeaseActive {
dm.log.Error("lease not active, not deploying")
return fmt.Errorf("%w: %s", ErrLeaseInactive, dm.lease)
}
return nil
}
func (dm *deploymentManager) do(fn func() error) <-chan error {
ch := make(chan error, 1)
go func() {
ch <- fn()
}()
return ch
}
func TieContextToChannel(parentCtx context.Context, donech <-chan struct{}) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(parentCtx)
go func() {
select {
case <-donech:
cancel()
case <-ctx.Done():
}
}()
return ctx, cancel
}