forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_common.go
executable file
·398 lines (354 loc) · 11.4 KB
/
compute_common.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
package compute
import (
"fmt"
urls "net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
"github.com/rancher/agent/core/progress"
"github.com/rancher/agent/core/storage"
"github.com/rancher/agent/model"
configuration "github.com/rancher/agent/utilities/config"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/utils"
"golang.org/x/net/context"
)
var (
dockerRootOnce = sync.Once{}
dockerRoot = ""
)
func createContainer(dockerClient *client.Client, config *container.Config, hostConfig *container.HostConfig, networkConfig *network.NetworkingConfig,
imageTag string, instance model.Instance, name string, progress *progress.Progress) (string, error) {
labels := config.Labels
if labels[constants.PullImageLabels] == "always" {
params := model.ImageParams{
Image: instance.Image,
Tag: "",
Mode: "all",
Complete: false,
ImageUUID: instance.Data.Fields.ImageUUID,
}
_, err := DoInstancePull(params, progress, dockerClient)
if err != nil {
return "", errors.Wrap(err, constants.CreateContainerError+"failed to pull instance")
}
}
imageName := utils.ParseRepoTag(imageTag)
config.Image = imageName
containerResponse, err := dockerContainerCreate(context.Background(), dockerClient, config, hostConfig, networkConfig, name)
// if image doesn't exist
if client.IsErrImageNotFound(err) {
if err := storage.PullImage(instance.Image, progress, dockerClient, imageTag); err != nil {
return "", errors.Wrap(err, constants.CreateContainerError+"failed to pull image")
}
containerResponse, err1 := dockerContainerCreate(context.Background(), dockerClient, config, hostConfig, networkConfig, name)
if err1 != nil {
return "", errors.Wrap(err1, constants.CreateContainerError+"failed to create container")
}
return containerResponse.ID, nil
} else if err != nil {
return "", errors.Wrap(err, constants.CreateContainerError+"failed to create container")
}
return containerResponse.ID, nil
}
func getImageTag(instance model.Instance) (string, error) {
dockerImage := instance.Data.Fields.ImageUUID
if dockerImage == "" {
return "", errors.New(constants.StartContainerNoImageError + "the full name of docker image is empty")
}
return dockerImage, 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, instance model.Instance) {
config.Hostname = instance.Hostname
}
func setupPorts(config *container.Config, instance model.Instance, hostConfig *container.HostConfig) {
ports := []model.Port{}
exposedPorts := map[nat.Port]struct{}{}
bindings := nat.PortMap{}
if instance.Ports != nil && len(instance.Ports) > 0 {
for _, port := range instance.Ports {
ports = append(ports, model.Port{PrivatePort: port.PrivatePort, Protocol: port.Protocol})
if port.PrivatePort != 0 {
bind := nat.Port(fmt.Sprintf("%v/%v", port.PrivatePort, port.Protocol))
bindAddr := port.Data.Fields.BindAddress
if _, ok := bindings[bind]; !ok {
bindings[bind] = []nat.PortBinding{
{
HostIP: bindAddr,
HostPort: utils.ConvertPortToString(port.PublicPort),
},
}
} else {
bindings[bind] = append(bindings[bind], nat.PortBinding{HostIP: bindAddr,
HostPort: utils.ConvertPortToString(port.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
}
func setupVolumes(config *container.Config, instance model.Instance, hostConfig *container.HostConfig, client *client.Client, progress *progress.Progress) error {
volumes := instance.Data.Fields.DataVolumes
volumesMap := map[string]struct{}{}
binds := []string{}
if len(volumes) > 0 {
for _, volume := range volumes {
parts := strings.SplitN(volume, ":", 3)
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 = binds
}
containers := []string{}
if vfsList := instance.DataVolumesFromContainers; vfsList != nil {
for _, vfs := range vfsList {
container, err := utils.GetContainer(client, (*vfs), false)
if err != nil {
if !utils.IsContainerNotFoundError(err) {
return errors.Wrap(err, constants.SetupVolumesError+"failed to get container")
}
}
if container.ID != "" {
containers = append(containers, container.ID)
}
}
if len(containers) > 0 {
hostConfig.VolumesFrom = containers
}
}
if vMounts := instance.VolumesFromDataVolumeMounts; len(vMounts) > 0 {
for _, vMount := range vMounts {
storagePool := model.StoragePool{}
// volume active == exists, possibly not attached to this host
if ok, err := storage.IsVolumeActive(vMount, storagePool, client); !ok && err == nil {
if err := storage.DoVolumeActivate(vMount, storagePool, progress, client); err != nil {
return errors.Wrap(err, constants.SetupVolumesError+"failed to activate volume")
}
} else if err != nil {
return errors.Wrap(err, constants.SetupVolumesError+"failed to check whether volume is activated")
}
if storage.IsRancherVolume(vMount) {
if err := storage.RancherStorageVolumeAttach(vMount); err != nil {
return errors.Wrap(err, constants.SetupVolumesError+"failed to attach volume")
}
}
}
}
return nil
}
func setupHeathConfig(instanceFields model.InstanceFields, config *container.Config) {
healthConfig := &container.HealthConfig{}
healthConfig.Test = instanceFields.HealthCmd
healthConfig.Interval = time.Duration(instanceFields.HealthInterval) * time.Second
healthConfig.Retries = instanceFields.HealthRetries
healthConfig.Timeout = time.Duration(instanceFields.HealthTimeout) * time.Second
config.Healthcheck = healthConfig
}
func setupProxy(instance model.Instance, config *container.Config, hostEntries map[string]string) {
// only setup envs for system container
if instance.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 constants.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(instance model.Instance, config *container.Config) {
if instance.AgentID == 0 && !utils.HasLabel(instance) {
return
}
utils.AddLabel(config, constants.AgentIDLabel, strconv.Itoa(instance.AgentID))
url := configuration.URL()
if len(url) > 0 {
parsed, err := urls.Parse(url)
if err != nil {
logrus.Error(err)
} else {
if strings.Contains(parsed.Host, "localhost") {
port := configuration.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 setupLegacyCommand(config *container.Config, fields model.InstanceFields, command string) {
// This can be removed shortly once cattle removes
// commandArgs
if len(command) == 0 || len(strings.TrimSpace(command)) == 0 {
return
}
commandArgs := fields.CommandArgs
commands := []string{}
parts := strings.Split(command, " ")
for _, part := range parts {
commands = append(commands, part)
}
if len(commandArgs) > 0 {
for _, value := range commandArgs {
commands = append(commands, value)
}
}
if len(commands) > 0 {
config.Cmd = commands
}
}
func setupNetworkingConfig(networkConfig *network.NetworkingConfig, instance model.Instance) {
}
func setupLabels(labels map[string]string, 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(fields model.InstanceFields, config *container.Config) {
// this one is really weird
commands, ok := fields.Command.([]interface{})
if !ok {
setupLegacyCommand(config, fields, utils.InterfaceToString(commands))
} else {
cmds := utils.InterfaceToArray(commands)
for _, cmd := range cmds {
config.Cmd = append(config.Cmd, utils.InterfaceToString(cmd))
}
}
envs := []string{}
for k, v := range fields.Environment {
envs = append(envs, fmt.Sprintf("%v=%v", k, v))
}
config.Env = append(config.Env, envs...)
config.WorkingDir = fields.WorkingDir
config.Entrypoint = fields.EntryPoint
config.Tty = fields.Tty
config.OpenStdin = fields.StdinOpen
config.Domainname = fields.DomainName
for k, v := range fields.Labels {
config.Labels[k] = v
}
config.StopSignal = fields.StopSignal
config.User = fields.User
}
func isStopped(client *client.Client, container types.Container) (bool, error) {
ok, err := isRunning(client, container)
if err != nil {
return false, err
}
return !ok, nil
}
func isRunning(dockerClient *client.Client, container types.Container) (bool, error) {
if container.ID == "" {
return false, nil
}
inspect, err := dockerClient.ContainerInspect(context.Background(), container.ID)
if err == nil {
return inspect.State.Running, nil
} else if client.IsErrContainerNotFound(err) {
return false, nil
}
return false, err
}
func getHostEntries() map[string]string {
data := map[string]string{}
for _, env := range constants.HTTPProxyList {
if os.Getenv(env) != "" {
data[env] = os.Getenv(env)
}
}
return data
}