forked from gliderlabs/registrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.go
227 lines (202 loc) · 5.19 KB
/
bridge.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
package main
import (
"log"
"net"
"os"
"path"
"strconv"
"strings"
"sync"
dockerapi "github.com/fsouza/go-dockerclient"
)
type PublishedPort struct {
HostPort string
HostIP string
HostName string
ExposedPort string
ExposedIP string
PortType string
Container *dockerapi.Container
}
type Service struct {
ID string
Name string
// HostName string
Port int
IP string
Tags []string
Attrs map[string]string
TTL int
pp PublishedPort
}
func CombineTags(tagParts ...string) []string {
tags := make([]string, 0)
for _, element := range tagParts {
if element != "" {
tags = append(tags, strings.Split(element, ",")...)
}
}
return tags
}
func NewService(port PublishedPort, isgroup bool) *Service {
container := port.Container
defaultName := strings.Split(path.Base(container.Config.Image), ":")[0]
if isgroup {
defaultName = defaultName + "-" + port.ExposedPort
}
hostname, err := os.Hostname()
if err != nil {
hostname = port.HostIP
} else {
if port.HostIP == "0.0.0.0" {
ip, err := net.ResolveIPAddr("ip", hostname)
if err == nil {
port.HostIP = ip.String()
}
}
}
if *hostIp != "" {
port.HostIP = *hostIp
}
metadata := serviceMetaData(container.Config.Env, port.ExposedPort)
ignore := mapdefault(metadata, "ignore", "")
if ignore != "" {
return nil
}
service := new(Service)
service.pp = port
service.ID = hostname + ":" + container.Name[1:] + ":" + port.ExposedPort
service.Name = mapdefault(metadata, "name", defaultName)
var p int
if *internal == true {
service.IP = port.ExposedIP
p, _ = strconv.Atoi(port.ExposedPort)
// service.HostName = port.HostName
} else {
service.IP = port.HostIP
p, _ = strconv.Atoi(port.HostPort)
}
service.Port = p
if port.PortType == "udp" {
service.Tags = CombineTags(mapdefault(metadata, "tags", ""), *forceTags, "udp")
service.ID = service.ID + ":udp"
} else {
service.Tags = CombineTags(mapdefault(metadata, "tags", ""), *forceTags)
}
id := mapdefault(metadata, "id", "")
if id != "" {
service.ID = id
}
delete(metadata, "id")
delete(metadata, "tags")
delete(metadata, "name")
service.Attrs = metadata
service.TTL = *refreshTtl
return service
}
func serviceMetaData(env []string, port string) map[string]string {
metadata := make(map[string]string)
for _, kv := range env {
kvp := strings.SplitN(kv, "=", 2)
if strings.HasPrefix(kvp[0], "SERVICE_") && len(kvp) > 1 {
key := strings.ToLower(strings.TrimPrefix(kvp[0], "SERVICE_"))
portkey := strings.SplitN(key, "_", 2)
_, err := strconv.Atoi(portkey[0])
if err == nil && len(portkey) > 1 {
if portkey[0] != port {
continue
}
metadata[portkey[1]] = kvp[1]
} else {
metadata[key] = kvp[1]
}
}
}
return metadata
}
type RegistryBridge struct {
sync.Mutex
docker *dockerapi.Client
registry ServiceRegistry
services map[string][]*Service
}
func (b *RegistryBridge) Add(containerId string) {
b.Lock()
defer b.Unlock()
container, err := b.docker.InspectContainer(containerId)
if err != nil {
log.Println("registrator: unable to inspect container:", containerId[:12], err)
return
}
ports := make([]PublishedPort, 0)
for port, published := range container.NetworkSettings.Ports {
var hp, hip string
if len(published) > 0 {
hp = published[0].HostPort
hip = published[0].HostIP
}
p := strings.Split(string(port), "/")
ports = append(ports, PublishedPort{
HostPort: hp,
HostIP: hip,
HostName: container.Config.Hostname,
ExposedPort: p[0],
ExposedIP: container.NetworkSettings.IPAddress,
PortType: p[1],
Container: container,
})
// }
}
for _, port := range ports {
if *internal != true && port.HostPort == "" {
log.Println("registrator: ignored", container.ID[:12], "port", port.ExposedPort, "not published on host")
continue
}
service := NewService(port, len(ports) > 1)
if service == nil {
log.Println("registrator: ignored:", container.ID[:12], "service on port", port.ExposedPort)
continue
}
err := retry(func() error {
return b.registry.Register(service)
})
if err != nil {
log.Println("registrator: unable to register service:", service, err)
continue
}
b.services[container.ID] = append(b.services[container.ID], service)
log.Println("registrator: added:", container.ID[:12], service.ID)
}
if len(b.services) == 0 {
log.Println("registrator: ignored:", container.ID[:12], "no published ports")
}
}
func (b *RegistryBridge) Remove(containerId string) {
b.Lock()
defer b.Unlock()
for _, service := range b.services[containerId] {
err := retry(func() error {
return b.registry.Deregister(service)
})
if err != nil {
log.Println("registrator: unable to deregister service:", service.ID, err)
continue
}
log.Println("registrator: removed:", containerId[:12], service.ID)
}
delete(b.services, containerId)
}
func (b *RegistryBridge) Refresh() {
b.Lock()
defer b.Unlock()
for containerId, services := range b.services {
for _, service := range services {
err := b.registry.Refresh(service)
if err != nil {
log.Println("registrator: unable to refresh service:", service.ID, err)
continue
}
log.Println("registrator: refreshed:", containerId[:12], service.ID)
}
}
}