Ver código fonte

marshal/unmarshal for overlay multiple subnets

Signed-off-by: Santhosh Manohar <santhosh@docker.com>
Santhosh Manohar 9 anos atrás
pai
commit
19f466369b

+ 11 - 0
libnetwork/drivers/overlay/joinleave.go

@@ -4,7 +4,9 @@ import (
 	"fmt"
 	"net"
 
+	log "github.com/Sirupsen/logrus"
 	"github.com/docker/libnetwork/driverapi"
+	"github.com/docker/libnetwork/types"
 	"github.com/vishvananda/netlink"
 )
 
@@ -78,6 +80,15 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
 		return fmt.Errorf("could not set mac address (%v) to the container interface: %v", ep.mac, err)
 	}
 
+	for _, sub := range n.subnets {
+		if sub == s {
+			continue
+		}
+		if err := jinfo.AddStaticRoute(sub.subnetIP, types.NEXTHOP, s.gwIP.IP); err != nil {
+			log.Errorf("Adding subnet %s static route in network %q failed\n", s.subnetIP, n.id)
+		}
+	}
+
 	if iNames := jinfo.InterfaceName(); iNames != nil {
 		err = iNames.SetNames(name2, "eth")
 		if err != nil {

+ 41 - 31
libnetwork/drivers/overlay/ov_network.go

@@ -29,6 +29,12 @@ type subnet struct {
 	gwIP      *net.IPNet
 }
 
+type subnetJSON struct {
+	SubnetIP string
+	GwIP     string
+	Vni      uint32
+}
+
 type network struct {
 	id        string
 	dbIndex   uint64
@@ -366,23 +372,22 @@ func (n *network) KeyPrefix() []string {
 }
 
 func (n *network) Value() []byte {
-	overlayNetmap := make(map[string]interface{})
+	netJSON := []*subnetJSON{}
 
-	s := n.subnets[0]
-	if s == nil {
-		logrus.Errorf("Network %s has no subnets", n.id)
-		return []byte{}
+	for _, s := range n.subnets {
+		sj := &subnetJSON{
+			SubnetIP: s.subnetIP.String(),
+			GwIP:     s.gwIP.String(),
+			Vni:      s.vni,
+		}
+		netJSON = append(netJSON, sj)
 	}
 
-	overlayNetmap["subnetIP"] = s.subnetIP.String()
-	overlayNetmap["gwIP"] = s.gwIP.String()
-	overlayNetmap["vni"] = s.vni
+	b, err := json.Marshal(netJSON)
 
-	b, err := json.Marshal(overlayNetmap)
 	if err != nil {
 		return []byte{}
 	}
-
 	return b
 }
 
@@ -404,36 +409,41 @@ func (n *network) Skip() bool {
 }
 
 func (n *network) SetValue(value []byte) error {
-	var (
-		overlayNetmap map[string]interface{}
-		err           error
-	)
+	var newNet bool
+	netJSON := []*subnetJSON{}
 
-	err = json.Unmarshal(value, &overlayNetmap)
+	err := json.Unmarshal(value, &netJSON)
 	if err != nil {
 		return err
 	}
 
-	subnetIPstr := overlayNetmap["subnetIP"].(string)
-	gwIPstr := overlayNetmap["gwIP"].(string)
-	vni := uint32(overlayNetmap["vni"].(float64))
+	if len(n.subnets) == 0 {
+		newNet = true
+	}
 
-	subnetIP, _ := types.ParseCIDR(subnetIPstr)
-	gwIP, _ := types.ParseCIDR(gwIPstr)
+	for _, sj := range netJSON {
+		subnetIPstr := sj.SubnetIP
+		gwIPstr := sj.GwIP
+		vni := sj.Vni
 
-	s := &subnet{
-		subnetIP: subnetIP,
-		gwIP:     gwIP,
-		vni:      vni,
-		once:     &sync.Once{},
-	}
-	n.subnets = append(n.subnets, s)
+		subnetIP, _ := types.ParseCIDR(subnetIPstr)
+		gwIP, _ := types.ParseCIDR(gwIPstr)
 
-	sNet := n.getMatchingSubnet(subnetIP)
-	if sNet != nil {
-		sNet.vni = vni
+		if newNet {
+			s := &subnet{
+				subnetIP: subnetIP,
+				gwIP:     gwIP,
+				vni:      vni,
+				once:     &sync.Once{},
+			}
+			n.subnets = append(n.subnets, s)
+		} else {
+			sNet := n.getMatchingSubnet(subnetIP)
+			if sNet != nil {
+				sNet.vni = vni
+			}
+		}
 	}
-
 	return nil
 }