Browse code

Merge pull request #17274 from aboch/ai

Turn off discovery when icc == false

David Calavera authored on 2015/10/23 05:08:31
Showing 5 changed files
... ...
@@ -783,7 +783,7 @@ func (container *Container) updateNetwork() error {
783 783
 	return nil
784 784
 }
785 785
 
786
-func (container *Container) buildCreateEndpointOptions() ([]libnetwork.EndpointOption, error) {
786
+func (container *Container) buildCreateEndpointOptions(n libnetwork.Network) ([]libnetwork.EndpointOption, error) {
787 787
 	var (
788 788
 		portSpecs     = make(nat.PortSet)
789 789
 		bindings      = make(nat.PortMap)
... ...
@@ -861,6 +861,10 @@ func (container *Container) buildCreateEndpointOptions() ([]libnetwork.EndpointO
861 861
 		createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOption))
862 862
 	}
863 863
 
864
+	if n.Name() == "bridge" && !container.daemon.config().Bridge.InterContainerCommunication {
865
+		createOptions = append(createOptions, libnetwork.CreateOptionAnonymous())
866
+	}
867
+
864 868
 	return createOptions, nil
865 869
 }
866 870
 
... ...
@@ -950,7 +954,7 @@ func (container *Container) connectToNetwork(idOrName string, updateSettings boo
950 950
 		return err
951 951
 	}
952 952
 
953
-	createOptions, err := container.buildCreateEndpointOptions()
953
+	createOptions, err := container.buildCreateEndpointOptions(n)
954 954
 	if err != nil {
955 955
 		return err
956 956
 	}
... ...
@@ -21,7 +21,7 @@ clone git github.com/vdemeester/shakers 3c10293ce22b900c27acad7b28656196fcc2f73b
21 21
 clone git golang.org/x/net 3cffabab72adf04f8e3b01c5baf775361837b5fe https://github.com/golang/net.git
22 22
 
23 23
 #get libnetwork packages
24
-clone git github.com/docker/libnetwork 0d7a57ddb94a92a57755eec5dc54f905287c7e65
24
+clone git github.com/docker/libnetwork f3c8ebf46b890d4612c5d98e792280d13abdb761
25 25
 clone git github.com/armon/go-metrics eb0af217e5e9747e41dd5303755356b62d28e3ec
26 26
 clone git github.com/hashicorp/go-msgpack 71c2886f5a673a35f909803f38ece5810165097b
27 27
 clone git github.com/hashicorp/memberlist 9a1e242e454d2443df330bdd51a436d5a9058fc4
... ...
@@ -408,3 +408,80 @@ func (s *DockerNetworkSuite) TestDockerNetworkDriverOptions(c *check.C) {
408 408
 	dockerCmd(c, "network", "rm", "testopt")
409 409
 
410 410
 }
