Browse code

Remove a network during task SHUTDOWN instead of REMOVE to make sure the LB sandbox is removed when a service is updated with a --network-rm option

Signed-off-by: Arko Dasgupta <arko.dasgupta@docker.com>
(cherry picked from commit 680d0ba4abfb00c8ac959638c72003dba0826b46)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Arko Dasgupta authored on 2019/05/07 00:46:12
Showing 2 changed files
... ...
@@ -376,6 +376,15 @@ func (r *controller) Shutdown(ctx context.Context) error {
376 376
 		return err
377 377
 	}
378 378
 
379
+	// Try removing networks referenced in this task in case this
380
+	// task is the last one referencing it
381
+	if err := r.adapter.removeNetworks(ctx); err != nil {
382
+		if isUnknownContainer(err) {
383
+			return nil
384
+		}
385
+		return err
386
+	}
387
+
379 388
 	return nil
380 389
 }
381 390
 
... ...
@@ -419,15 +428,6 @@ func (r *controller) Remove(ctx context.Context) error {
419 419
 		log.G(ctx).WithError(err).Debug("shutdown failed on removal")
420 420
 	}
421 421
 
422
-	// Try removing networks referenced in this task in case this
423
-	// task is the last one referencing it
424
-	if err := r.adapter.removeNetworks(ctx); err != nil {
425
-		if isUnknownContainer(err) {
426
-			return nil
427
-		}
428
-		return err
429
-	}
430
-
431 422
 	if err := r.adapter.remove(ctx); err != nil {
432 423
 		if isUnknownContainer(err) {
433 424
 			return nil
... ...
@@ -7,6 +7,7 @@ import (
7 7
 	"github.com/docker/docker/api/types"
8 8
 	swarmtypes "github.com/docker/docker/api/types/swarm"
9 9
 	"github.com/docker/docker/client"
10
+	"github.com/docker/docker/integration/internal/network"
10 11
 	"github.com/docker/docker/integration/internal/swarm"
11 12
 	"gotest.tools/assert"
12 13
 	is "gotest.tools/assert/cmp"
... ...
@@ -194,6 +195,59 @@ func TestServiceUpdateConfigs(t *testing.T) {
194 194
 	assert.NilError(t, err)
195 195
 }
196 196
 
197
+func TestServiceUpdateNetwork(t *testing.T) {
198
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
199
+	defer setupTest(t)()
200
+	d := swarm.NewSwarm(t, testEnv)
201
+	defer d.Stop(t)
202
+	cli := d.NewClientT(t)
203
+	defer cli.Close()
204
+
205
+	ctx := context.Background()
206
+
207
+	// Create a overlay network
208
+	testNet := "testNet" + t.Name()
209
+	overlayID := network.CreateNoError(t, ctx, cli, testNet,
210
+		network.WithDriver("overlay"))
211
+
212
+	var instances uint64 = 1
213
+	// Create service with the overlay network
214
+	serviceName := "TestServiceUpdateNetworkRM_" + t.Name()
215
+	serviceID := swarm.CreateService(t, d,
216
+		swarm.ServiceWithReplicas(instances),
217
+		swarm.ServiceWithName(serviceName),
218
+		swarm.ServiceWithNetwork(testNet))
219
+
220
+	poll.WaitOn(t, swarm.RunningTasksCount(cli, serviceID, instances), swarm.ServicePoll)
221
+	service := getService(t, cli, serviceID)
222
+	netInfo, err := cli.NetworkInspect(ctx, testNet, types.NetworkInspectOptions{
223
+		Verbose: true,
224
+		Scope:   "swarm",
225
+	})
226
+	assert.NilError(t, err)
227
+	assert.Assert(t, len(netInfo.Containers) == 2, "Expected 2 endpoints, one for container and one for LB Sandbox")
228
+
229
+	//Remove network from service
230
+	service.Spec.TaskTemplate.Networks = []swarmtypes.NetworkAttachmentConfig{}
231
+	_, err = cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{})
232
+	assert.NilError(t, err)
233
+	poll.WaitOn(t, serviceIsUpdated(cli, serviceID), swarm.ServicePoll)
234
+
235
+	netInfo, err = cli.NetworkInspect(ctx, testNet, types.NetworkInspectOptions{
236
+		Verbose: true,
237
+		Scope:   "swarm",
238
+	})
239
+
240
+	assert.NilError(t, err)
241
+	assert.Assert(t, len(netInfo.Containers) == 0, "Load balancing endpoint still exists in network")
242
+
243
+	err = cli.NetworkRemove(ctx, overlayID)
244
+	assert.NilError(t, err)
245
+
246
+	err = cli.ServiceRemove(ctx, serviceID)
247
+	assert.NilError(t, err)
248
+}
249
+
197 250
 func getService(t *testing.T, cli client.ServiceAPIClient, serviceID string) swarmtypes.Service {
198 251
 	t.Helper()
199 252
 	service, _, err := cli.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})