forked from containerd/containerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
171 lines (142 loc) · 3.79 KB
/
runtime.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
// +build windows
package windows
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/runtime"
"github.com/containerd/containerd/typeurl"
"github.com/containerd/containerd/windows/hcs"
"github.com/containerd/containerd/windows/pid"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
const (
runtimeName = "windows"
owner = "containerd"
)
var _ = (runtime.Runtime)(&Runtime{})
func init() {
plugin.Register(&plugin.Registration{
ID: "windows",
Type: plugin.RuntimePlugin,
Init: New,
})
typeurl.Register(&RuntimeSpec{}, "windows/Spec")
}
func New(ic *plugin.InitContext) (interface{}, error) {
rootDir := filepath.Join(ic.Root)
if err := os.MkdirAll(rootDir, 0755); err != nil {
return nil, errors.Wrapf(err, "could not create state directory at %s", rootDir)
}
c, cancel := context.WithCancel(ic.Context)
r := &Runtime{
pidPool: pid.NewPool(),
containers: make(map[string]*container),
events: make(chan interface{}, 2048),
eventsContext: c,
eventsCancel: cancel,
rootDir: rootDir,
hcs: hcs.New(owner, rootDir),
}
// Terminate all previous container that we may have started. We don't
// support restoring containers
ctrs, err := loadContainers(ic.Context, r.hcs)
if err != nil {
return nil, err
}
for _, c := range ctrs {
c.ctr.Delete(ic.Context)
//r.sendEvent(c.ctr.ID(), events.RuntimeEvent_EXIT, c.ctr.Pid(), 255, time.Time{})
}
// Try to delete the old state dir and recreate it
stateDir := filepath.Join(ic.Root, "state")
if err := os.RemoveAll(stateDir); err != nil {
log.G(c).WithError(err).Warnf("failed to cleanup old state directory at %s", stateDir)
}
if err := os.MkdirAll(stateDir, 0755); err != nil {
return nil, errors.Wrapf(err, "could not create state directory at %s", stateDir)
}
r.stateDir = stateDir
return r, nil
}
type Runtime struct {
sync.Mutex
rootDir string
stateDir string
pidPool *pid.Pool
hcs *hcs.HCS
containers map[string]*container
events chan interface{}
eventsContext context.Context
eventsCancel func()
}
type RuntimeSpec struct {
// Spec is the OCI spec
OCISpec specs.Spec
// HCS specific options
hcs.Configuration
}
func (r *Runtime) ID() string {
return fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtimeName)
}
func (r *Runtime) Create(ctx context.Context, id string, opts runtime.CreateOpts) (runtime.Task, error) {
v, err := typeurl.UnmarshalAny(opts.Spec)
if err != nil {
return nil, err
}
rtSpec := v.(*RuntimeSpec)
ctr, err := newContainer(ctx, r.hcs, id, rtSpec, opts.IO)
if err != nil {
return nil, err
}
r.Lock()
r.containers[id] = ctr
r.Unlock()
return ctr, nil
}
func (r *Runtime) Delete(ctx context.Context, c runtime.Task) (*runtime.Exit, error) {
wc, ok := c.(*container)
if !ok {
return nil, fmt.Errorf("container cannot be cast as *windows.container")
}
ec, err := wc.ctr.ExitCode()
if err != nil {
log.G(ctx).WithError(err).Errorf("failed to retrieve exit code for container %s", wc.ctr.ID())
}
wc.ctr.Delete(ctx)
r.Lock()
delete(r.containers, wc.ctr.ID())
r.Unlock()
return &runtime.Exit{
Status: ec,
Timestamp: wc.ctr.Processes()[0].ExitedAt(),
}, nil
}
func (r *Runtime) Tasks(ctx context.Context) ([]runtime.Task, error) {
r.Lock()
defer r.Unlock()
list := make([]runtime.Task, len(r.containers))
for _, c := range r.containers {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
list = append(list, c)
}
}
return list, nil
}
func (r *Runtime) Get(ctx context.Context, id string) (runtime.Task, error) {
r.Lock()
defer r.Unlock()
c, ok := r.containers[id]
if !ok {
return nil, fmt.Errorf("container %s does not exit", id)
}
return c, nil
}