forked from nginx-proxy/docker-gen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker_client.go
168 lines (149 loc) · 4.13 KB
/
docker_client.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
package main
import (
"fmt"
"log"
"strconv"
"strings"
docker "github.com/fsouza/go-dockerclient"
)
type DockerContainer struct {
}
// based off of https://github.com/dotcloud/docker/blob/2a711d16e05b69328f2636f88f8eac035477f7e4/utils/utils.go
func parseHost(addr string) (string, string, error) {
var (
proto string
host string
port int
)
addr = strings.TrimSpace(addr)
switch {
case addr == "tcp://":
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
case strings.HasPrefix(addr, "unix://"):
proto = "unix"
addr = strings.TrimPrefix(addr, "unix://")
if addr == "" {
addr = "/var/run/docker.sock"
}
case strings.HasPrefix(addr, "tcp://"):
proto = "tcp"
addr = strings.TrimPrefix(addr, "tcp://")
case strings.HasPrefix(addr, "fd://"):
return "fd", addr, nil
case addr == "":
proto = "unix"
addr = "/var/run/docker.sock"
default:
if strings.Contains(addr, "://") {
return "", "", fmt.Errorf("Invalid bind address protocol: %s", addr)
}
proto = "tcp"
}
if proto != "unix" && strings.Contains(addr, ":") {
hostParts := strings.Split(addr, ":")
if len(hostParts) != 2 {
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
}
if hostParts[0] != "" {
host = hostParts[0]
} else {
host = "127.0.0.1"
}
if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 {
port = p
} else {
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
}
} else if proto == "tcp" && !strings.Contains(addr, ":") {
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
} else {
host = addr
}
if proto == "unix" {
return proto, host, nil
}
return proto, fmt.Sprintf("%s:%d", host, port), nil
}
func splitDockerImage(img string) (string, string, string) {
index := 0
repository := img
var registry, tag string
if strings.Contains(img, "/") {
separator := strings.Index(img, "/")
registry = img[index:separator]
index = separator + 1
repository = img[index:]
}
if strings.Contains(repository, ":") {
separator := strings.Index(repository, ":")
tag = repository[separator+1:]
repository = repository[0:separator]
}
return registry, repository, tag
}
func getContainers(client *docker.Client) ([]*RuntimeContainer, error) {
apiContainers, err := client.ListContainers(docker.ListContainersOptions{
All: false,
Size: false,
})
if err != nil {
return nil, err
}
containers := []*RuntimeContainer{}
for _, apiContainer := range apiContainers {
container, err := client.InspectContainer(apiContainer.ID)
if err != nil {
log.Printf("error inspecting container: %s: %s\n", apiContainer.ID, err)
continue
}
registry, repository, tag := splitDockerImage(container.Config.Image)
runtimeContainer := &RuntimeContainer{
ID: container.ID,
Image: DockerImage{
Registry: registry,
Repository: repository,
Tag: tag,
},
Name: strings.TrimLeft(container.Name, "/"),
Hostname: container.Config.Hostname,
Gateway: container.NetworkSettings.Gateway,
Addresses: []Address{},
Env: make(map[string]string),
Volumes: make(map[string]Volume),
Node: SwarmNode{},
Labels: make(map[string]string),
IP: container.NetworkSettings.IPAddress,
}
for k, v := range container.NetworkSettings.Ports {
address := Address{
IP: container.NetworkSettings.IPAddress,
Port: k.Port(),
Proto: k.Proto(),
}
if len(v) > 0 {
address.HostPort = v[0].HostPort
address.HostIP = v[0].HostIP
}
runtimeContainer.Addresses = append(runtimeContainer.Addresses,
address)
}
for k, v := range container.Volumes {
runtimeContainer.Volumes[k] = Volume{
Path: k,
HostPath: v,
ReadWrite: container.VolumesRW[k],
}
}
if container.Node != nil {
runtimeContainer.Node.ID = container.Node.ID
runtimeContainer.Node.Name = container.Node.Name
runtimeContainer.Node.Address = Address{
IP: container.Node.IP,
}
}
runtimeContainer.Env = splitKeyValueSlice(container.Config.Env)
runtimeContainer.Labels = container.Config.Labels
containers = append(containers, runtimeContainer)
}
return containers, nil
}