/
factory.go
209 lines (164 loc) · 6.13 KB
/
factory.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
package vm
import (
"context"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
"github.com/cppforlife/bosh-cpi-go/apiv1"
dkrclient "github.com/docker/engine-api/client"
dkrtypes "github.com/docker/engine-api/types"
dkrcont "github.com/docker/engine-api/types/container"
dkrfilters "github.com/docker/engine-api/types/filters"
dkrstrslice "github.com/docker/engine-api/types/strslice"
dkrnat "github.com/docker/go-connections/nat"
bstem "github.com/cppforlife/bosh-docker-cpi/stemcell"
)
type Factory struct {
dkrClient *dkrclient.Client
uuidGen boshuuid.Generator
agentOptions apiv1.AgentOptions
logTag string
logger boshlog.Logger
}
func NewFactory(
dkrClient *dkrclient.Client,
uuidGen boshuuid.Generator,
agentOptions apiv1.AgentOptions,
logger boshlog.Logger,
) Factory {
return Factory{
dkrClient: dkrClient,
uuidGen: uuidGen,
agentOptions: agentOptions,
logTag: "vm.Factory",
logger: logger,
}
}
func (f Factory) Create(agentID apiv1.AgentID, stemcell bstem.Stemcell,
cloudProps apiv1.VMCloudProps, networks apiv1.Networks,
diskCIDs []apiv1.DiskCID, env apiv1.VMEnv) (VM, error) {
var vmProps VMProps
err := cloudProps.As(&vmProps)
if err != nil {
return Container{}, bosherr.WrapError(err, "Unmarshaling VM properties")
}
networkInitBashCmd, netConfig, err := NewNetworks(f.dkrClient, f.uuidGen, networks).Enable()
if err != nil {
return nil, bosherr.WrapError(err, "Enabling networks")
}
idStr, err := f.uuidGen.Generate()
if err != nil {
return nil, bosherr.WrapError(err, "Generating container ID")
}
idStr = "c-" + idStr
id := apiv1.NewVMCID(idStr)
containerConfig := &dkrcont.Config{
Image: stemcell.ID().AsString(),
ExposedPorts: map[dkrnat.Port]struct{}{}, // todo what ports?
// todo hacky umount to avoid conflicting with bosh dns updates
// todo why perms are wrong on /var/vcap/data
// todo dont need to create /var/vcap/store
Cmd: dkrstrslice.StrSlice{"bash", "-c", strings.Join([]string{
`umount /etc/resolv.conf`,
`umount /etc/hosts`,
`umount /etc/hostname`,
networkInitBashCmd,
`rm -rf /var/vcap/data/sys`,
`mkdir -p /var/vcap/data/sys`,
`mkdir -p /var/vcap/store`,
`exec env -i /usr/sbin/runsvdir-start`,
}, " && ")},
Env: []string{"reschedule:on-node-failure"},
}
if len(diskCIDs) > 0 {
node, err := f.possiblyFindNodeWithDisk(diskCIDs[0])
if err != nil {
return Container{}, bosherr.WrapError(err, "Finding node for disk")
}
if len(node) > 0 {
// Hard schedule on the same node that has the disk
// todo hopefully swarm handles this functionality in future?
containerConfig.Env = append(containerConfig.Env, "constraint:node=="+node)
}
}
vmProps.HostConfig.Privileged = true
vmProps.HostConfig.PublishAllPorts = true
for _, port := range vmProps.ExposedPorts {
containerConfig.ExposedPorts[dkrnat.Port(port)] = struct{}{}
}
vmProps = f.cleanMounts(vmProps)
vmProps.HostConfig.Binds = []string{EphemeralDiskCID{id}.AsString() + ":/var/vcap/data/"}
f.logger.Debug(f.logTag, "Creating container %#v, host %#v", containerConfig, &vmProps.HostConfig)
netConfig, additionalEndPtConfigs := splitNetworkSettings(netConfig)
container, err := f.dkrClient.ContainerCreate(
context.TODO(), containerConfig, &vmProps.HostConfig, netConfig, id.AsString())
if err != nil {
return Container{}, bosherr.WrapError(err, "Creating container")
}
f.logger.Debug(f.logTag,
"Connecting container '%s' to networks with opts %#v", container.ID, netConfig)
for name, endPtConfig := range additionalEndPtConfigs {
err := f.dkrClient.NetworkConnect(context.TODO(), name, id.AsString(), endPtConfig)
if err != nil {
return Container{}, bosherr.WrapErrorf(err, "Connecting container to network '%s'", name)
}
}
err = f.dkrClient.ContainerStart(context.TODO(), id.AsString(), dkrtypes.ContainerStartOptions{})
if err != nil {
f.cleanUpContainer(container)
return Container{}, bosherr.WrapError(err, "Starting container")
}
agentEnv := apiv1.AgentEnvFactory{}.ForVM(agentID, id, networks, env, f.agentOptions)
agentEnv.AttachSystemDisk("")
fileService := NewFileService(f.dkrClient, id, f.logger)
agentEnvService := NewFSAgentEnvService(fileService, f.logger)
err = agentEnvService.Update(agentEnv)
if err != nil {
f.cleanUpContainer(container)
return Container{}, bosherr.WrapError(err, "Updating container's agent env")
}
return NewContainer(id, f.dkrClient, agentEnvService, f.logger), nil
}
func (f Factory) Find(id apiv1.VMCID) (VM, error) {
fileService := NewFileService(f.dkrClient, id, f.logger)
agentEnvService := NewFSAgentEnvService(fileService, f.logger)
return NewContainer(id, f.dkrClient, agentEnvService, f.logger), nil
}
func (f Factory) cleanUpContainer(container dkrtypes.ContainerCreateResponse) {
// todo be more reselient at removal see Container#Delete()
rmOpts := dkrtypes.ContainerRemoveOptions{Force: true}
err := f.dkrClient.ContainerRemove(context.TODO(), container.ID, rmOpts)
if err != nil {
f.logger.Error(f.logTag, "Failed destroying container '%s': %s", container.ID, err.Error())
}
}
func (f Factory) possiblyFindNodeWithDisk(diskID apiv1.DiskCID) (string, error) {
resp, err := f.dkrClient.VolumeList(context.TODO(), dkrfilters.NewArgs())
if err != nil {
return "", bosherr.WrapError(err, "Listing volumes")
}
for _, vol := range resp.Volumes {
// Check if volume is available on any node
if strings.Contains(vol.Name, "/"+diskID.AsString()) {
return strings.SplitN(vol.Name, "/", 2)[0], nil
}
// Check if volume is available locally (no node prefix)
if vol.Name == diskID.AsString() {
return "", nil
}
}
// Did not find volume on any nodes
return "", nil
}
func (f Factory) cleanMounts(vmProps VMProps) VMProps {
const unixSock = "unix://"
for i, _ := range vmProps.HostConfig.Mounts {
// Strip off unix socker from sources for convenience of configuration
if strings.HasPrefix(vmProps.HostConfig.Mounts[i].Source, unixSock) {
vmProps.HostConfig.Mounts[i].Source =
strings.TrimPrefix(vmProps.HostConfig.Mounts[i].Source, unixSock)
}
}
return vmProps
}