@@ -6,16 +6,17 @@ import (
6
6
"net"
7
7
8
8
"github.com/Sirupsen/logrus"
9
+ mapset "github.com/deckarep/golang-set"
9
10
"github.com/docker/libnetwork/common"
10
11
)
11
12
12
- func (c * controller ) addEndpointNameResolution (svcName , svcID , nID , eID , containerName string , vip net.IP , serviceAliases , taskAliases []string , ip net.IP , addService bool , method string ) error {
13
+ func (c * controller ) addEndpointNameResolution (svcName , svcID , nID , eID , containerName string , vip net.IP , serviceAliases , taskAliases []string , ip net.IP , method string ) error {
13
14
n , err := c .NetworkByID (nID )
14
15
if err != nil {
15
16
return err
16
17
}
17
18
18
- logrus .Debugf ("addEndpointNameResolution %s %s add_service:%t " , eID , svcName , addService )
19
+ logrus .Debugf ("addEndpointNameResolution %s %s add_service" , eID , svcName )
19
20
20
21
// Add container resolution mappings
21
22
c .addContainerNameResolution (nID , eID , containerName , taskAliases , ip , method )
@@ -27,18 +28,14 @@ func (c *controller) addEndpointNameResolution(svcName, svcID, nID, eID, contain
27
28
}
28
29
29
30
// Add service name to vip in DNS, if vip is valid. Otherwise resort to DNS RR
31
+ svcIP := vip
30
32
if len (vip ) == 0 {
31
- n .(* network ).addSvcRecords (eID , svcName , ip , nil , false , method )
32
- for _ , alias := range serviceAliases {
33
- n .(* network ).addSvcRecords (eID , alias , ip , nil , false , method )
34
- }
33
+ svcIP = ip
35
34
}
36
35
37
- if addService && len (vip ) != 0 {
38
- n .(* network ).addSvcRecords (eID , svcName , vip , nil , false , method )
39
- for _ , alias := range serviceAliases {
40
- n .(* network ).addSvcRecords (eID , alias , vip , nil , false , method )
41
- }
36
+ n .(* network ).addSvcRecords (eID , svcName , svcIP , nil , false , method )
37
+ for _ , alias := range serviceAliases {
38
+ n .(* network ).addSvcRecords (eID , alias , svcIP , nil , false , method )
42
39
}
43
40
44
41
return nil
@@ -62,39 +59,38 @@ func (c *controller) addContainerNameResolution(nID, eID, containerName string,
62
59
return nil
63
60
}
64
61
65
- func (c * controller ) deleteEndpointNameResolution (svcName , svcID , nID , eID , containerName string , vip net.IP , serviceAliases , taskAliases []string , ip net.IP , rmService , multipleEntries bool , method string ) error {
62
+ func (c * controller ) deleteEndpointNameResolution (svcName , svcID , nID , eID , containerName string , vip net.IP , serviceAliases , tasksServiceAliases , taskAliases []string , ip net.IP , rmService , multipleEntries bool , method string ) error {
66
63
n , err := c .NetworkByID (nID )
67
64
if err != nil {
68
65
return err
69
66
}
70
67
71
68
logrus .Debugf ("deleteEndpointNameResolution %s %s rm_service:%t suppress:%t" , eID , svcName , rmService , multipleEntries )
72
-
73
69
// Delete container resolution mappings
74
70
c .delContainerNameResolution (nID , eID , containerName , taskAliases , ip , method )
75
71
72
+ if multipleEntries {
73
+ return nil
74
+ }
75
+
76
76
// Delete the special "tasks.svc_name" backend record.
77
- if ! multipleEntries {
78
- n .(* network ).deleteSvcRecords (eID , "tasks." + svcName , ip , nil , false , method )
79
- for _ , alias := range serviceAliases {
80
- n .(* network ).deleteSvcRecords (eID , "tasks." + alias , ip , nil , false , method )
81
- }
77
+ n .(* network ).deleteSvcRecords (eID , "tasks." + svcName , ip , nil , false , method )
78
+ for _ , alias := range tasksServiceAliases {
79
+ n .(* network ).deleteSvcRecords (eID , "tasks." + alias , ip , nil , false , method )
82
80
}
83
81
84
82
// If we are doing DNS RR delete the endpoint IP from DNS record right away.
85
- if ! multipleEntries && len (vip ) == 0 {
86
- n .(* network ).deleteSvcRecords (eID , svcName , ip , nil , false , method )
87
- for _ , alias := range serviceAliases {
88
- n .(* network ).deleteSvcRecords (eID , alias , ip , nil , false , method )
89
- }
83
+ svcIP := vip
84
+ if len (vip ) == 0 {
85
+ svcIP = ip
90
86
}
91
87
92
- // Remove the DNS record for VIP only if we are removing the service
93
- if rmService && len ( vip ) != 0 && ! multipleEntries {
94
- n .( * network ). deleteSvcRecords ( eID , svcName , vip , nil , false , method )
95
- for _ , alias := range serviceAliases {
96
- n .( * network ). deleteSvcRecords ( eID , alias , vip , nil , false , method )
97
- }
88
+ if rmService {
89
+ n .( * network ). deleteSvcRecords ( eID , svcName , svcIP , nil , false , method )
90
+ }
91
+
92
+ for _ , alias := range serviceAliases {
93
+ n .( * network ). deleteSvcRecords ( eID , alias , svcIP , nil , false , method )
98
94
}
99
95
100
96
return nil
@@ -118,13 +114,12 @@ func (c *controller) delContainerNameResolution(nID, eID, containerName string,
118
114
return nil
119
115
}
120
116
121
- func newService (name string , id string , ingressPorts []* PortConfig , serviceAliases [] string ) * service {
117
+ func newService (name string , id string , ingressPorts []* PortConfig ) * service {
122
118
return & service {
123
119
name : name ,
124
120
id : id ,
125
121
ingressPorts : ingressPorts ,
126
122
loadBalancers : make (map [string ]* loadBalancer ),
127
- aliases : serviceAliases ,
128
123
ipToEndpoint : common .NewSetMatrix (),
129
124
}
130
125
}
@@ -180,7 +175,7 @@ func (c *controller) cleanupServiceBindings(cleanupNID string) {
180
175
181
176
cleanupFuncs = append (cleanupFuncs , func () {
182
177
if err := c .rmServiceBinding (service .name , service .id , networkID , epID , be .containerName , loadBalancer .vip ,
183
- service .ingressPorts , service . aliases , be .taskAliases , epIP , "cleanupServiceBindings" ); err != nil {
178
+ service .ingressPorts , be . serviceAliases , be .taskAliases , epIP , "cleanupServiceBindings" ); err != nil {
184
179
logrus .Errorf ("Failed to remove service bindings for service %s network %s endpoint %s while cleanup: %v" ,
185
180
service .id , networkID , epID , err )
186
181
}
@@ -197,8 +192,6 @@ func (c *controller) cleanupServiceBindings(cleanupNID string) {
197
192
}
198
193
199
194
func (c * controller ) addServiceBinding (svcName , svcID , nID , eID , containerName string , vip net.IP , ingressPorts []* PortConfig , serviceAliases , taskAliases []string , ip net.IP , method string ) error {
200
- var addService bool
201
-
202
195
n , err := c .NetworkByID (nID )
203
196
if err != nil {
204
197
return err
@@ -217,7 +210,7 @@ func (c *controller) addServiceBinding(svcName, svcID, nID, eID, containerName s
217
210
if ! ok {
218
211
// Create a new service if we are seeing this service
219
212
// for the first time.
220
- s = newService (svcName , svcID , ingressPorts , serviceAliases )
213
+ s = newService (svcName , svcID , ingressPorts )
221
214
c .serviceBindings [skey ] = s
222
215
}
223
216
c .Unlock ()
@@ -250,12 +243,12 @@ func (c *controller) addServiceBinding(svcName, svcID, nID, eID, containerName s
250
243
fwMarkCtrMu .Unlock ()
251
244
252
245
s .loadBalancers [nID ] = lb
253
- addService = true
254
246
}
255
247
256
248
lb .backEnds [eID ] = loadBalancerBackend {ip : ip ,
257
- containerName : containerName ,
258
- taskAliases : taskAliases }
249
+ containerName : containerName ,
250
+ serviceAliases : serviceAliases ,
251
+ taskAliases : taskAliases }
259
252
260
253
ok , entries := s .assignIPToEndpoint (ip .String (), eID )
261
254
if ! ok || entries > 1 {
@@ -270,15 +263,14 @@ func (c *controller) addServiceBinding(svcName, svcID, nID, eID, containerName s
270
263
}
271
264
272
265
// Add the appropriate name resolutions
273
- c .addEndpointNameResolution (svcName , svcID , nID , eID , containerName , vip , serviceAliases , taskAliases , ip , addService , "addServiceBinding" )
266
+ c .addEndpointNameResolution (svcName , svcID , nID , eID , containerName , vip , serviceAliases , taskAliases , ip , "addServiceBinding" )
274
267
275
268
logrus .Debugf ("addServiceBinding from %s END for %s %s" , method , svcName , eID )
276
269
277
270
return nil
278
271
}
279
272
280
273
func (c * controller ) rmServiceBinding (svcName , svcID , nID , eID , containerName string , vip net.IP , ingressPorts []* PortConfig , serviceAliases []string , taskAliases []string , ip net.IP , method string ) error {
281
-
282
274
var rmService bool
283
275
284
276
n , err := c .NetworkByID (nID )
@@ -308,7 +300,7 @@ func (c *controller) rmServiceBinding(svcName, svcID, nID, eID, containerName st
308
300
return nil
309
301
}
310
302
311
- _ , ok = lb .backEnds [eID ]
303
+ be , ok : = lb .backEnds [eID ]
312
304
if ! ok {
313
305
logrus .Warnf ("rmServiceBinding %s %s %s aborted lb.backEnds[eid] !ok" , method , svcName , eID )
314
306
return nil
@@ -347,8 +339,45 @@ func (c *controller) rmServiceBinding(svcName, svcID, nID, eID, containerName st
347
339
n .(* network ).rmLBBackend (ip , vip , lb .fwMark , ingressPorts , rmService )
348
340
}
349
341
342
+ // Always clean up all possible aliases for the special "tasks.<alias>" names.
343
+ tasksServiceAliases := serviceAliases
344
+ logrus .Debugf ("tasksServiceAliases %s" , tasksServiceAliases )
345
+
346
+ if len (vip ) == 0 {
347
+ // If not using a VIP then always remove the service and all its aliases.
348
+ rmService = true
349
+ } else if ! rmService {
350
+ // Make sure to only remove the VIP when the last alias referencing it has
351
+ // been removed, but remove any service aliases that are no longer needed.
352
+ epAliasSet := mapset .NewSet ()
353
+ for _ , alias := range serviceAliases {
354
+ epAliasSet .Add (alias )
355
+ }
356
+
357
+ // This had better be redundant but just in case...
358
+ for _ , alias := range be .serviceAliases {
359
+ epAliasSet .Add (alias )
360
+ }
361
+
362
+ logrus .Debugf ("epAliasSet %s" , epAliasSet )
363
+
364
+ sAliasSet := s .aliasSet ()
365
+ logrus .Debugf ("sAliasSet %s" , sAliasSet )
366
+
367
+ for alias := range sAliasSet .Iter () {
368
+ epAliasSet .Remove (alias )
369
+ }
370
+
371
+ serviceAliases = make ([]string , 0 , epAliasSet .Cardinality ())
372
+ for alias := range epAliasSet .Iter () {
373
+ serviceAliases = append (serviceAliases , alias .(string ))
374
+ }
375
+ }
376
+
377
+ logrus .Debugf ("serviceAliases %s" , serviceAliases )
378
+
350
379
// Delete the name resolutions
351
- c .deleteEndpointNameResolution (svcName , svcID , nID , eID , containerName , vip , serviceAliases , taskAliases , ip , rmService , entries > 0 , "rmServiceBinding" )
380
+ c .deleteEndpointNameResolution (svcName , svcID , nID , eID , containerName , vip , serviceAliases , tasksServiceAliases , taskAliases , ip , rmService , entries > 0 , "rmServiceBinding" )
352
381
353
382
logrus .Debugf ("rmServiceBinding from %s END for %s %s" , method , svcName , eID )
354
383
return nil
0 commit comments