This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathdecommission.go
More file actions
625 lines (539 loc) · 13.6 KB
/
Copy pathdecommission.go
File metadata and controls
625 lines (539 loc) · 13.6 KB
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
package pod
import (
"fmt"
"os"
"path"
"syscall"
"time"
dockertypes "github.com/docker/engine-api/types"
"github.com/hyperhq/hyperd/utils"
vc "github.com/kata-containers/runtime/virtcontainers"
)
type sandboxOp func(sb *vc.Sandbox) error
type stateValidator func(state PodState) bool
func (p *XPod) DelayDeleteOn() bool {
return false
}
func (p *XPod) Stop(graceful int) error {
err := p.doStopPod(graceful)
if err != nil {
return err
}
p.Log(DEBUG, "pod stopped, now wait cleanup")
if cleanup := p.waitStopDone(graceful, "stop container"); !cleanup {
p.Log(WARNING, "timeout while wait cleanup pod")
return fmt.Errorf("did not finish clean up in %d seconds", graceful)
}
return nil
}
func (p *XPod) ForceQuit() {
err := p.protectedSandboxOperation(
func(sb *vc.Sandbox) error {
_, err := vc.StopSandbox(sb.ID())
return err
},
time.Second*5,
"kill pod")
if err != nil {
p.Log(ERROR, "force quit failed: %v", err)
}
}
func (p *XPod) Remove(force bool) error {
if p.IsRunning() {
if !force {
err := fmt.Errorf("pod is running, cannot be removed")
p.Log(ERROR, err)
return err
}
p.Log(DEBUG, "stop pod before remove")
p.doStopPod(10)
if cleanup := p.waitStopDone(60, "Remove Pod"); !cleanup {
p.Log(WARNING, "timeout while waiting pod stopped")
}
}
p.resourceLock.Lock()
defer p.resourceLock.Unlock()
p.Log(INFO, "removing pod")
p.statusLock.Lock()
p.status = S_POD_NONE
p.statusLock.Unlock()
os.RemoveAll(path.Join(utils.HYPER_ROOT, "hosts", p.Id()))
for id, c := range p.containers {
p.factory.registry.ReleaseContainer(id, c.SpecName())
p.factory.engine.ContainerRm(id, &dockertypes.ContainerRmConfig{false, false, false})
}
//remove pod(including all containers/volumes/interfaces) in daemondb
p.removeFromDB()
if p.DelayDeleteOn() {
p.Log(DEBUG, "should wait periodical clean up")
p.factory.registry.Release(p.Id())
return nil
}
return nil
}
func (p *XPod) Dissociate() error {
p.resourceLock.Lock()
defer p.resourceLock.Unlock()
err := dissociateSandbox(p.sandbox, 0)
p.factory.registry.Release(p.Id())
for _, c := range p.containers {
p.factory.registry.ReleaseContainer(c.Id(), c.SpecName())
}
if err != nil {
p.Log(ERROR, "failed to release vm: %v", err)
return err
}
return nil
}
func (p *XPod) Pause() error {
p.statusLock.Lock()
if p.status != S_POD_RUNNING {
p.statusLock.Unlock()
err := fmt.Errorf("pause: pod state is not valid: %v", p.status)
p.Log(ERROR, err)
return err
}
p.status = S_POD_PAUSED
p.statusLock.Unlock()
err := p.protectedSandboxOperation(
func(sb *vc.Sandbox) error {
return sb.Pause()
},
time.Second*5,
"pause pod")
if err != nil {
p.Log(WARNING, "pause: roll back status from %v because of: %v", p.status, err)
p.statusLock.Lock()
if p.status == S_POD_PAUSED {
p.status = S_POD_RUNNING
}
p.statusLock.Unlock()
}
return err
}
func (p *XPod) UnPause() error {
p.statusLock.Lock()
if p.status != S_POD_PAUSED {
p.statusLock.Unlock()
err := fmt.Errorf("unpause: pod state is not valid: %v", p.status)
p.Log(ERROR, err)
return err
}
p.status = S_POD_RUNNING
p.statusLock.Unlock()
err := p.protectedSandboxOperation(
func(sb *vc.Sandbox) error {
return sb.Pause()
},
time.Second*5,
"resume pod")
if err != nil {
//TODO, looks not safe we just rollback status here, should we shutdown if unpause failed?
p.Log(WARNING, "pause: roll back status from %v because of %v", p.status, err)
p.statusLock.Lock()
if p.status == S_POD_RUNNING {
p.status = S_POD_PAUSED
}
p.statusLock.Unlock()
}
return err
}
func (p *XPod) KillContainer(id string, sig int64) error {
c, ok := p.containers[id]
if !ok {
err := fmt.Errorf("pod does not have a container %s", id)
p.Log(ERROR, err)
return err
}
c.setKill()
return p.protectedSandboxOperation(
func(sb *vc.Sandbox) error {
return vc.KillContainer(sb.ID(), id, syscall.Signal(sig), true)
},
time.Second*5,
fmt.Sprintf("Kill container %s with %d", id, sig))
}
func (p *XPod) StopContainer(id string, graceful int) error {
p.resourceLock.Lock()
defer p.resourceLock.Unlock()
if !p.IsRunning() {
err := fmt.Errorf("pod is not running, cannot stop container")
p.Log(ERROR, err)
return err
}
_, ok := p.containers[id]
if !ok {
err := fmt.Errorf("pod does not have a container %s", id)
p.Log(ERROR, err)
return err
}
err := p.stopContainers([]string{id}, graceful)
if err != nil {
p.Log(ERROR, "fail during stop container %s: %v", id, err)
}
return err
}
func (p *XPod) RemoveContainer(id string) error {
p.resourceLock.Lock()
defer p.resourceLock.Unlock()
c, ok := p.containers[id]
if !ok {
err := fmt.Errorf("container %s not found", id)
p.Log(WARNING, err)
return nil
}
var (
err error
)
defer func() {
if err != nil {
if err != nil {
p.statusLock.Lock()
p.status = S_POD_ERROR
p.statusLock.Unlock()
}
}
}()
cvols := c.volumes()
removedVols := make([]string, 0, len(cvols))
if p.IsRunning() {
if c.IsRunning() {
err = p.stopContainers([]string{id}, 5)
if err != nil {
p.Log(ERROR, "fail during stop container %s: %v", id, err)
return err
}
}
err = c.removeFromSandbox()
if err != nil {
c.Log(ERROR, "failed to remove from sandbox")
return err
}
for _, cv := range cvols {
if v, ok := p.volumes[cv.Name]; ok {
removed, err := v.tryRemoveFromSandbox()
if err != nil {
v.Log(ERROR, "failed to unplug vol: %v", err)
continue
}
if !removed {
v.Log(DEBUG, "volume did not unplug because it is in use")
continue
}
v.Log(DEBUG, "volume unplugged")
removedVols = append(removedVols, cv.Name)
}
}
err = c.umountRootVol()
if err != nil {
c.Log(ERROR, "failed to umount root volume")
return err
}
}
err = p.factory.engine.ContainerRm(id, &dockertypes.ContainerRmConfig{})
if err != nil {
c.Log(ERROR, "failed to remove container through engine")
return err
}
p.factory.registry.ReleaseContainer(id, c.SpecName())
delete(p.containers, id)
p.statusLock.Lock()
delete(p.snapContainers, id)
p.statusLock.Unlock()
//remove volumes from daemondb
for _, vName := range removedVols {
if v, ok := p.volumes[vName]; ok {
if err = v.removeFromDB(); err != nil {
return err
}
}
}
// remove container in daemondb.
if err = c.removeFromDB(); err != nil {
return err
}
// update layout to remove container from pod layout.
if err = p.saveLayout(); err != nil {
return err
}
// save sandbox change
if err = p.saveSandbox(); err != nil {
return err
}
return nil
}
// protectedSandboxOperation() protect the hypervisor operations, which may
// panic or hang too long time.
func (p *XPod) protectedSandboxOperation(op sandboxOp, timeout time.Duration, comment string) error {
dangerousOp := func(sb *vc.Sandbox, errChan chan<- error) {
defer func() {
err := recover()
if err != nil {
p.Log(ERROR, err)
if re, ok := err.(error); ok {
errChan <- re
} else {
errChan <- fmt.Errorf("%v", err)
}
}
}()
if sb != nil {
errChan <- op(sb)
} else {
p.Log(WARNING, "%s: sandbox not existed", comment)
errChan <- nil
}
}
errChan := make(chan error, 1)
p.statusLock.Lock()
go dangerousOp(p.sandbox, errChan)
p.statusLock.Unlock()
var timeoutChan <-chan time.Time
if timeout < 0 {
timeoutChan = make(chan time.Time, 1)
} else {
timeoutChan = time.After(timeout)
}
select {
case err, ok := <-errChan:
if !ok {
err := fmt.Errorf("%s: failed to get operation result", comment)
p.Log(ERROR, err)
return err
}
return err
case <-timeoutChan:
err := fmt.Errorf("%s: timeout (%v) during waiting operation result", comment, timeout)
p.Log(ERROR, err)
return err
}
}
func (p *XPod) doStopPod(graceful int) error {
var err error
p.statusLock.Lock()
if p.status != S_POD_RUNNING && p.status != S_POD_STARTING {
err = fmt.Errorf("only alived pod could be stopped, current %d", p.status)
} else {
p.status = S_POD_STOPPING
}
p.statusLock.Unlock()
if err != nil {
p.Log(ERROR, err)
return err
}
p.Log(INFO, "going to stop pod")
//lock all resource action of the pod, but don't block list/read query
p.resourceLock.Lock()
defer p.resourceLock.Unlock()
//whatever the result of stop container, go on shutdown vm
err = p.stopAllContainers(graceful)
if err != nil {
p.Log(INFO, "stop container failed, force quit sandbox")
p.ForceQuit()
return nil
}
for cid, c := range p.containers {
err = c.removeFromSandbox()
if err != nil {
c.Log(ERROR, "failed to remove %s from sandbox", cid)
}
}
p.Log(INFO, "stop container success, shutdown sandbox")
_, err = vc.StopSandbox(p.sandbox.ID())
if err == nil {
p.Log(INFO, "pod is stopped")
return nil
}
err = fmt.Errorf("failed to shuting down: %s", err)
p.Log(ERROR, err)
return err
}
func (p *XPod) stopAllContainers(graceful int) error {
//wait all, fire stop signal, and check status,
if len(p.containers) == 0 {
p.Log(DEBUG, "no container to be stopped")
return nil
}
var (
cList = make([]string, 0, len(p.containers))
)
for cid := range p.containers {
cList = append(cList, cid)
}
err := p.stopContainers(cList, graceful)
if err != nil {
p.Log(ERROR, "exception during stop all containers: %v", err)
}
return err
}
func (p *XPod) stopContainers(cList []string, graceful int) error {
p.Log(INFO, "begin stop containers %s", cList)
future := utils.NewFutureSet()
waitTime := time.Duration(graceful) * time.Second
if graceful == 0 {
// set default waiting time to 5s
waitTime = time.Duration(5) * time.Second
}
for _, cid := range cList {
c, ok := p.containers[cid]
if !ok {
p.Log(WARNING, "no container %s to be stopped", cid)
continue
}
if !c.IsRunning() {
c.Log(DEBUG, "container state %v is not running(3), ignore during stop", c.CurrentState())
continue
}
future.Add(c.Id(), func() error {
var toc <-chan time.Time
var retch = make(chan int32)
if int64(graceful) < 0 {
toc = make(chan time.Time)
} else {
toc = time.After(waitTime)
}
forceKill := graceful == 0
go func(retch chan int32, c *Container) {
ret, _ := p.sandbox.WaitProcess(c.Id(), c.Id())
retch <- ret
}(retch, c)
c.Log(DEBUG, "now, stop container")
err := c.terminate(forceKill)
// TODO filter container/process can't find error
if err != nil && !forceKill {
forceKill = true
if err = c.terminate(true); err != nil {
return err
}
}
for {
select {
case ret := <-retch:
p.Log(DEBUG, "container %s stopped (%d)", c.Id(), ret)
return nil
case <-toc:
if forceKill {
return fmt.Errorf("timeout for killing container %s", c.Id())
}
c.Log(DEBUG, "kill container with default signal failed, try SIGKILL")
forceKill = true
toc = time.After(time.Duration(graceful) * time.Second)
// TODO filter container/process can't find error
if err = c.terminate(true); err != nil {
return err
}
}
}
return nil
})
}
err := future.Wait(waitTime * 2)
if err != nil {
return err
}
p.Log(INFO, "complete stop containers %s", cList)
return nil
}
func (p *XPod) waitStopDone(timeout int, comments string) bool {
select {
case s, ok := <-p.stoppedChan:
if ok {
p.Log(DEBUG, "got stop msg and push it again: %s", comments)
select {
case p.stoppedChan <- s:
default:
}
}
p.Log(DEBUG, "wait stop done: %s", comments)
return true
case <-utils.Timeout(timeout):
p.Log(DEBUG, "wait stop timeout: %s", comments)
return false
}
}
// waitVMStop() should only be call for the life monitoring, others should wait the `waitStopDone`
func (p *XPod) waitVMStop() {
p.statusLock.RLock()
if p.status == S_POD_STOPPED {
p.statusLock.RUnlock()
return
}
p.statusLock.RUnlock()
monitor, _ := p.sandbox.Monitor()
_ = <-monitor
p.Log(INFO, "got vm exit event")
p.cleanup()
}
//cleanup is used to cleanup the resource after VM shutdown. This method should only called by waitVMStop
func (p *XPod) cleanup() {
//if removing, the remove will get the resourceLock in advance, and it will set
// the pod status to NONE when it complete.
// Therefore, if get into cleanup() after remove, cleanup should exit when it
// got a
p.resourceLock.Lock()
defer p.resourceLock.Unlock()
p.statusLock.Lock()
if p.status == S_POD_STOPPED || p.status == S_POD_NONE {
p.statusLock.Unlock()
return
} else {
p.status = S_POD_STOPPING
}
p.statusLock.Unlock()
err := p.decommissionResources()
if err != nil {
// even if error, we set the vm to be stopped
p.Log(ERROR, "pod stopping failed, failed to decommit the resources: %v", err)
err = nil
}
err = p.removeSandboxFromDB()
if err != nil {
p.Log(ERROR, "pod stopping failed, failed to remove sandbox persist data: %v", err)
err = nil
}
p.Log(DEBUG, "tag pod as stopped")
p.statusLock.Lock()
if p.status != S_POD_NONE {
p.status = S_POD_STOPPED
}
p.statusLock.Unlock()
p.Log(INFO, "pod stopped")
select {
case p.stoppedChan <- true:
default:
}
}
func (p *XPod) decommissionResources() (err error) {
p.Log(DEBUG, "umount all containers and volumes, release IP addresses")
err = p.flushPortMapping()
if err != nil {
p.Log(WARNING, "(ignored) flush port mappings failed: %v", err)
err = nil
}
for _, c := range p.containers {
ec := c.umountRootVol()
if ec != nil {
err = ec
c.Log(ERROR, err)
}
}
for _, v := range p.volumes {
ev := v.umount()
if ev != nil {
err = ev
v.Log(ERROR, err)
}
}
for _, n := range p.interfaces {
ei := n.cleanup()
if ei != nil {
err = ei
n.Log(ERROR, err)
}
}
p.sandbox = nil
cleanupHosts(p.Id())
// then it could be start again.
p.factory.hosts = HostsCreator(p.Id())
return err
}