411
+
412
+func (s *DockerDaemonSuite) TestDockerNetworkDiscoveryICCFalse(c *check.C) {
413
+	// When icc == false, containers' etc/hosts should not be populated with containers' names
414
+	hostsFile := "/etc/hosts"
415
+	bridgeName := "external-bridge"
416
+	bridgeIP := "192.169.255.254/24"
417
+	out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
418
+	c.Assert(err, check.IsNil, check.Commentf(out))
419
+	defer deleteInterface(c, bridgeName)
420
+
421
+	err = s.d.StartWithBusybox("--bridge", bridgeName, "--icc=false")
422
+	c.Assert(err, check.IsNil)
423
+	defer s.d.Restart()
424
+
425
+	// run two containers and store first container's etc/hosts content
426
+	out, err = s.d.Cmd("run", "-d", "busybox", "top")
427
+	c.Assert(err, check.IsNil)
428
+	cid1 := strings.TrimSpace(out)
429
+	defer s.d.Cmd("stop", cid1)
430
+
431
+	hosts, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
432
+	c.Assert(err, checker.IsNil)
433
+
434
+	out, err = s.d.Cmd("run", "-d", "busybox", "top")
435
+	c.Assert(err, check.IsNil)
436
+	cid2 := strings.TrimSpace(out)
437
+
438
+	// verify first container's etc/hosts file has not changed after spawning second container
439
+	hostsPost, err := s.d.Cmd("exec", cid1, "cat", hostsFile)
440
+	c.Assert(err, checker.IsNil)
441
+	c.Assert(string(hosts), checker.Equals, string(hostsPost),
442
+		check.Commentf("Unexpected %s change on second container creation", hostsFile))
443
+
444
+	// stop container 2 and verify first container's etc/hosts has not changed
445
+	_, err = s.d.Cmd("stop", cid2)
446
+	c.Assert(err, check.IsNil)
447
+
448
+	hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
449
+	c.Assert(err, checker.IsNil)
450
+	c.Assert(string(hosts), checker.Equals, string(hostsPost),
451
+		check.Commentf("Unexpected %s change on second container creation", hostsFile))
452
+
453
+	// but discovery is on when connecting to non default bridge network
454
+	network := "anotherbridge"
455
+	out, err = s.d.Cmd("network", "create", network)
456
+	c.Assert(err, check.IsNil, check.Commentf(out))
457
+	defer s.d.Cmd("network", "rm", network)
458
+
459
+	out, err = s.d.Cmd("network", "connect", network, cid1)
460
+	c.Assert(err, check.IsNil, check.Commentf(out))
461
+
462
+	hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
463
+	c.Assert(err, checker.IsNil)
464
+	c.Assert(string(hosts), checker.Equals, string(hostsPost),
465
+		check.Commentf("Unexpected %s change on second network connection", hostsFile))
466
+
467
+	cName := "container3"
468
+	out, err = s.d.Cmd("run", "-d", "--net", network, "--name", cName, "busybox", "top")
469
+	c.Assert(err, check.IsNil, check.Commentf(out))
470
+	cid3 := strings.TrimSpace(out)
471
+	defer s.d.Cmd("stop", cid3)
472
+
473
+	// container1 etc/hosts file should contain an entry for the third container
474
+	hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
475
+	c.Assert(err, checker.IsNil)
476
+	c.Assert(string(hostsPost), checker.Contains, cName,
477
+		check.Commentf("Container 1  %s file does not contain entries for named container %q: %s", hostsFile, cName, string(hostsPost)))
478
+
479
+	// on container3 disconnect, first container's etc/hosts should go back to original form
480
+	out, err = s.d.Cmd("network", "disconnect", network, cid3)
481
+	c.Assert(err, check.IsNil, check.Commentf(out))
482
+
483
+	hostsPost, err = s.d.Cmd("exec", cid1, "cat", hostsFile)
484
+	c.Assert(err, checker.IsNil)
485
+	c.Assert(string(hosts), checker.Equals, string(hostsPost),
486
+		check.Commentf("Unexpected %s content after disconnecting from second network", hostsFile))
487
+}
... ...
@@ -57,6 +57,7 @@ type endpoint struct {
57 57
 	joinInfo      *endpointJoinInfo
58 58
 	sandboxID     string
59 59
 	exposedPorts  []types.TransportPort
60
+	anonymous     bool
60 61
 	generic       map[string]interface{}
61 62
 	joinLeaveDone chan struct{}
62 63
 	dbIndex       uint64
... ...
@@ -77,6 +78,7 @@ func (ep *endpoint) MarshalJSON() ([]byte, error) {
77 77
 		epMap["generic"] = ep.generic
78 78
 	}
79 79
 	epMap["sandbox"] = ep.sandboxID
80
+	epMap["anonymous"] = ep.anonymous
80 81
 	return json.Marshal(epMap)
81 82
 }
82 83
 
... ...
@@ -105,6 +107,10 @@ func (ep *endpoint) UnmarshalJSON(b []byte) (err error) {
105 105
 	if v, ok := epMap["generic"]; ok {
106 106
 		ep.generic = v.(map[string]interface{})
107 107
 	}
108
+
109
+	if v, ok := epMap["anonymous"]; ok {
110
+		ep.anonymous = v.(bool)
111
+	}
108 112
 	return nil
109 113
 }
110 114
 
... ...
@@ -122,6 +128,7 @@ func (ep *endpoint) CopyTo(o datastore.KVObject) error {
122 122
 	dstEp.sandboxID = ep.sandboxID
123 123
 	dstEp.dbIndex = ep.dbIndex
124 124
 	dstEp.dbExists = ep.dbExists
125
+	dstEp.anonymous = ep.anonymous
125 126
 
126 127
 	if ep.iface != nil {
127 128
 		dstEp.iface = &endpointInterface{}
... ...
@@ -161,6 +168,12 @@ func (ep *endpoint) Network() string {
161 161
 	return ep.network.name
162 162
 }
163 163
 
164
+func (ep *endpoint) isAnonymous() bool {
165
+	ep.Lock()
166
+	defer ep.Unlock()
167
+	return ep.anonymous
168
+}
169
+
164 170
 // endpoint Key structure : endpoint/network-id/endpoint-id
165 171
 func (ep *endpoint) Key() []string {
166 172
 	if ep.network == nil {
... ...
@@ -603,6 +616,14 @@ func CreateOptionPortMapping(portBindings []types.PortBinding) EndpointOption {
603 603
 	}
604 604
 }
605 605
 
606
+// CreateOptionAnonymous function returns an option setter for setting
607
+// this endpoint as anonymous
608
+func CreateOptionAnonymous() EndpointOption {
609
+	return func(ep *endpoint) {
610
+		ep.anonymous = true
611
+	}
612
+}
613
+
606 614
 // JoinOptionPriority function returns an option setter for priority option to
607 615
 // be passed to the endpoint.Join() method.
608 616
 func JoinOptionPriority(ep Endpoint, prio int) EndpointOption {
... ...
@@ -753,6 +753,10 @@ func (n *network) EndpointByID(id string) (Endpoint, error) {
753 753
 }
754 754
 
755 755
 func (n *network) updateSvcRecord(ep *endpoint, localEps []*endpoint, isAdd bool) {
756
+	if ep.isAnonymous() {
757
+		return
758
+	}
759
+
756 760
 	c := n.getController()
757 761
 	sr, ok := c.svcDb[n.ID()]
758 762
 	if !ok {