-
Notifications
You must be signed in to change notification settings - Fork 11
/
reload.go
136 lines (112 loc) · 2.87 KB
/
reload.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
package embedshim
import (
"context"
"io/ioutil"
"path/filepath"
pkgbundle "github.com/fuweid/embedshim/pkg/bundle"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/runtime"
)
func (manager *TaskManager) reloadExistingTasks(ctx context.Context) error {
nsDirs, err := ioutil.ReadDir(manager.stateDir)
if err != nil {
return err
}
for _, nsd := range nsDirs {
if !nsd.IsDir() {
continue
}
ns := nsd.Name()
if len(ns) > 0 && ns[0] == '.' {
continue
}
log.G(ctx).WithField("namespace", ns).Info("loading tasks in namespace")
if err := manager.loadTasks(namespaces.WithNamespace(ctx, ns)); err != nil {
log.G(ctx).WithField("namespace", ns).WithError(err).Error("loading tasks in namespace")
continue
}
}
return nil
}
func (manager *TaskManager) loadTasks(ctx context.Context) error {
ns, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return err
}
shimDirs, err := ioutil.ReadDir(filepath.Join(manager.stateDir, ns))
if err != nil {
return err
}
for _, sd := range shimDirs {
if !sd.IsDir() {
continue
}
id := sd.Name()
if len(id) > 0 && id[0] == '.' {
continue
}
bundle, err := pkgbundle.LoadBundle(manager.stateDir, ns, id)
if err != nil {
return err
}
// fast path
if err := bundle.IsValid(); err != nil {
log.G(ctx).WithError(err).Errorf("bundle %s is invalid, cleanup", bundle.Path)
bundle.Delete()
continue
}
shim, err := manager.loadShim(ctx, bundle)
if err != nil {
log.G(ctx).WithError(err).Errorf("failed to load exiting task %s", id)
bundle.Delete()
continue
}
if _, err := manager.containers.Get(ctx, id); err != nil {
log.G(ctx).WithError(err).Errorf("failed to load container %s and start to delete task", id)
shim.Delete(ctx)
continue
}
manager.tasks.Add(ctx, shim)
}
return nil
}
func (manager *TaskManager) loadShim(ctx context.Context, bundle *pkgbundle.Bundle) (_ *shim, retErr error) {
init, err := renewInitProcess(ctx, bundle)
if err != nil {
return nil, err
}
defer func() {
if retErr != nil {
deferCtx, deferCancel := deferContext()
defer deferCancel()
init.Delete(deferCtx)
manager.cleanInitProcessTraceEvent(init)
}
}()
if err := manager.repollingInitProcess(init); err != nil {
return nil, err
}
return renewShim(manager, init), nil
}
func renewShim(manager *TaskManager, init *initProcess) *shim {
s := &shim{
manager: manager,
bundle: init.bundle,
init: init,
execProcesses: make(map[string]runtime.Process),
reservedExecIDs: make(map[string]struct{}),
}
init.parent = s
return s
}
func renewInitProcess(ctx context.Context, bundle *pkgbundle.Bundle) (*initProcess, error) {
init, err := newInitProcess(bundle)
if err != nil {
return nil, err
}
if err = init.reload(ctx); err != nil {
return nil, err
}
return init, nil
}