Skip to content

Commit

Permalink
moved netdXXX APIs into common utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
g1rana committed Sep 15, 2017
1 parent 9740990 commit ac902c6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 87 deletions.
3 changes: 2 additions & 1 deletion mgmtfn/dockplugin/ipamDriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/contiv/netplugin/netmaster/master"
"github.com/contiv/netplugin/netplugin/cluster"
"github.com/contiv/netplugin/utils"
"github.com/docker/libnetwork/ipams/remote/api"
"github.com/docker/libnetwork/netlabel"
)
Expand Down Expand Up @@ -97,7 +98,7 @@ func requestPool(w http.ResponseWriter, r *http.Request) {
Pool = epgCfg.IPPool
} else {
// Get the pool from the Network
nwCfg, err := netdGetNetwork(epgCfg.NetworkName + "." + epgCfg.TenantName)
nwCfg, err := utils.GetNetwork(epgCfg.NetworkName + "." + epgCfg.TenantName)
if err != nil {
httpError(w, "failed to lookup network. ", err)
return
Expand Down
46 changes: 5 additions & 41 deletions mgmtfn/dockplugin/netDriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/contiv/contivmodel/client"
"github.com/contiv/netplugin/drivers"
"github.com/contiv/netplugin/netmaster/docknet"
"github.com/contiv/netplugin/netmaster/intent"
"github.com/contiv/netplugin/netmaster/master"
Expand Down Expand Up @@ -148,7 +147,7 @@ func deleteEndpoint(hostname string) func(http.ResponseWriter, *http.Request) {
}

netID := netName + "." + tenantName
_, err = netdGetEndpoint(netID + "-" + delreq.EndpointID)
_, err = utils.GetEndpoint(netID + "-" + delreq.EndpointID)
if err != nil {
httpError(w, "Could not find endpoint", err)
return
Expand Down Expand Up @@ -232,7 +231,7 @@ func createEndpoint(hostname string) func(http.ResponseWriter, *http.Request) {
return
}

ep, err := netdGetEndpoint(netID + "-" + cereq.EndpointID)
ep, err := utils.GetEndpoint(netID + "-" + cereq.EndpointID)
if err != nil {
httpError(w, "Could not find created endpoint", err)
return
Expand Down Expand Up @@ -310,13 +309,13 @@ func join(w http.ResponseWriter, r *http.Request) {
}

netID := netName + "." + tenantName
ep, err := netdGetEndpoint(netID + "-" + jr.EndpointID)
ep, err := utils.GetEndpoint(netID + "-" + jr.EndpointID)
if err != nil {
httpError(w, "Could not find created endpoint", err)
return
}

nw, err := netdGetNetwork(netID)
nw, err := utils.GetNetwork(netID)
if err != nil {
httpError(w, "Could not get network", err)
return
Expand Down Expand Up @@ -530,41 +529,6 @@ func discoverDelete(w http.ResponseWriter, r *http.Request) {
w.Write(content)
}

func netdGetEndpoint(epID string) (*drivers.OperEndpointState, error) {
// Get hold of the state driver
stateDriver, err := utils.GetStateDriver()
if err != nil {
return nil, err
}

operEp := &drivers.OperEndpointState{}
operEp.StateDriver = stateDriver
err = operEp.Read(epID)
if err != nil {
return nil, err
}

return operEp, nil
}

func netdGetNetwork(networkID string) (*mastercfg.CfgNetworkState, error) {
// Get hold of the state driver
stateDriver, err := utils.GetStateDriver()
if err != nil {
return nil, err
}

// find the network from network id
nwCfg := &mastercfg.CfgNetworkState{}
nwCfg.StateDriver = stateDriver
err = nwCfg.Read(networkID)
if err != nil {
return nil, err
}

return nwCfg, nil
}

// GetDockerNetworkName gets network name from network UUID
func GetDockerNetworkName(nwID string) (string, string, string, error) {
// first see if we can find the network in docknet oper state
Expand Down Expand Up @@ -781,7 +745,7 @@ func createNetworkHelper(networkID string, tag string, IPv4Data, IPv6Data []driv
// deleteNetworkHelper removes the association between docker network and contiv network
func deleteNetworkHelper(networkID string) error {
netID := networkID + ".default"
_, err := netdGetNetwork(netID)
_, err := utils.GetNetwork(netID)
if err == nil {
// if we find a contiv network with the ID hash, then it must be
// a docker created network (from the libnetwork create api).
Expand Down
45 changes: 3 additions & 42 deletions mgmtfn/k8splugin/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ import (
"time"

log "github.com/Sirupsen/logrus"
"github.com/contiv/netplugin/drivers"
"github.com/contiv/netplugin/mgmtfn/k8splugin/cniapi"
"github.com/contiv/netplugin/netmaster/intent"
"github.com/contiv/netplugin/netmaster/master"
"github.com/contiv/netplugin/netmaster/mastercfg"
"github.com/contiv/netplugin/netplugin/cluster"
"github.com/contiv/netplugin/utils"
"github.com/contiv/netplugin/utils/netutils"
Expand All @@ -55,43 +53,6 @@ type epAttr struct {
IPv6Gateway string
}

// netdGetEndpoint is a utility that reads the EP oper state
func netdGetEndpoint(epID string) (*drivers.OperEndpointState, error) {
// Get hold of the state driver
stateDriver, err := utils.GetStateDriver()
if err != nil {
return nil, err
}

operEp := &drivers.OperEndpointState{}
operEp.StateDriver = stateDriver
err = operEp.Read(epID)
if err != nil {
return nil, err
}

return operEp, nil
}

// netdGetNetwork is a utility that reads the n/w oper state
func netdGetNetwork(networkID string) (*mastercfg.CfgNetworkState, error) {
// Get hold of the state driver
stateDriver, err := utils.GetStateDriver()
if err != nil {
return nil, err
}

// find the network from network id
nwCfg := &mastercfg.CfgNetworkState{}
nwCfg.StateDriver = stateDriver
err = nwCfg.Read(networkID)
if err != nil {
return nil, err
}

return nwCfg, nil
}

// epCleanUp deletes the ep from netplugin and netmaster
func epCleanUp(req *epSpec) error {
// first delete from netplugin
Expand Down Expand Up @@ -128,7 +89,7 @@ func createEP(req *epSpec) (*epAttr, error) {

// if the ep already exists, treat as error for now.
netID := req.Network + "." + req.Tenant
ep, err := netdGetEndpoint(netID + "-" + req.EndpointID)
ep, err := utils.GetEndpoint(netID + "-" + req.EndpointID)
if err == nil {
return nil, fmt.Errorf("EP %s already exists", req.EndpointID)
}
Expand Down Expand Up @@ -165,15 +126,15 @@ func createEP(req *epSpec) (*epAttr, error) {
return nil, err
}

ep, err = netdGetEndpoint(netID + "-" + req.EndpointID)
ep, err = utils.GetEndpoint(netID + "-" + req.EndpointID)
if err != nil {
epCleanUp(req)
return nil, err
}

log.Debug(ep)
// need to get the subnetlen from nw state.
nw, err := netdGetNetwork(netID)
nw, err := utils.GetNetwork(netID)
if err != nil {
epCleanUp(req)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion test/systemtests/cfg.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"scheduler": "k8s", "install_mode": "kubeadm", "swarm_variable": "", "platform": "vagrant", "product": "netplugin", "aci_mode": "off", "short": false, "containers": 3, "iterations": 3, "enableDNS": false, "contiv_l3": "", "contiv_cluster_store": "etcd://netmaster:6666", "keyFile": "/home/admin/.ssh/id_rsa", "binpath": "contiv/bin", "hostips": "192.168.2.10,192.168.2.11,192.168.2.12", "hostusernames": "admin,admin,admin", "dataInterfaces": "eth2", "mgmtInterface": "eth1", "vlan": "1120-1150", "vxlan": "1-10000", "subnet": "10.1.1.0/24", "gateway": "10.1.1.254", "network": "TestNet", "tenant": "TestTenant", "encap": "vlan"}
{"install_mode": "legacy", "binpath": "/opt/gopath/bin", "iterations": 3, "encap": "vlan", "keyFile": "/home/admin/.ssh/id_rsa", "gateway": "10.1.1.254", "subnet": "10.1.1.0/24", "network": "TestNet", "contiv_cluster_store": "etcd://localhost:2379", "platform": "vagrant", "mgmtInterface": "eth1", "dataInterfaces": "eth2,eth3", "vxlan": "1-10000", "product": "netplugin", "swarm_variable": "", "vlan": "1120-1150", "aci_mode": "off", "scheduler": "docker", "enableDNS": false, "tenant": "TestTenant", "short": false, "hostips": "192.168.2.10,192.168.2.11,192.168.2.12", "hostusernames": "admin,admin,admin", "containers": 3, "contiv_l3": ""}
3 changes: 1 addition & 2 deletions utils/netutils/netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ import (
osexec "os/exec"

log "github.com/Sirupsen/logrus"
"github.com/contiv/netplugin/core"
"github.com/jainvipin/bitset"
netlink "github.com/vishvananda/netlink"

"github.com/contiv/netplugin/core"
)

var endianNess string
Expand Down
43 changes: 43 additions & 0 deletions utils/stateutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package utils

import (
"github.com/contiv/netplugin/drivers"
"github.com/contiv/netplugin/netmaster/mastercfg"
)

// GetEndpoint is a utility that reads the EP oper state
func GetEndpoint(epID string) (*drivers.OperEndpointState, error) {
// Get hold of the state driver
stateDriver, err := GetStateDriver()
if err != nil {
return nil, err
}

operEp := &drivers.OperEndpointState{}
operEp.StateDriver = stateDriver
err = operEp.Read(epID)
if err != nil {
return nil, err
}

return operEp, nil
}

// GetNetwork is a utility that reads the n/w oper state
func GetNetwork(networkID string) (*mastercfg.CfgNetworkState, error) {
// Get hold of the state driver
stateDriver, err := GetStateDriver()
if err != nil {
return nil, err
}

// find the network from network id
nwCfg := &mastercfg.CfgNetworkState{}
nwCfg.StateDriver = stateDriver
err = nwCfg.Read(networkID)
if err != nil {
return nil, err
}

return nwCfg, nil
}

0 comments on commit ac902c6

Please sign in to comment.