-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
69 lines (57 loc) · 1.83 KB
/
utils.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
package docker
import (
"path/filepath"
"strings"
"github.com/docker/cli/cli/compose/loader"
dockertypes "github.com/docker/docker/api/types"
dockermount "github.com/docker/docker/api/types/mount"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
)
func ParseVolumes(specs ...string) (volumes map[string]struct{}, binds []string, err error) {
volumes = make(map[string]struct{})
for _, spec := range specs {
parsed, err := loader.ParseVolume(spec)
if err != nil {
return nil, nil, err
}
if parsed.Source != "" {
toBind := spec
if parsed.Type == string(dockermount.TypeBind) {
if arr := strings.SplitN(spec, ":", 2); len(arr) == 2 {
hostPart := arr[0]
if strings.HasPrefix(hostPart, "."+string(filepath.Separator)) || hostPart == "." {
if absHostPart, err := filepath.Abs(hostPart); err == nil {
hostPart = absHostPart
}
}
toBind = hostPart + ":" + arr[1]
}
}
// after creating the bind mount we want to delete it from the copts.volumes values because
// we do not want bind mounts being committed to image configs
binds = append(binds, toBind)
} else {
volumes[spec] = struct{}{}
}
}
return
}
// GetPortBindings returns the list of port binding to access container on given port
func GetPortBindings(port string, container *dockertypes.ContainerJSON) ([]nat.PortBinding, error) {
proto := "tcp"
parts := strings.SplitN(port, "/", 2)
if len(parts) == 2 && parts[1] == "" {
port = parts[0]
proto = parts[1]
}
natPort := port + "/" + proto
newP, err := nat.NewPort(proto, port)
if err != nil {
return nil, err
}
if frontends, exists := container.NetworkSettings.Ports[newP]; exists && frontends != nil {
return frontends, nil
}
return nil, errors.Errorf("Error: No public port '%s' published for container %s", natPort, container.ID)
}