forked from rancher/agent
/
container_start.go
489 lines (419 loc) · 14.1 KB
/
container_start.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package runtime
import (
"context"
"fmt"
urls "net/url"
"strconv"
"strings"
"time"
"os"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
"github.com/rancher/agent/progress"
"github.com/rancher/agent/utils"
v3 "github.com/rancher/go-rancher/v3"
"sync"
)
const (
ContainerNameLabel = "io.rancher.container.name"
PullImageLabels = "io.rancher.container.pull_image"
UUIDLabel = "io.rancher.container.uuid"
AgentIDLabel = "io.rancher.container.agent_id"
)
var (
dockerRootOnce = sync.Once{}
dockerRoot = ""
HTTPProxyList = []string{"http_proxy", "HTTP_PROXY", "https_proxy", "HTTPS_PROXY", "no_proxy", "NO_PROXY"}
)
func ContainerStart(containerSpec v3.Container, volumes []v3.Volume, networkKind string, credentials []v3.Credential, progress *progress.Progress, runtimeClient *client.Client, idsMap map[string]string) error {
started := false
// setup name
parts := strings.Split(containerSpec.Uuid, "-")
if len(parts) == 0 {
return errors.New("Failed to parse UUID")
}
name := fmt.Sprintf("r-%s", containerSpec.Uuid)
if str := utils.NameRegexCompiler.FindString(containerSpec.Name); str != "" {
name = fmt.Sprintf("r-%s-%s", containerSpec.Name, parts[0])
}
// creating managed volumes
rancherBindMounts, err := setupRancherFlexVolume(volumes, containerSpec.DataVolumes, progress)
if err != nil {
return errors.Wrap(err, "failed to set up rancher flex volumes")
}
// make sure managed volumes are unmounted if container is not started
defer func() {
if !started {
unmountRancherFlexVolume(volumes)
}
}()
// setup container spec(config and hostConfig)
spec, err := setupContainerSpec(containerSpec, volumes, networkKind, rancherBindMounts, runtimeClient, progress, idsMap)
if err != nil {
return errors.Wrap(err, "failed to generate container spec")
}
containerID, err := utils.FindContainer(runtimeClient, containerSpec, false)
if err != nil {
if !utils.IsContainerNotFoundError(err) {
return errors.Wrap(err, "failed to get container")
}
}
created := false
if containerID == "" {
credential := v3.Credential{}
if credentials != nil && len(credentials) > 0 {
credential = credentials[0]
}
newID, err := createContainer(runtimeClient, &spec.config, &spec.hostConfig, containerSpec, credential, name, progress)
if err != nil {
return errors.Wrap(err, "failed to create container")
}
containerID = newID
created = true
}
startErr := utils.Serialize(func() error {
return runtimeClient.ContainerStart(context.Background(), containerID, types.ContainerStartOptions{})
})
if startErr != nil {
if created {
if err := utils.RemoveContainer(runtimeClient, containerID); err != nil {
return errors.Wrap(err, "failed to remove container")
}
}
return errors.Wrap(startErr, "failed to start container")
}
logrus.Infof("rancher id [%v]: Container [%v] with docker id [%v] has been started", containerSpec.Id, containerSpec.Name, containerID)
started = true
return nil
}
func IsContainerStarted(containerSpec v3.Container, client *client.Client) (bool, bool, error) {
cont, err := utils.FindContainer(client, containerSpec, false)
if err != nil {
if utils.IsContainerNotFoundError(err) {
return false, false, nil
}
return false, false, errors.Wrap(err, "failed to get container")
}
return isRunning(client, cont)
}
type dockerContainerSpec struct {
config container.Config
hostConfig container.HostConfig
}
func setupContainerSpec(containerSpec v3.Container, volumes []v3.Volume, networkKind string, rancherBindMounts []string, runtimeClient *client.Client, progress *progress.Progress, idsMap map[string]string) (dockerContainerSpec, error) {
config := container.Config{
OpenStdin: true,
}
hostConfig := container.HostConfig{
PublishAllPorts: false,
Privileged: containerSpec.Privileged,
ReadonlyRootfs: containerSpec.ReadOnly,
}
initializeMaps(&config, &hostConfig)
setupLabels(containerSpec.Labels, &config)
config.Labels[UUIDLabel] = containerSpec.Uuid
config.Labels[ContainerNameLabel] = containerSpec.Name
setupFieldsHostConfig(containerSpec, &hostConfig)
setupFieldsConfig(containerSpec, &config)
setupPublishPorts(&hostConfig, containerSpec)
if err := setupDNSSearch(&hostConfig, containerSpec); err != nil {
return dockerContainerSpec{}, errors.Wrap(err, "failed to set up DNS search")
}
setupHostname(&config, containerSpec)
setupPorts(&config, containerSpec, &hostConfig)
hostConfig.Binds = append(hostConfig.Binds, rancherBindMounts...)
if err := setupNonRancherVolumes(&config, volumes, containerSpec, &hostConfig, runtimeClient, progress, idsMap); err != nil {
return dockerContainerSpec{}, errors.Wrap(err, "failed to set up volumes")
}
if err := setupNetworking(containerSpec, &config, &hostConfig, idsMap, networkKind); err != nil {
return dockerContainerSpec{}, errors.Wrap(err, "failed to set up networking")
}
setupProxy(containerSpec, &config, getHostEntries())
setupCattleConfigURL(containerSpec, &config)
setupDeviceOptions(&hostConfig, containerSpec)
setupComputeResourceFields(&hostConfig, containerSpec)
setupHeathConfig(containerSpec, &config)
return dockerContainerSpec{
config: config,
hostConfig: hostConfig,
}, nil
}
type PullParams struct {
Tag string
Mode string
Complete bool
ImageUUID string
}
func createContainer(dockerClient *client.Client, config *container.Config, hostConfig *container.HostConfig, containerSpec v3.Container, credential v3.Credential, name string, progress *progress.Progress) (string, error) {
labels := config.Labels
if labels[PullImageLabels] == "always" {
params := PullParams{
Tag: "",
Mode: "all",
Complete: false,
ImageUUID: containerSpec.Image,
}
_, err := DoInstancePull(params, progress, dockerClient, credential)
if err != nil {
return "", errors.Wrap(err, "failed to pull instance")
}
}
config.Image = containerSpec.Image
containerResponse, err := dockerContainerCreate(context.Background(), dockerClient, config, hostConfig, name)
// if image doesn't exist
if client.IsErrImageNotFound(err) {
if err := ImagePull(progress, dockerClient, containerSpec.Image, credential); err != nil {
return "", errors.Wrap(err, "failed to pull image")
}
containerResponse, err1 := dockerContainerCreate(context.Background(), dockerClient, config, hostConfig, name)
if err1 != nil {
return "", errors.Wrap(err1, "failed to create container")
}
return containerResponse.ID, nil
} else if err != nil {
return "", errors.Wrap(err, "failed to create container")
}
return containerResponse.ID, nil
}
func initializeMaps(config *container.Config, hostConfig *container.HostConfig) {
config.Labels = make(map[string]string)
config.Volumes = make(map[string]struct{})
config.ExposedPorts = make(map[nat.Port]struct{})
hostConfig.PortBindings = make(map[nat.Port][]nat.PortBinding)
hostConfig.StorageOpt = make(map[string]string)
hostConfig.Tmpfs = make(map[string]string)
hostConfig.Sysctls = make(map[string]string)
}
func setupHostname(config *container.Config, containerSpec v3.Container) {
config.Hostname = containerSpec.Hostname
}
func setupPorts(config *container.Config, containerSpec v3.Container, hostConfig *container.HostConfig) {
//ports := []types.Port{}
exposedPorts := map[nat.Port]struct{}{}
bindings := nat.PortMap{}
for _, endpoint := range containerSpec.PublicEndpoints {
if endpoint.PrivatePort != 0 {
bind := nat.Port(fmt.Sprintf("%v/%v", endpoint.PrivatePort, endpoint.Protocol))
bindAddr := endpoint.BindIpAddress
if _, ok := bindings[bind]; !ok {
bindings[bind] = []nat.PortBinding{
{
HostIP: bindAddr,
HostPort: strconv.Itoa(int(endpoint.PublicPort)),
},
}
} else {
bindings[bind] = append(bindings[bind], nat.PortBinding{
HostIP: bindAddr,
HostPort: strconv.Itoa(int(endpoint.PublicPort)),
})
}
exposedPorts[bind] = struct{}{}
}
}
config.ExposedPorts = exposedPorts
if len(bindings) > 0 {
hostConfig.PortBindings = bindings
}
}
func getDockerRoot(client *client.Client) string {
dockerRootOnce.Do(func() {
info, err := client.Info(context.Background())
if err != nil {
panic(err.Error())
}
dockerRoot = info.DockerRootDir
})
return dockerRoot
}
// setupVolumes volumes except rancher specific volumes. For rancher-managed volume driver they will be setup through special steps like flexvolume
func setupNonRancherVolumes(config *container.Config, volumes []v3.Volume, containerSpec v3.Container, hostConfig *container.HostConfig, client *client.Client, progress *progress.Progress, idsMap map[string]string) error {
volumesMap := map[string]struct{}{}
binds := []string{}
rancherManagedVolumeNames := map[string]struct{}{}
for _, volume := range volumes {
if IsRancherVolume(volume) {
rancherManagedVolumeNames[volume.Name] = struct{}{}
}
}
for _, volume := range containerSpec.DataVolumes {
parts := strings.SplitN(volume, ":", 3)
// don't set rancher managed volume
if _, ok := rancherManagedVolumeNames[parts[0]]; ok {
continue
}
if len(parts) == 1 {
volumesMap[parts[0]] = struct{}{}
} else if len(parts) > 1 {
volumesMap[parts[1]] = struct{}{}
mode := ""
if len(parts) == 3 {
mode = parts[2]
} else {
mode = "rw"
}
// Redirect /var/lib/docker:/var/lib/docker to where Docker root really is
if parts[0] == "/var/lib/docker" && parts[1] == "/var/lib/docker" {
root := getDockerRoot(client)
if root != "/var/lib/docker" {
volumesMap[root] = struct{}{}
binds = append(binds, fmt.Sprintf("%s:%s:%s", root, parts[1], mode))
binds = append(binds, fmt.Sprintf("%s:%s:%s", root, root, mode))
continue
}
}
bind := fmt.Sprintf("%s:%s:%s", parts[0], parts[1], mode)
binds = append(binds, bind)
}
config.Volumes = volumesMap
hostConfig.Binds = append(hostConfig.Binds, binds...)
}
containers := []string{}
if containerSpec.DataVolumesFrom != nil {
for _, volumeFrom := range containerSpec.DataVolumesFrom {
if idsMap[volumeFrom] != "" {
containers = append(containers, idsMap[volumeFrom])
}
}
if len(containers) > 0 {
hostConfig.VolumesFrom = containers
}
}
for _, volume := range volumes {
// volume active == exists, possibly not attached to this host
if !IsRancherVolume(volume) {
if ok, err := IsVolumeActive(volume, client); !ok && err == nil {
if err := DoVolumeActivate(volume, client, progress); err != nil {
return errors.Wrap(err, "failed to activate volume")
}
} else if err != nil {
return errors.Wrap(err, "failed to check whether volume is activated")
}
}
}
return nil
}
func setupHeathConfig(spec v3.Container, config *container.Config) {
healthConfig := &container.HealthConfig{}
healthConfig.Test = spec.HealthCmd
healthConfig.Interval = time.Duration(spec.HealthInterval) * time.Second
healthConfig.Retries = int(spec.HealthRetries)
healthConfig.Timeout = time.Duration(spec.HealthTimeout) * time.Second
config.Healthcheck = healthConfig
}
func setupProxy(containerSpec v3.Container, config *container.Config, hostEntries map[string]string) {
// only setup envs for system container
if containerSpec.System {
envMap := map[string]string{}
envList := []string{}
for _, env := range config.Env {
/*
3 case:
1. foo=bar. Parse as normal
2. foo=. Parse as foo=
3. foo. Parse as foo
*/
part := strings.SplitN(env, "=", 2)
if len(part) == 1 {
if strings.Contains(env, "=") {
//case 2
envMap[part[0]] = ""
} else {
envList = append(envList, env)
}
} else if len(part) == 2 {
envMap[part[0]] = part[1]
}
}
for _, key := range HTTPProxyList {
if hostEntries[key] != "" {
envMap[key] = hostEntries[key]
}
}
envs := []string{}
for _, env := range envList {
if _, ok := envMap[env]; ok {
continue
}
envs = append(envs, env)
}
for key, value := range envMap {
envs = append(envs, fmt.Sprintf("%v=%v", key, value))
}
config.Env = envs
}
}
func setupCattleConfigURL(containerSpec v3.Container, config *container.Config) {
if containerSpec.AgentId == "" && !utils.HasLabel(containerSpec) {
return
}
utils.AddLabel(config, AgentIDLabel, containerSpec.AgentId)
url := utils.URL()
if len(url) > 0 {
parsed, err := urls.Parse(url)
if err != nil {
logrus.Error(err)
} else {
if strings.Contains(parsed.Host, "localhost") {
port := utils.APIProxyListenPort()
utils.AddToEnv(config, map[string]string{
"CATTLE_AGENT_INSTANCE": "true",
"CATTLE_CONFIG_URL_SCHEME": parsed.Scheme,
"CATTLE_CONFIG_URL_PATH": parsed.Path,
"CATTLE_CONFIG_URL_PORT": strconv.Itoa(port),
})
} else {
utils.AddToEnv(config, map[string]string{
"CATTLE_CONFIG_URL": url,
"CATTLE_URL": url,
})
}
}
}
}
func setupLabels(labels map[string]interface{}, config *container.Config) {
for k, v := range labels {
config.Labels[k] = utils.InterfaceToString(v)
}
}
// this method convert fields data to fields in configuration
func setupFieldsConfig(spec v3.Container, config *container.Config) {
config.Cmd = spec.Command
envs := []string{}
for k, v := range spec.Environment {
envs = append(envs, fmt.Sprintf("%v=%v", k, v))
}
config.Env = append(config.Env, envs...)
config.WorkingDir = spec.WorkingDir
config.Entrypoint = spec.EntryPoint
config.Tty = spec.Tty
config.OpenStdin = spec.StdinOpen
config.Domainname = spec.DomainName
config.StopSignal = spec.StopSignal
config.User = spec.User
}
func isRunning(dockerClient *client.Client, containerID string) (bool, bool, error) {
if containerID == "" {
return false, false, nil
}
inspect, err := dockerClient.ContainerInspect(context.Background(), containerID)
if err == nil {
return inspect.State.Running && !inspect.State.Restarting, inspect.State.Restarting, nil
} else if client.IsErrContainerNotFound(err) {
return false, false, nil
}
return false, false, err
}
func getHostEntries() map[string]string {
data := map[string]string{}
for _, env := range HTTPProxyList {
if os.Getenv(env) != "" {
data[env] = os.Getenv(env)
}
}
return data
}