forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_utils.go
302 lines (265 loc) · 7.72 KB
/
container_utils.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
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
package mgr
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/alibaba/pouch/apis/opts"
"github.com/alibaba/pouch/apis/types"
networktypes "github.com/alibaba/pouch/network/types"
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/meta"
"github.com/alibaba/pouch/pkg/randomid"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/pkg/errors"
)
// containerID returns the container's id, the parameter 'nameOrPrefix' may be container's
// name, id or prefix id.
func (mgr *ContainerManager) containerID(nameOrPrefix string) (string, error) {
var obj meta.Object
// name is the container's name.
id, ok := mgr.NameToID.Get(nameOrPrefix).String()
if ok {
return id, nil
}
// name is the container's prefix of the id.
objs, err := mgr.Store.GetWithPrefix(nameOrPrefix)
if err != nil {
return "", errors.Wrapf(err, "failed to get container info with prefix %s", nameOrPrefix)
}
if len(objs) > 1 {
return "", errors.Wrapf(errtypes.ErrTooMany, "container %s", nameOrPrefix)
}
if len(objs) == 0 {
return "", errors.Wrapf(errtypes.ErrNotfound, "container %s", nameOrPrefix)
}
obj = objs[0]
con, ok := obj.(*Container)
if !ok {
return "", fmt.Errorf("failed to get container info, invalid meta's type")
}
return con.ID, nil
}
func (mgr *ContainerManager) container(nameOrPrefix string) (*Container, error) {
res, ok := mgr.cache.Get(nameOrPrefix).Result()
if ok {
return res.(*Container), nil
}
id, err := mgr.containerID(nameOrPrefix)
if err != nil {
return nil, err
}
// lookup again
res, ok = mgr.cache.Get(id).Result()
if ok {
return res.(*Container), nil
}
return nil, errors.Wrapf(errtypes.ErrNotfound, "container %s", nameOrPrefix)
}
// generateID generates an ID for newly created container. We must ensure that
// this ID has not used yet.
func (mgr *ContainerManager) generateID() (string, error) {
var id string
for {
id = randomid.Generate()
_, err := mgr.Store.Get(id)
if err != nil {
if merr, ok := err.(meta.Error); ok && merr.IsNotfound() {
break
}
return "", err
}
}
return id, nil
}
// generateName generates container name by container ID.
// It get first 6 continuous letters which has not been taken.
// TODO: take a shorter than 6 letters ID into consideration.
// FIXME: there is possibility that for block loops forever.
func (mgr *ContainerManager) generateName(id string) string {
var name string
i := 0
for {
if i+6 > len(id) {
break
}
name = id[i : i+6]
i++
if !mgr.NameToID.Get(name).Exist() {
break
}
}
return name
}
// getRuntime returns runtime real path.
// TODO(huamin): do we need validate runtime is executable ?
func (mgr *ContainerManager) getRuntime(runtime string) (string, error) {
r, exist := mgr.Config.Runtimes[runtime]
if !exist {
return "", fmt.Errorf("failed to find runtime %s in daemon config", runtime)
}
// it is ok to use runtime name as a path.
rPath := runtime
// generally speaking, path is not be empty, but we not forbid empty path
// in config set, since name can be a path too.
if r.Path != "" {
rPath = r.Path
}
// if Runtime has args, use script path as runtime path.
if len(r.RuntimeArgs) > 0 {
rPath = filepath.Join(mgr.Config.HomeDir, RuntimeDir, runtime)
}
return rPath, nil
}
// BuildContainerEndpoint is used to build container's endpoint config.
func BuildContainerEndpoint(c *Container) *networktypes.Endpoint {
return &networktypes.Endpoint{
Owner: c.ID,
Hostname: c.Config.Hostname,
Domainname: c.Config.Domainname,
HostsPath: c.HostsPath,
ExtraHosts: c.HostConfig.ExtraHosts,
HostnamePath: c.HostnamePath,
ResolvConfPath: c.ResolvConfPath,
NetworkDisabled: c.Config.NetworkDisabled,
NetworkMode: c.HostConfig.NetworkMode,
DNS: c.HostConfig.DNS,
DNSOptions: c.HostConfig.DNSOptions,
DNSSearch: c.HostConfig.DNSSearch,
MacAddress: c.Config.MacAddress,
PublishAllPorts: c.HostConfig.PublishAllPorts,
ExposedPorts: c.Config.ExposedPorts,
PortBindings: c.HostConfig.PortBindings,
NetworkConfig: c.NetworkSettings,
}
}
func parseSecurityOpts(c *Container, securityOpts []string) error {
var (
labelOpts []string
err error
)
for _, securityOpt := range securityOpts {
if securityOpt == "no-new-privileges" {
c.NoNewPrivileges = true
continue
}
fields := strings.SplitN(securityOpt, "=", 2)
if len(fields) != 2 {
return fmt.Errorf("invalid --security-opt %s: must be in format of key=value", securityOpt)
}
key, value := fields[0], fields[1]
switch key {
// TODO: handle other security options.
case "apparmor":
c.AppArmorProfile = value
case "seccomp":
c.SeccompProfile = value
case "no-new-privileges":
noNewPrivileges, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("invalid --security-opt: %q", securityOpt)
}
c.NoNewPrivileges = noNewPrivileges
case "label":
labelOpts = append(labelOpts, value)
default:
return fmt.Errorf("invalid type %s in --security-opt %s: unknown type from apparmor, seccomp, no-new-privileges and SELinux label", key, securityOpt)
}
}
if len(labelOpts) == 0 {
return nil
}
c.ProcessLabel, c.MountLabel, err = label.InitLabels(labelOpts)
if err != nil {
return fmt.Errorf("failed to init labels: %v", err)
}
return nil
}
// fieldsASCII is similar to strings.Fields but only allows ASCII whitespaces
func fieldsASCII(s string) []string {
fn := func(r rune) bool {
switch r {
case '\t', '\n', '\f', '\r', ' ':
return true
}
return false
}
return strings.FieldsFunc(s, fn)
}
func parsePSOutput(output []byte, pids []int) (*types.ContainerProcessList, error) {
procList := &types.ContainerProcessList{}
lines := strings.Split(string(output), "\n")
procList.Titles = fieldsASCII(lines[0])
pidIndex := -1
for i, name := range procList.Titles {
if name == "PID" {
pidIndex = i
}
}
if pidIndex == -1 {
return nil, fmt.Errorf("Couldn't find PID field in ps output")
}
// loop through the output and extract the PID from each line
for _, line := range lines[1:] {
if len(line) == 0 {
continue
}
fields := fieldsASCII(line)
p, err := strconv.Atoi(fields[pidIndex])
if err != nil {
return nil, fmt.Errorf("Unexpected pid '%s': %s", fields[pidIndex], err)
}
for _, pid := range pids {
if pid == p {
// Make sure number of fields equals number of header titles
// merging "overhanging" fields
process := fields[:len(procList.Titles)-1]
process = append(process, strings.Join(fields[len(procList.Titles)-1:], " "))
procList.Processes = append(procList.Processes, process)
}
}
}
return procList, nil
}
// amendContainerSettings modify config settings to wanted,
// it will be call before container created.
func amendContainerSettings(config *types.ContainerConfig, hostConfig *types.HostConfig) {
r := &hostConfig.Resources
if r.Memory > 0 && r.MemorySwap == 0 {
r.MemorySwap = 2 * r.Memory
}
}
func mergeEnvSlice(newEnv, oldEnv []string) ([]string, error) {
// if newEnv is empty, return old env slice
if len(newEnv) == 0 {
return oldEnv, nil
}
newEnvMap, err := opts.ParseEnv(newEnv)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse new env")
}
oldEnvMap, err := opts.ParseEnv(oldEnv)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse old env")
}
for k, v := range newEnvMap {
// key should not be empty
if k == "" {
continue
}
// add or change an env
if v != "" {
oldEnvMap[k] = v
continue
}
// value is empty, we need delete the env
if _, exists := oldEnvMap[k]; exists {
delete(oldEnvMap, k)
}
}
newEnvSlice := []string{}
for k, v := range oldEnvMap {
newEnvSlice = append(newEnvSlice, fmt.Sprintf("%s=%s", k, v))
}
return newEnvSlice, nil
}