diff --git a/libnetwork/drivers/overlay/ov_endpoint.go b/libnetwork/drivers/overlay/ov_endpoint.go index 9fc94085af..24bfc9e8f4 100644 --- a/libnetwork/drivers/overlay/ov_endpoint.go +++ b/libnetwork/drivers/overlay/ov_endpoint.go @@ -4,30 +4,23 @@ package overlay import ( - "encoding/json" "fmt" "net" - "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/netutils" "github.com/docker/docker/libnetwork/ns" - "github.com/docker/docker/libnetwork/types" "github.com/sirupsen/logrus" ) type endpointTable map[string]*endpoint -const overlayEndpointPrefix = "overlay/endpoint" - type endpoint struct { - id string - nid string - ifName string - mac net.HardwareAddr - addr *net.IPNet - dbExists bool - dbIndex uint64 + id string + nid string + ifName string + mac net.HardwareAddr + addr *net.IPNet } func (n *network) endpoint(eid string) *endpoint { @@ -133,99 +126,3 @@ func (d *driver) DeleteEndpoint(nid, eid string) error { func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) { return make(map[string]interface{}), nil } - -func (ep *endpoint) DataScope() string { - return datastore.LocalScope -} - -func (ep *endpoint) New() datastore.KVObject { - return &endpoint{} -} - -func (ep *endpoint) CopyTo(o datastore.KVObject) error { - dstep := o.(*endpoint) - *dstep = *ep - return nil -} - -func (ep *endpoint) Key() []string { - return []string{overlayEndpointPrefix, ep.id} -} - -func (ep *endpoint) KeyPrefix() []string { - return []string{overlayEndpointPrefix} -} - -func (ep *endpoint) Index() uint64 { - return ep.dbIndex -} - -func (ep *endpoint) SetIndex(index uint64) { - ep.dbIndex = index - ep.dbExists = true -} - -func (ep *endpoint) Exists() bool { - return ep.dbExists -} - -func (ep *endpoint) Skip() bool { - return false -} - -func (ep *endpoint) Value() []byte { - b, err := json.Marshal(ep) - if err != nil { - return nil - } - return b -} - -func (ep *endpoint) SetValue(value []byte) error { - return json.Unmarshal(value, ep) -} - -func (ep *endpoint) MarshalJSON() ([]byte, error) { - epMap := make(map[string]interface{}) - - epMap["id"] = ep.id - epMap["nid"] = ep.nid - if ep.ifName != "" { - epMap["ifName"] = ep.ifName - } - if ep.addr != nil { - epMap["addr"] = ep.addr.String() - } - if len(ep.mac) != 0 { - epMap["mac"] = ep.mac.String() - } - - return json.Marshal(epMap) -} - -func (ep *endpoint) UnmarshalJSON(value []byte) error { - var ( - err error - epMap map[string]interface{} - ) - - json.Unmarshal(value, &epMap) - - ep.id = epMap["id"].(string) - ep.nid = epMap["nid"].(string) - if v, ok := epMap["mac"]; ok { - if ep.mac, err = net.ParseMAC(v.(string)); err != nil { - return types.InternalErrorf("failed to decode endpoint interface mac address after json unmarshal: %s", v.(string)) - } - } - if v, ok := epMap["addr"]; ok { - if ep.addr, err = types.ParseCIDR(v.(string)); err != nil { - return types.InternalErrorf("failed to decode endpoint interface ipv4 address after json unmarshal: %v", err) - } - } - if v, ok := epMap["ifName"]; ok { - ep.ifName = v.(string) - } - - return nil -} diff --git a/libnetwork/drivers/overlay/ov_network.go b/libnetwork/drivers/overlay/ov_network.go index 6afe2e9623..f7f8400a80 100644 --- a/libnetwork/drivers/overlay/ov_network.go +++ b/libnetwork/drivers/overlay/ov_network.go @@ -4,7 +4,6 @@ package overlay import ( - "encoding/json" "errors" "fmt" "net" @@ -15,7 +14,6 @@ import ( "strings" "sync" - "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/netlabel" "github.com/docker/docker/libnetwork/netutils" @@ -49,16 +47,8 @@ type subnet struct { gwIP *net.IPNet } -type subnetJSON struct { - SubnetIP string - GwIP string - Vni uint32 -} - type network struct { id string - dbIndex uint64 - dbExists bool sbox osl.Sandbox endpoints endpointTable driver *driver @@ -703,121 +693,6 @@ func (n *network) sandbox() osl.Sandbox { return n.sbox } -func (n *network) Key() []string { - return []string{"overlay", "network", n.id} -} - -func (n *network) KeyPrefix() []string { - return []string{"overlay", "network"} -} - -func (n *network) Value() []byte { - m := map[string]interface{}{} - - netJSON := []*subnetJSON{} - - for _, s := range n.subnets { - sj := &subnetJSON{ - SubnetIP: s.subnetIP.String(), - GwIP: s.gwIP.String(), - Vni: s.vni, - } - netJSON = append(netJSON, sj) - } - - m["secure"] = n.secure - m["subnets"] = netJSON - m["mtu"] = n.mtu - b, err := json.Marshal(m) - if err != nil { - return []byte{} - } - - return b -} - -func (n *network) Index() uint64 { - return n.dbIndex -} - -func (n *network) SetIndex(index uint64) { - n.dbIndex = index - n.dbExists = true -} - -func (n *network) Exists() bool { - return n.dbExists -} - -func (n *network) Skip() bool { - return false -} - -func (n *network) SetValue(value []byte) error { - var ( - m map[string]interface{} - newNet bool - isMap = true - netJSON = []*subnetJSON{} - ) - - if err := json.Unmarshal(value, &m); err != nil { - err := json.Unmarshal(value, &netJSON) - if err != nil { - return err - } - isMap = false - } - - if len(n.subnets) == 0 { - newNet = true - } - - if isMap { - if val, ok := m["secure"]; ok { - n.secure = val.(bool) - } - if val, ok := m["mtu"]; ok { - n.mtu = int(val.(float64)) - } - bytes, err := json.Marshal(m["subnets"]) - if err != nil { - return err - } - if err := json.Unmarshal(bytes, &netJSON); err != nil { - return err - } - } - - for _, sj := range netJSON { - subnetIPstr := sj.SubnetIP - gwIPstr := sj.GwIP - vni := sj.Vni - - subnetIP, _ := types.ParseCIDR(subnetIPstr) - gwIP, _ := types.ParseCIDR(gwIPstr) - - if newNet { - s := &subnet{ - subnetIP: subnetIP, - gwIP: gwIP, - vni: vni, - } - n.subnets = append(n.subnets, s) - } else { - sNet := n.getMatchingSubnet(subnetIP) - if sNet != nil { - sNet.vni = vni - } - } - } - return nil -} - -func (n *network) DataScope() string { - return datastore.GlobalScope -} - // getSubnetforIP returns the subnet to which the given IP belongs func (n *network) getSubnetforIP(ip *net.IPNet) *subnet { for _, s := range n.subnets { @@ -833,22 +708,3 @@ func (n *network) getSubnetforIP(ip *net.IPNet) *subnet { } return nil } - -// getMatchingSubnet return the network's subnet that matches the input -func (n *network) getMatchingSubnet(ip *net.IPNet) *subnet { - if ip == nil { - return nil - } - for _, s := range n.subnets { - // first check if the mask lengths are the same - i, _ := s.subnetIP.Mask.Size() - j, _ := ip.Mask.Size() - if i != j { - continue - } - if s.subnetIP.IP.Equal(ip.IP) { - return s - } - } - return nil -}