-
Notifications
You must be signed in to change notification settings - Fork 239
/
container.go
231 lines (197 loc) · 7.1 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
//go:build linux
// +build linux
package hcsv2
import (
"context"
"sync"
"syscall"
"github.com/containerd/cgroups"
v1 "github.com/containerd/cgroups/stats/v1"
oci "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
"github.com/Microsoft/hcsshim/internal/guest/gcserr"
"github.com/Microsoft/hcsshim/internal/guest/prot"
"github.com/Microsoft/hcsshim/internal/guest/runtime"
specInternal "github.com/Microsoft/hcsshim/internal/guest/spec"
"github.com/Microsoft/hcsshim/internal/guest/stdio"
"github.com/Microsoft/hcsshim/internal/guest/storage"
"github.com/Microsoft/hcsshim/internal/guest/transport"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
)
type Container struct {
id string
vsock transport.Transport
spec *oci.Spec
isSandbox bool
container runtime.Container
initProcess *containerProcess
etL sync.Mutex
exitType prot.NotificationType
processesMutex sync.Mutex
processes map[uint32]*containerProcess
}
func (c *Container) Start(ctx context.Context, conSettings stdio.ConnectionSettings) (int, error) {
log.G(ctx).WithField(logfields.ContainerID, c.id).Info("opengcs::Container::Start")
stdioSet, err := stdio.Connect(c.vsock, conSettings)
if err != nil {
return -1, err
}
if c.initProcess.spec.Terminal {
ttyr := c.container.Tty()
ttyr.ReplaceConnectionSet(stdioSet)
ttyr.Start()
} else {
pr := c.container.PipeRelay()
pr.ReplaceConnectionSet(stdioSet)
pr.CloseUnusedPipes()
pr.Start()
}
err = c.container.Start()
if err != nil {
stdioSet.Close()
}
return int(c.initProcess.pid), err
}
func (c *Container) ExecProcess(ctx context.Context, process *oci.Process, conSettings stdio.ConnectionSettings) (int, error) {
log.G(ctx).WithField(logfields.ContainerID, c.id).Info("opengcs::Container::ExecProcess")
stdioSet, err := stdio.Connect(c.vsock, conSettings)
if err != nil {
return -1, err
}
// Add in the core rlimit specified on the container in case there was one set. This makes it so that execed processes can also generate
// core dumps.
process.Rlimits = c.spec.Process.Rlimits
// If the client provided a user for the container to run as, we want to have the exec run as this user as well
// unless the exec's spec was explicitly set to a different user. If the Username field is filled in on the containers
// spec, at this point that means the work to find a uid:gid pairing for this username has already been done, so simply
// assign the uid:gid from the container.
if process.User.Username != "" {
// The exec provided a user string of it's own. Grab the uid:gid pairing for the string (if one exists).
if err := setUserStr(&oci.Spec{Root: c.spec.Root, Process: process}, process.User.Username); err != nil {
return -1, err
}
// Runc doesn't care about this, and just to be safe clear it.
process.User.Username = ""
} else if c.spec.Process.User.Username != "" {
process.User = c.spec.Process.User
}
p, err := c.container.ExecProcess(process, stdioSet)
if err != nil {
stdioSet.Close()
return -1, err
}
pid := p.Pid()
c.processesMutex.Lock()
c.processes[uint32(pid)] = newProcess(c, process, p, uint32(pid), false)
c.processesMutex.Unlock()
return pid, nil
}
// InitProcess returns the container's init process
func (c *Container) InitProcess() Process {
return c.initProcess
}
// GetProcess returns the Process with the matching 'pid'. If the 'pid' does
// not exit returns error.
func (c *Container) GetProcess(pid uint32) (Process, error) {
//todo: thread a context to this function call
logrus.WithFields(logrus.Fields{
logfields.ContainerID: c.id,
logfields.ProcessID: pid,
}).Info("opengcs::Container::GetProcess")
if c.initProcess.pid == pid {
return c.initProcess, nil
}
c.processesMutex.Lock()
defer c.processesMutex.Unlock()
p, ok := c.processes[pid]
if !ok {
return nil, gcserr.NewHresultError(gcserr.HrErrNotFound)
}
return p, nil
}
// GetAllProcessPids returns all process pids in the container namespace.
func (c *Container) GetAllProcessPids(ctx context.Context) ([]int, error) {
log.G(ctx).WithField(logfields.ContainerID, c.id).Info("opengcs::Container::GetAllProcessPids")
state, err := c.container.GetAllProcesses()
if err != nil {
return nil, err
}
pids := make([]int, len(state))
for i, s := range state {
pids[i] = s.Pid
}
return pids, nil
}
// Kill sends 'signal' to the container process.
func (c *Container) Kill(ctx context.Context, signal syscall.Signal) error {
log.G(ctx).WithField(logfields.ContainerID, c.id).Info("opengcs::Container::Kill")
err := c.container.Kill(signal)
if err != nil {
return err
}
c.setExitType(signal)
return nil
}
func (c *Container) Delete(ctx context.Context) error {
entity := log.G(ctx).WithField(logfields.ContainerID, c.id)
entity.Info("opengcs::Container::Delete")
if c.isSandbox {
// remove user mounts in sandbox container
if err := storage.UnmountAllInPath(ctx, specInternal.SandboxMountsDir(c.id), true); err != nil {
entity.WithError(err).Error("failed to unmount sandbox mounts")
}
// remove hugepages mounts in sandbox container
if err := storage.UnmountAllInPath(ctx, specInternal.HugePagesMountsDir(c.id), true); err != nil {
entity.WithError(err).Error("failed to unmount hugepages mounts")
}
}
return c.container.Delete()
}
func (c *Container) Update(ctx context.Context, resources interface{}) error {
log.G(ctx).WithField(logfields.ContainerID, c.id).Info("opengcs::Container::Update")
return c.container.Update(resources)
}
// Wait waits for the container's init process to exit.
func (c *Container) Wait() prot.NotificationType {
_, span := trace.StartSpan(context.Background(), "opengcs::Container::Wait")
defer span.End()
span.AddAttributes(trace.StringAttribute(logfields.ContainerID, c.id))
c.initProcess.writersWg.Wait()
c.etL.Lock()
defer c.etL.Unlock()
return c.exitType
}
// setExitType sets `c.exitType` to the appropriate value based on `signal` if
// `signal` will take down the container.
func (c *Container) setExitType(signal syscall.Signal) {
c.etL.Lock()
defer c.etL.Unlock()
if signal == syscall.SIGTERM {
c.exitType = prot.NtGracefulExit
} else if signal == syscall.SIGKILL {
c.exitType = prot.NtForcedExit
}
}
// GetStats returns the cgroup metrics for the container.
func (c *Container) GetStats(ctx context.Context) (*v1.Metrics, error) {
_, span := trace.StartSpan(ctx, "opengcs::Container::GetStats")
defer span.End()
span.AddAttributes(trace.StringAttribute("cid", c.id))
cgroupPath := c.spec.Linux.CgroupsPath
cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(cgroupPath))
if err != nil {
return nil, errors.Errorf("failed to get container stats for %v: %v", c.id, err)
}
return cg.Stat(cgroups.IgnoreNotExist)
}
func (c *Container) modifyContainerConstraints(ctx context.Context, rt guestrequest.RequestType, cc *guestresource.LCOWContainerConstraints) (err error) {
return c.Update(ctx, cc.Linux)
}
func (c *Container) ID() string {
return c.id
}