/
config.go
132 lines (110 loc) · 3.49 KB
/
config.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
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package docker
import (
"fmt"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/go-connections/nat"
"github.com/lastbackend/lastbackend/pkg/distribution/types"
"strconv"
)
func GetConfig(manifest *types.ContainerManifest) *container.Config {
var (
volumes = make(map[string]struct{}, 0)
ports = make(nat.PortSet, 0)
)
for _, p := range manifest.Ports {
port := nat.Port(strconv.Itoa(int(p.ContainerPort)))
ports[port] = struct{}{}
}
return &container.Config{
Hostname: manifest.Network.Hostname,
Domainname: manifest.Network.Domain,
Env: manifest.Envs,
ExposedPorts: ports,
Volumes: volumes,
Labels: manifest.Labels,
Cmd: strslice.StrSlice(manifest.Exec.Command),
Entrypoint: strslice.StrSlice(manifest.Exec.Entrypoint),
Image: manifest.Image,
WorkingDir: manifest.Exec.Workdir,
}
}
func GetHostConfig(manifest *types.ContainerManifest) *container.HostConfig {
rPolicy := container.RestartPolicy{
Name: manifest.RestartPolicy.Policy,
MaximumRetryCount: manifest.RestartPolicy.Attempt,
}
resources := container.Resources{
Memory: manifest.Resources.Request.RAM * 1024 * 1024,
CPUShares: manifest.Resources.Request.CPU,
}
var (
ports = make(nat.PortMap, 0)
mounts []mount.Mount
links []string
)
for _, l := range manifest.Links {
links = append(links, fmt.Sprintf("%s:%s", l.Link, l.Alias))
}
logC := container.LogConfig{}
ports = make(nat.PortMap)
for _, p := range manifest.Ports {
port := nat.Port(strconv.Itoa(int(p.ContainerPort)))
ports[port] = make([]nat.PortBinding, 0)
if p.HostPort != 0 {
if p.HostIP == types.EmptyString {
p.HostIP = "0.0.0.0"
}
ports[port] = append(ports[port], nat.PortBinding{
HostIP: p.HostIP,
HostPort: fmt.Sprintf("%d", p.HostPort),
})
}
}
cfg := container.HostConfig{
Binds: manifest.Binds,
LogConfig: logC,
NetworkMode: container.NetworkMode(manifest.Network.Mode),
PortBindings: ports,
DNS: manifest.DNS.Server,
DNSOptions: manifest.DNS.Options,
DNSSearch: manifest.DNS.Search,
RestartPolicy: rPolicy,
Resources: resources,
Mounts: mounts,
Links: links,
Privileged: manifest.Security.Privileged,
ExtraHosts: manifest.ExtraHosts,
PublishAllPorts: manifest.PublishAllPorts,
AutoRemove: manifest.AutoRemove,
}
if cfg.NetworkMode != types.EmptyString {
cfg.DNS = make([]string, 0)
cfg.DNSOptions = make([]string, 0)
cfg.DNSSearch = make([]string, 0)
}
return &cfg
}
func GetNetworkConfig(manifest *types.ContainerManifest) *network.NetworkingConfig {
cfg := &network.NetworkingConfig{}
return cfg
}