forked from goodrain/rainbond
/
version12.go
396 lines (345 loc) · 13.5 KB
/
version12.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
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package compose
import (
"fmt"
"net"
"path/filepath"
"strconv"
"strings"
"github.com/Sirupsen/logrus"
"github.com/docker/libcompose/project"
)
// Parse Docker Compose with libcompose (only supports v1 and v2). Eventually we will
// switch to using only libcompose once v3 is supported.
func parseV1V2(bodys [][]byte) (ComposeObject, error) {
// Gather the appropriate context for parsing
context := &project.Context{}
context.ComposeBytes = bodys
// Load the context and let's start parsing
composeObject := project.NewProject(context, nil, nil)
err := composeObject.Parse()
if err != nil {
return ComposeObject{}, fmt.Errorf("composeObject.Parse() failed, Failed to load compose file,%s", err)
}
noSupKeys := checkUnsupportedKey(composeObject)
for _, keyName := range noSupKeys {
logrus.Warningf("Unsupported %s key - ignoring", keyName)
}
// Map the parsed struct to a struct we understand (kobject)
co, err := libComposeToKomposeMapping(composeObject)
if err != nil {
return ComposeObject{}, err
}
return co, nil
}
// Load ports from compose file
func loadPorts(composePorts []string) ([]Ports, error) {
ports := []Ports{}
character := ":"
// For each port listed
for _, port := range composePorts {
// Get the TCP / UDP protocol. Checks to see if it splits in 2 with '/' character.
// ex. 15000:15000/tcp
// else, set a default protocol of using TCP
proto := "TCP"
protocolCheck := strings.Split(port, "/")
if len(protocolCheck) == 2 {
if strings.EqualFold("tcp", protocolCheck[1]) {
proto = "TCP"
} else if strings.EqualFold("udp", protocolCheck[1]) {
proto = "UDP"
} else {
return nil, fmt.Errorf("invalid protocol %q", protocolCheck[1])
}
}
// Split up the ports / IP without the "/tcp" or "/udp" appended to it
justPorts := strings.Split(protocolCheck[0], character)
if len(justPorts) == 3 {
// ex. 127.0.0.1:80:80
// Get the IP address
hostIP := justPorts[0]
ip := net.ParseIP(hostIP)
if ip.To4() == nil && ip.To16() == nil {
return nil, fmt.Errorf("%q contains an invalid IPv4 or IPv6 IP address", port)
}
// Get the host port
hostPortInt, err := strconv.Atoi(justPorts[1])
if err != nil {
return nil, fmt.Errorf("invalid host port %q valid example: 127.0.0.1:80:80", port)
}
// Get the container port
containerPortInt, err := strconv.Atoi(justPorts[2])
if err != nil {
return nil, fmt.Errorf("invalid container port %q valid example: 127.0.0.1:80:80", port)
}
// Convert to a kobject struct with ports as well as IP
ports = append(ports, Ports{
HostPort: int32(hostPortInt),
ContainerPort: int32(containerPortInt),
HostIP: hostIP,
Protocol: proto,
})
} else if len(justPorts) == 2 {
// ex. 80:80
// Get the host port
hostPortInt, err := strconv.Atoi(justPorts[0])
if err != nil {
return nil, fmt.Errorf("invalid host port %q valid example: 80:80", port)
}
// Get the container port
containerPortInt, err := strconv.Atoi(justPorts[1])
if err != nil {
return nil, fmt.Errorf("invalid container port %q valid example: 80:80", port)
}
// Convert to a kobject struct and add to the list of ports
ports = append(ports, Ports{
HostPort: int32(hostPortInt),
ContainerPort: int32(containerPortInt),
Protocol: proto,
})
} else {
// ex. 80
containerPortInt, err := strconv.Atoi(justPorts[0])
if err != nil {
return nil, fmt.Errorf("invalid container port %q valid example: 80", port)
}
ports = append(ports, Ports{
ContainerPort: int32(containerPortInt),
Protocol: proto,
})
}
}
return ports, nil
}
// Uses libcompose's APIProject type and converts it to a Kompose object for us to understand
func libComposeToKomposeMapping(composeObject *project.Project) (ComposeObject, error) {
// Initialize what's going to be returned
co := ComposeObject{
ServiceConfigs: make(map[string]ServiceConfig),
}
// Here we "clean up" the service configuration so we return something that includes
// all relevant information as well as avoid the unsupported keys as well.
for name, composeServiceConfig := range composeObject.ServiceConfigs.All() {
serviceConfig := ServiceConfig{}
serviceConfig.Image = composeServiceConfig.Image
serviceConfig.Build = composeServiceConfig.Build.Context
newName := normalizeServiceNames(composeServiceConfig.ContainerName)
serviceConfig.ContainerName = newName
if serviceConfig.ContainerName == "" {
serviceConfig.ContainerName = name
}
logrus.Debugf("newName is %s, name is %s", newName, name)
if newName != composeServiceConfig.ContainerName {
logrus.Infof("Container name in service %q has been changed from %q to %q", name, composeServiceConfig.ContainerName, newName)
}
serviceConfig.Command = composeServiceConfig.Entrypoint
serviceConfig.Args = composeServiceConfig.Command
serviceConfig.Dockerfile = composeServiceConfig.Build.Dockerfile
serviceConfig.BuildArgs = composeServiceConfig.Build.Args
envs := loadEnvVars(composeServiceConfig.Environment)
serviceConfig.Environment = envs
// Validate dockerfile path
if filepath.IsAbs(serviceConfig.Dockerfile) {
logrus.Fatalf("%q defined in service %q is an absolute path, it must be a relative path.", serviceConfig.Dockerfile, name)
}
// load ports
ports, err := loadPorts(composeServiceConfig.Ports)
if err != nil {
return ComposeObject{}, fmt.Errorf("loadPorts failed. " + name + " failed to load ports from compose file")
}
serviceConfig.Port = ports
serviceConfig.WorkingDir = composeServiceConfig.WorkingDir
if composeServiceConfig.Volumes != nil {
for _, volume := range composeServiceConfig.Volumes.Volumes {
v := normalizeServiceNames(volume.String())
serviceConfig.VolList = append(serviceConfig.VolList, v)
}
}
// canonical "Custom Labels" handler
// Labels used to influence conversion of kompose will be handled
// from here for docker-compose. Each loader will have such handler.
for key, value := range composeServiceConfig.Labels {
switch key {
case "kompose.service.type":
serviceType, err := handleServiceType(value)
if err != nil {
return ComposeObject{}, fmt.Errorf("handleServiceType failed")
}
serviceConfig.ServiceType = serviceType
case "kompose.service.expose":
serviceConfig.ExposeService = strings.ToLower(value)
case "kompose.service.expose.tls-secret":
serviceConfig.ExposeServiceTLS = value
}
}
if serviceConfig.ExposeService == "" && serviceConfig.ExposeServiceTLS != "" {
return ComposeObject{}, fmt.Errorf("kompose.service.expose.tls-secret was specifed without kompose.service.expose")
}
err = checkLabelsPorts(len(serviceConfig.Port), composeServiceConfig.Labels["kompose.service.type"], name)
if err != nil {
return ComposeObject{}, fmt.Errorf("kompose.service.type can't be set if service doesn't expose any ports.")
}
logrus.Debugf("inner service conf is %v", composeServiceConfig)
// convert compose labels to annotations
serviceConfig.Annotations = map[string]string(composeServiceConfig.Labels)
serviceConfig.CPUQuota = int64(composeServiceConfig.CPUQuota)
serviceConfig.CapAdd = composeServiceConfig.CapAdd
serviceConfig.CapDrop = composeServiceConfig.CapDrop
serviceConfig.Pid = composeServiceConfig.Pid
serviceConfig.Expose = composeServiceConfig.Expose
serviceConfig.Privileged = composeServiceConfig.Privileged
serviceConfig.Restart = composeServiceConfig.Restart
serviceConfig.User = composeServiceConfig.User
serviceConfig.VolumesFrom = composeServiceConfig.VolumesFrom
serviceConfig.Stdin = composeServiceConfig.StdinOpen
serviceConfig.Tty = composeServiceConfig.Tty
serviceConfig.MemLimit = composeServiceConfig.MemLimit
serviceConfig.TmpFs = composeServiceConfig.Tmpfs
serviceConfig.StopGracePeriod = composeServiceConfig.StopGracePeriod
serviceConfig.DependsON = composeServiceConfig.DependsOn
serviceConfig.Links = composeServiceConfig.Links
// Get GroupAdd, group should be mentioned in gid format but not the group name
groupAdd, err := getGroupAdd(composeServiceConfig.GroupAdd)
if err != nil {
return ComposeObject{}, fmt.Errorf("GroupAdd should be mentioned in gid format, not a group name")
}
serviceConfig.GroupAdd = groupAdd
co.ServiceConfigs[normalizeServiceNames(name)] = serviceConfig
if normalizeServiceNames(name) != name {
logrus.Infof("Service name in docker-compose has been changed from %q to %q", name, normalizeServiceNames(name))
}
}
// This will handle volume at earlier stage itself, it will resolves problems occurred due to `volumes_from` key
handleVolume(&co)
return co, nil
}
// This function will retrieve volumes for each service, as well as it will parse volume information and store it in Volumes struct
func handleVolume(ComposeObject *ComposeObject) {
for name := range ComposeObject.ServiceConfigs {
// retrieve volumes of service
vols, err := retrieveVolume(name, *ComposeObject)
if err != nil {
logrus.Errorf("could not retrieve volume %s", err.Error())
}
// We can't assign value to struct field in map while iterating over it, so temporary variable `temp` is used here
var temp = ComposeObject.ServiceConfigs[name]
temp.Volumes = vols
ComposeObject.ServiceConfigs[name] = temp
}
}
func checkLabelsPorts(noOfPort int, labels string, svcName string) error {
if noOfPort == 0 && (labels == "NodePort" || labels == "LoadBalancer") {
return fmt.Errorf("%s defined in service %s with no ports present. Issues may occur when bringing up artifacts", labels, svcName)
}
return nil
}
// returns all volumes associated with service, if `volumes_from` key is used, we have to retrieve volumes from the services which are mentioned there. Hence, recursive function is used here.
func retrieveVolume(svcName string, ComposeObject ComposeObject) (volume []Volumes, err error) {
// if volumes-from key is present
if ComposeObject.ServiceConfigs[svcName].VolumesFrom != nil {
// iterating over services from `volumes-from`
for _, depSvc := range ComposeObject.ServiceConfigs[svcName].VolumesFrom {
// recursive call for retrieving volumes of services from `volumes-from`
dVols, err := retrieveVolume(depSvc, ComposeObject)
if err != nil {
return nil, fmt.Errorf("could not retrieve the volume")
}
var cVols []Volumes
cVols, err = ParseVols(ComposeObject.ServiceConfigs[svcName].VolList, svcName)
if err != nil {
return nil, fmt.Errorf("error generting current volumes")
}
for _, cv := range cVols {
// check whether volumes of current service is same or not as that of dependent volumes coming from `volumes-from`
ok, dv := getVol(cv, dVols)
if ok {
// change current volumes service name to dependent service name
if dv.VFrom == "" {
cv.VFrom = dv.SvcName
cv.SvcName = dv.SvcName
} else {
cv.VFrom = dv.VFrom
cv.SvcName = dv.SvcName
}
cv.PVCName = dv.PVCName
}
volume = append(volume, cv)
}
// iterating over dependent volumes
for _, dv := range dVols {
// check whether dependent volume is already present or not
if checkVolDependent(dv, volume) {
// if found, add service name to `VFrom`
dv.VFrom = dv.SvcName
volume = append(volume, dv)
}
}
}
} else {
// if `volumes-from` is not present
volume, err = ParseVols(ComposeObject.ServiceConfigs[svcName].VolList, svcName)
if err != nil {
return nil, fmt.Errorf("error generting current volumes")
}
}
return
}
// checkVolDependent returns false if dependent volume is present
func checkVolDependent(dv Volumes, volume []Volumes) bool {
for _, vol := range volume {
if vol.PVCName == dv.PVCName {
return false
}
}
return true
}
func ParseVols(volNames []string, svcName string) ([]Volumes, error) {
var volumes []Volumes
var err error
for i, vn := range volNames {
var v Volumes
v.VolumeName, v.Host, v.Container, v.Mode, err = ParseVolume(vn)
if err != nil {
return nil, fmt.Errorf("could not parse volume %q: %v", vn, err)
}
v.SvcName = svcName
v.MountPath = fmt.Sprintf("%s:%s", v.Host, v.Container)
v.PVCName = fmt.Sprintf("%s-claim%d", v.SvcName, i)
volumes = append(volumes, v)
}
return volumes, nil
}
// for dependent volumes, returns true and the respective volume if mountpath are same
func getVol(toFind Volumes, Vols []Volumes) (bool, Volumes) {
for _, dv := range Vols {
if toFind.MountPath == dv.MountPath {
return true, dv
}
}
return false, Volumes{}
}
// getGroupAdd will return group in int64 format
func getGroupAdd(group []string) ([]int64, error) {
var groupAdd []int64
for _, i := range group {
j, err := strconv.Atoi(i)
if err != nil {
return nil, fmt.Errorf("unable to get group_add key")
}
groupAdd = append(groupAdd, int64(j))
}
return groupAdd, nil
}