-
Notifications
You must be signed in to change notification settings - Fork 239
/
container.go
262 lines (228 loc) · 7.85 KB
/
container.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
package gcs
import (
"context"
"errors"
"sync"
"time"
"github.com/Microsoft/hcsshim/internal/cow"
"github.com/Microsoft/hcsshim/internal/hcs/schema1"
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/oc"
"go.opencensus.io/trace"
)
const hrComputeSystemDoesNotExist = 0xc037010e
// Container implements the cow.Container interface for containers
// created via GuestConnection.
type Container struct {
gc *GuestConnection
id string
notifyCh chan struct{}
closeCh chan struct{}
closeOnce sync.Once
// waitBlock is the channel used to wait for container shutdown or termination
waitBlock chan struct{}
// waitError indicates the container termination error if any
waitError error
}
var _ cow.Container = &Container{}
// CreateContainer creates a container using ID `cid` and `cfg`. The request
// will likely not be cancellable even if `ctx` becomes done.
func (gc *GuestConnection) CreateContainer(ctx context.Context, cid string, config interface{}) (_ *Container, err error) {
ctx, span := trace.StartSpan(ctx, "gcs::GuestConnection::CreateContainer")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", cid))
c := &Container{
gc: gc,
id: cid,
notifyCh: make(chan struct{}),
closeCh: make(chan struct{}),
waitBlock: make(chan struct{}),
}
err = gc.requestNotify(cid, c.notifyCh)
if err != nil {
return nil, err
}
req := containerCreate{
requestBase: makeRequest(ctx, cid),
ContainerConfig: anyInString{config},
}
var resp containerCreateResponse
err = gc.brdg.RPC(ctx, rpcCreate, &req, &resp, false)
if err != nil {
return nil, err
}
go c.waitBackground()
return c, nil
}
// CloneContainer just creates the wrappers and sets up notification requests for a
// container that is already running inside the UVM (after cloning).
func (gc *GuestConnection) CloneContainer(ctx context.Context, cid string) (_ *Container, err error) {
c := &Container{
gc: gc,
id: cid,
notifyCh: make(chan struct{}),
closeCh: make(chan struct{}),
waitBlock: make(chan struct{}),
}
err = gc.requestNotify(cid, c.notifyCh)
if err != nil {
return nil, err
}
go c.waitBackground()
return c, nil
}
// OS returns the operating system of the container, "linux" or "windows".
func (c *Container) OS() string {
return c.gc.os
}
// IsOCI specifies whether CreateProcess should be called with an OCI
// specification in its input.
func (c *Container) IsOCI() bool {
return c.gc.os != "windows"
}
// Close releases associated with the container.
func (c *Container) Close() error {
c.closeOnce.Do(func() {
_, span := trace.StartSpan(context.Background(), "gcs::Container::Close")
defer span.End()
span.AddAttributes(trace.StringAttribute("cid", c.id))
close(c.closeCh)
})
return nil
}
// CreateProcess creates a process in the container.
func (c *Container) CreateProcess(ctx context.Context, config interface{}) (_ cow.Process, err error) {
ctx, span := trace.StartSpan(ctx, "gcs::Container::CreateProcess")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", c.id))
return c.gc.exec(ctx, c.id, config)
}
// ID returns the container's ID.
func (c *Container) ID() string {
return c.id
}
// Modify sends a modify request to the container.
func (c *Container) Modify(ctx context.Context, config interface{}) (err error) {
ctx, span := trace.StartSpan(ctx, "gcs::Container::Modify")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", c.id))
req := containerModifySettings{
requestBase: makeRequest(ctx, c.id),
Request: config,
}
var resp responseBase
return c.gc.brdg.RPC(ctx, rpcModifySettings, &req, &resp, false)
}
// Properties returns the requested container properties targeting a V1 schema container.
func (c *Container) Properties(ctx context.Context, types ...schema1.PropertyType) (_ *schema1.ContainerProperties, err error) {
ctx, span := trace.StartSpan(ctx, "gcs::Container::Properties")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", c.id))
req := containerGetProperties{
requestBase: makeRequest(ctx, c.id),
Query: containerPropertiesQuery{PropertyTypes: types},
}
var resp containerGetPropertiesResponse
err = c.gc.brdg.RPC(ctx, rpcGetProperties, &req, &resp, true)
if err != nil {
return nil, err
}
return (*schema1.ContainerProperties)(&resp.Properties), nil
}
// PropertiesV2 returns the requested container properties targeting a V2 schema container.
func (c *Container) PropertiesV2(ctx context.Context, types ...hcsschema.PropertyType) (_ *hcsschema.Properties, err error) {
ctx, span := trace.StartSpan(ctx, "gcs::Container::PropertiesV2")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", c.id))
req := containerGetPropertiesV2{
requestBase: makeRequest(ctx, c.id),
Query: containerPropertiesQueryV2{PropertyTypes: types},
}
var resp containerGetPropertiesResponseV2
err = c.gc.brdg.RPC(ctx, rpcGetProperties, &req, &resp, true)
if err != nil {
return nil, err
}
return (*hcsschema.Properties)(&resp.Properties), nil
}
// Start starts the container.
func (c *Container) Start(ctx context.Context) (err error) {
ctx, span := trace.StartSpan(ctx, "gcs::Container::Start")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", c.id))
req := makeRequest(ctx, c.id)
var resp responseBase
return c.gc.brdg.RPC(ctx, rpcStart, &req, &resp, false)
}
func (c *Container) shutdown(ctx context.Context, proc rpcProc) error {
req := makeRequest(ctx, c.id)
var resp responseBase
err := c.gc.brdg.RPC(ctx, proc, &req, &resp, true)
if err != nil {
if uint32(resp.Result) != hrComputeSystemDoesNotExist {
return err
}
select {
case <-c.notifyCh:
default:
log.G(ctx).WithError(err).Warn("ignoring missing container")
}
}
return nil
}
// Shutdown sends a graceful shutdown request to the container. The container
// might not be terminated by the time the request completes (and might never
// terminate).
func (c *Container) Shutdown(ctx context.Context) (err error) {
ctx, span := trace.StartSpan(ctx, "gcs::Container::Shutdown")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", c.id))
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
return c.shutdown(ctx, rpcShutdownGraceful)
}
// Terminate sends a forceful terminate request to the container. The container
// might not be terminated by the time the request completes (and might never
// terminate).
func (c *Container) Terminate(ctx context.Context) (err error) {
ctx, span := trace.StartSpan(ctx, "gcs::Container::Terminate")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(trace.StringAttribute("cid", c.id))
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
return c.shutdown(ctx, rpcShutdownForced)
}
func (c *Container) WaitChannel() <-chan struct{} {
return c.waitBlock
}
func (c *Container) WaitError() error {
return c.waitError
}
// Wait waits for the container to terminate (or Close to be called, or the
// guest connection to terminate).
func (c *Container) Wait() error {
<-c.WaitChannel()
return c.WaitError()
}
func (c *Container) waitBackground() {
ctx, span := trace.StartSpan(context.Background(), "gcs::Container::waitBackground")
defer span.End()
span.AddAttributes(trace.StringAttribute("cid", c.id))
select {
case <-c.notifyCh:
case <-c.closeCh:
c.waitError = errors.New("container closed")
}
close(c.waitBlock)
log.G(ctx).Debug("container exited")
oc.SetSpanStatus(span, c.waitError)
}