Browse code

Fix api server null pointer def on inspect/ls null ipam-driver networks

- When a network is created with the null ipam driver, docker api server
thread will deference a nil pointer on `docker network ls` and on
`docker network inspect <nw>`. This because buildIpamResource()
assumes a gateway address is always present, which is not correct.

Signed-off-by: Alessandro Boch <aboch@tetrationanalytics.com>

Alessandro Boch authored on 2017/08/03 15:00:12
Showing 2 changed files
... ...
@@ -409,7 +409,9 @@ func buildIpamResources(r *types.NetworkResource, nwInfo libnetwork.NetworkInfo)
409 409
 		for _, ip4Info := range ipv4Info {
410 410
 			iData := network.IPAMConfig{}
411 411
 			iData.Subnet = ip4Info.IPAMData.Pool.String()
412
-			iData.Gateway = ip4Info.IPAMData.Gateway.IP.String()
412
+			if ip4Info.IPAMData.Gateway != nil {
413
+				iData.Gateway = ip4Info.IPAMData.Gateway.IP.String()
414
+			}
413 415
 			r.IPAM.Config = append(r.IPAM.Config, iData)
414 416
 		}
415 417
 	}
... ...
@@ -690,6 +690,21 @@ func (s *DockerNetworkSuite) TestDockerNetworkIPAMOptions(c *check.C) {
690 690
 	c.Assert(opts["opt2"], checker.Equals, "drv2")
691 691
 }
692 692
 
693
+func (s *DockerNetworkSuite) TestDockerNetworkNullIPAMDriver(c *check.C) {
694
+	// Create a network with null ipam driver
695
+	_, _, err := dockerCmdWithError("network", "create", "-d", dummyNetworkDriver, "--ipam-driver", "null", "test000")
696
+	c.Assert(err, check.IsNil)
697
+	assertNwIsAvailable(c, "test000")
698
+
699
+	// Verify the inspect data contains the default subnet provided by the null
700
+	// ipam driver and no gateway, as the null ipam driver does not provide one
701
+	nr := getNetworkResource(c, "test000")
702
+	c.Assert(nr.IPAM.Driver, checker.Equals, "null")
703
+	c.Assert(len(nr.IPAM.Config), checker.Equals, 1)
704
+	c.Assert(nr.IPAM.Config[0].Subnet, checker.Equals, "0.0.0.0/0")
705
+	c.Assert(nr.IPAM.Config[0].Gateway, checker.Equals, "")
706
+}
707
+
693 708
 func (s *DockerNetworkSuite) TestDockerNetworkInspectDefault(c *check.C) {
694 709
 	nr := getNetworkResource(c, "none")
695 710
 	c.Assert(nr.Driver, checker.Equals, "null")