Browse code

Call deactivateServiceBinding when necessary

Signed-off-by: Dong Chen <dongluo.chen@docker.com>

Dong Chen authored on 2017/03/01 09:12:24
Showing 1 changed files
... ...
@@ -307,6 +307,24 @@ func (r *controller) Wait(pctx context.Context) error {
307 307
 	return nil
308 308
 }
309 309
 
310
+func (r *controller) hasServiceBinding() bool {
311
+	if r.task == nil {
312
+		return false
313
+	}
314
+
315
+	// service is attached to a network besides the default bridge
316
+	for _, na := range r.task.Networks {
317
+		if na.Network == nil ||
318
+			na.Network.DriverState == nil ||
319
+			na.Network.DriverState.Name == "bridge" && na.Network.Spec.Annotations.Name == "bridge" {
320
+			continue
321
+		}
322
+		return true
323
+	}
324
+
325
+	return false
326
+}
327
+
310 328
 // Shutdown the container cleanly.
311 329
 func (r *controller) Shutdown(ctx context.Context) error {
312 330
 	if err := r.checkClosed(); err != nil {
... ...
@@ -317,20 +335,19 @@ func (r *controller) Shutdown(ctx context.Context) error {
317 317
 		r.cancelPull()
318 318
 	}
319 319
 
320
-	// remove container from service binding
321
-	if err := r.adapter.deactivateServiceBinding(); err != nil {
322
-		log.G(ctx).WithError(err).Warningf("failed to deactivate service binding for container %s", r.adapter.container.name())
323
-		// Don't return an error here, because failure to deactivate
324
-		// the service binding is expected if the container was never
325
-		// started.
326
-	}
320
+	if r.hasServiceBinding() {
321
+		// remove container from service binding
322
+		if err := r.adapter.deactivateServiceBinding(); err != nil {
323
+			log.G(ctx).WithError(err).Warningf("failed to deactivate service binding for container %s", r.adapter.container.name())
324
+			// Don't return an error here, because failure to deactivate
325
+			// the service binding is expected if the container was never
326
+			// started.
327
+		}
327 328
 
328
-	// add a delay for gossip converge
329
-	// TODO(dongluochen): this delay shoud be configurable to fit different cluster size and network delay.
330
-	// TODO(dongluochen): if there is no service binding for this container, this delay is not necessary.
331
-	// if r.adapter.deactivateServiceBinding can return an error to indicate that, it'd reduce service
332
-	// converge time.
333
-	time.Sleep(defaultGossipConvergeDelay)
329
+		// add a delay for gossip converge
330
+		// TODO(dongluochen): this delay shoud be configurable to fit different cluster size and network delay.
331
+		time.Sleep(defaultGossipConvergeDelay)
332
+	}
334 333
 
335 334
 	if err := r.adapter.shutdown(ctx); err != nil {
336 335
 		if isUnknownContainer(err) || isStoppedContainer(err) {