Browse code

Remove daemon.VXSubnets duplicate code

Refactor daemon.V4Subnets and daemon.V6Subnets to limit duplication

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2017/02/28 18:51:40
Showing 3 changed files
... ...
@@ -89,8 +89,7 @@ var errSwarmCertificatesExpired = errors.New("Swarm certificates have expired. T
89 89
 // NetworkSubnetsProvider exposes functions for retrieving the subnets
90 90
 // of networks managed by Docker, so they can be filtered.
91 91
 type NetworkSubnetsProvider interface {
92
-	V4Subnets() []net.IPNet
93
-	V6Subnets() []net.IPNet
92
+	Subnets() ([]net.IPNet, []net.IPNet)
94 93
 }
95 94
 
96 95
 // Config provides values for Cluster.
... ...
@@ -162,8 +162,7 @@ func (c *Cluster) resolveSystemAddrViaSubnetCheck() (net.IP, error) {
162 162
 	var systemInterface string
163 163
 
164 164
 	// List Docker-managed subnets
165
-	v4Subnets := c.config.NetworkSubnetsProvider.V4Subnets()
166
-	v6Subnets := c.config.NetworkSubnetsProvider.V6Subnets()
165
+	v4Subnets, v6Subnets := c.config.NetworkSubnetsProvider.Subnets()
167 166
 
168 167
 ifaceLoop:
169 168
 	for _, intf := range interfaces {
... ...
@@ -878,40 +878,28 @@ func (daemon *Daemon) Unmount(container *container.Container) error {
878 878
 	return nil
879 879
 }
880 880
 
881
-// V4Subnets returns the IPv4 subnets of networks that are managed by Docker.
882
-func (daemon *Daemon) V4Subnets() []net.IPNet {
883
-	var subnets []net.IPNet
881
+// Subnets return the IPv4 and IPv6 subnets of networks that are manager by Docker.
882
+func (daemon *Daemon) Subnets() ([]net.IPNet, []net.IPNet) {
883
+	var v4Subnets []net.IPNet
884
+	var v6Subnets []net.IPNet
884 885
 
885 886
 	managedNetworks := daemon.netController.Networks()
886 887
 
887 888
 	for _, managedNetwork := range managedNetworks {
888
-		v4Infos, _ := managedNetwork.Info().IpamInfo()
889
-		for _, v4Info := range v4Infos {
890
-			if v4Info.IPAMData.Pool != nil {
891
-				subnets = append(subnets, *v4Info.IPAMData.Pool)
889
+		v4infos, v6infos := managedNetwork.Info().IpamInfo()
890
+		for _, info := range v4infos {
891
+			if info.IPAMData.Pool != nil {
892
+				v4Subnets = append(v4Subnets, *info.IPAMData.Pool)
892 893
 			}
893 894
 		}
894
-	}
895
-
896
-	return subnets
897
-}
898
-
899
-// V6Subnets returns the IPv6 subnets of networks that are managed by Docker.
900
-func (daemon *Daemon) V6Subnets() []net.IPNet {
901
-	var subnets []net.IPNet
902
-
903
-	managedNetworks := daemon.netController.Networks()
904
-
905
-	for _, managedNetwork := range managedNetworks {
906
-		_, v6Infos := managedNetwork.Info().IpamInfo()
907
-		for _, v6Info := range v6Infos {
908
-			if v6Info.IPAMData.Pool != nil {
909
-				subnets = append(subnets, *v6Info.IPAMData.Pool)
895
+		for _, info := range v6infos {
896
+			if info.IPAMData.Pool != nil {
897
+				v6Subnets = append(v6Subnets, *info.IPAMData.Pool)
910 898
 			}
911 899
 		}
912 900
 	}
913 901
 
914
-	return subnets
902
+	return v4Subnets, v6Subnets
915 903
 }
916 904
 
917 905
 // GraphDriverName returns the name of the graph driver used by the layer.Store