-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #280 from linkernetworks/hwchiu/VX-62
[Task] Support the new API to get the port status of ovs.
- Loading branch information
Showing
21 changed files
with
481 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package entity | ||
|
||
// PortStatsReceive contains information regarding the number of received | ||
// packets, bytes, etc. | ||
type PortStatsReceive struct { | ||
Packets uint64 `json:"packets"` | ||
Bytes uint64 `json:"bytes"` | ||
Dropped uint64 `json:"dropped"` | ||
Errors uint64 `json:"errors"` | ||
Frame uint64 `json:"-"` | ||
Over uint64 `json:"-"` | ||
CRC uint64 `json:"-"` | ||
} | ||
|
||
// PortStatsTransmit contains information regarding the number of transmitted | ||
// packets, bytes, etc. | ||
type PortStatsTransmit struct { | ||
Packets uint64 `json:"packets"` | ||
Bytes uint64 `json:"bytes"` | ||
Dropped uint64 `json:"dropped"` | ||
Errors uint64 `json:"errors"` | ||
Collisions uint64 `json:"collisions"` | ||
} | ||
type OVSPortStat struct { | ||
PortID uint32 `json:'portID"` | ||
Received PortStatsReceive `json:"received"` | ||
Transmitted PortStatsTransmit `json:"traansmitted"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ovscontroller | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/linkernetworks/vortex/src/entity" | ||
"github.com/linkernetworks/vortex/src/networkcontroller" | ||
"github.com/linkernetworks/vortex/src/serviceprovider" | ||
) | ||
|
||
func DumpPorts(sp *serviceprovider.Container, nodeName string, bridgeName string) ([]entity.OVSPortStat, error) { | ||
nodeIP, err := sp.KubeCtl.GetNodeInternalIP(nodeName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
nodeAddr := net.JoinHostPort(nodeIP, networkcontroller.DEFAULT_CONTROLLER_PORT) | ||
nc, err := networkcontroller.New(nodeAddr) | ||
|
||
return nc.DumpOVSPorts(bridgeName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package ovscontroller | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/linkernetworks/vortex/src/config" | ||
_ "github.com/linkernetworks/vortex/src/entity" | ||
kc "github.com/linkernetworks/vortex/src/kubernetes" | ||
"github.com/linkernetworks/vortex/src/serviceprovider" | ||
|
||
"github.com/moby/moby/pkg/namesgenerator" | ||
"github.com/stretchr/testify/suite" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
fakeclientset "k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
const OVS_LOCAL_IP = "127.0.0.1" | ||
|
||
func init() { | ||
rand.Seed(time.Now().UnixNano()) | ||
} | ||
|
||
func execute(suite *suite.Suite, cmd *exec.Cmd) { | ||
w := bytes.NewBuffer(nil) | ||
cmd.Stderr = w | ||
err := cmd.Run() | ||
suite.NoError(err) | ||
fmt.Printf("Stderr: %s\n", string(w.Bytes())) | ||
} | ||
|
||
type OVSControllerTestSuite struct { | ||
suite.Suite | ||
sp *serviceprovider.Container | ||
nodeName string | ||
bridgeName string | ||
} | ||
|
||
func (suite *OVSControllerTestSuite) SetupSuite() { | ||
cf := config.MustRead("../../config/testing.json") | ||
suite.sp = serviceprovider.NewForTesting(cf) | ||
|
||
// init fakeclient | ||
fakeclient := fakeclientset.NewSimpleClientset() | ||
suite.sp.KubeCtl = kc.New(fakeclient) | ||
|
||
suite.bridgeName = namesgenerator.GetRandomName(0)[0:6] | ||
|
||
// Create a fake clinet | ||
// Initial nodes | ||
suite.nodeName = namesgenerator.GetRandomName(0) | ||
_, err := suite.sp.KubeCtl.Clientset.CoreV1().Nodes().Create(&corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: suite.nodeName, | ||
}, | ||
Status: corev1.NodeStatus{ | ||
Addresses: []corev1.NodeAddress{ | ||
{ | ||
Type: "InternalIP", | ||
Address: OVS_LOCAL_IP, | ||
}, | ||
}, | ||
}, | ||
}) | ||
suite.NoError(err) | ||
|
||
execute(&suite.Suite, exec.Command("ovs-vsctl", "add-br", suite.bridgeName)) | ||
} | ||
|
||
func (suite *OVSControllerTestSuite) TearDownSuite() { | ||
defer exec.Command("ovs-vsctl", "del-br", suite.bridgeName).Run() | ||
} | ||
|
||
func TestOVSNetworkSuite(t *testing.T) { | ||
if runtime.GOOS != "linux" { | ||
fmt.Println("We only testing the ovs function on Linux Host") | ||
t.Skip() | ||
return | ||
} | ||
if _, defined := os.LookupEnv("TEST_GRPC"); !defined { | ||
t.SkipNow() | ||
return | ||
} | ||
suite.Run(t, new(OVSControllerTestSuite)) | ||
} | ||
|
||
// OK | ||
func (suite *OVSControllerTestSuite) TestDumpOVSPorts() { | ||
portStats, err := DumpPorts(suite.sp, suite.nodeName, suite.bridgeName) | ||
suite.NoError(err) | ||
suite.Equal(1, len(portStats)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
|
||
response "github.com/linkernetworks/vortex/src/net/http" | ||
"github.com/linkernetworks/vortex/src/net/http/query" | ||
"github.com/linkernetworks/vortex/src/ovscontroller" | ||
"github.com/linkernetworks/vortex/src/web" | ||
) | ||
|
||
func getOVSPortStatsHandler(ctx *web.Context) { | ||
sp, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response | ||
|
||
//Get the parameter | ||
query := query.New(req.Request.URL.Query()) | ||
nodeName, exist := query.Str("nodeName") | ||
if !exist { | ||
response.BadRequest(req.Request, resp.ResponseWriter, fmt.Errorf("The nodeName must not be empty")) | ||
return | ||
} | ||
|
||
bridgeName, exist := query.Str("bridgeName") | ||
if !exist { | ||
response.BadRequest(req.Request, resp.ResponseWriter, fmt.Errorf("The bridgeName must not be empty")) | ||
return | ||
} | ||
|
||
fmt.Println(nodeName, bridgeName) | ||
portStats, err := ovscontroller.DumpPorts(sp, nodeName, bridgeName) | ||
if err != nil { | ||
response.InternalServerError(req.Request, resp.ResponseWriter, err) | ||
return | ||
} | ||
resp.WriteEntity(portStats) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package server | ||
|
||
import ( | ||
_ "encoding/json" | ||
"math/rand" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
"time" | ||
|
||
restful "github.com/emicklei/go-restful" | ||
"github.com/linkernetworks/mongo" | ||
"github.com/linkernetworks/vortex/src/config" | ||
"github.com/linkernetworks/vortex/src/entity" | ||
"github.com/linkernetworks/vortex/src/serviceprovider" | ||
_ "github.com/moby/moby/pkg/namesgenerator" | ||
"github.com/stretchr/testify/suite" | ||
_ "gopkg.in/mgo.v2/bson" | ||
//corev1 "k8s.io/api/core/v1" | ||
|
||
"testing" | ||
) | ||
|
||
func init() { | ||
rand.Seed(time.Now().UnixNano()) | ||
} | ||
|
||
type OVSTestSuite struct { | ||
suite.Suite | ||
sp *serviceprovider.Container | ||
wc *restful.Container | ||
session *mongo.Session | ||
storage entity.Storage | ||
} | ||
|
||
func (suite *OVSTestSuite) SetupSuite() { | ||
cf := config.MustRead("../../config/testing.json") | ||
sp := serviceprovider.NewForTesting(cf) | ||
|
||
suite.sp = sp | ||
//init session | ||
suite.session = sp.Mongo.NewSession() | ||
//init restful container | ||
suite.wc = restful.NewContainer() | ||
service := newOVSService(suite.sp) | ||
suite.wc.Add(service) | ||
} | ||
|
||
func (suite *OVSTestSuite) TearDownSuite() { | ||
} | ||
|
||
func TestOVSSuite(t *testing.T) { | ||
suite.Run(t, new(OVSTestSuite)) | ||
} | ||
|
||
func (suite *OVSTestSuite) TestGetOVSPortStatsFail() { | ||
//Empty data | ||
httpRequest, err := http.NewRequest("GET", "http://localhost:7890/v1/ovs/portstat", nil) | ||
suite.NoError(err) | ||
|
||
httpWriter := httptest.NewRecorder() | ||
suite.wc.Dispatch(httpWriter, httpRequest) | ||
assertResponseCode(suite.T(), http.StatusBadRequest, httpWriter) | ||
|
||
httpRequest, err = http.NewRequest("GET", "http://localhost:7890/v1/ovs/portstat?nodeName=11", nil) | ||
httpWriter = httptest.NewRecorder() | ||
suite.wc.Dispatch(httpWriter, httpRequest) | ||
assertResponseCode(suite.T(), http.StatusBadRequest, httpWriter) | ||
|
||
httpRequest, err = http.NewRequest("GET", "http://localhost:7890/v1/ovs/portstat?nodeName=11&&bridgeName=111", nil) | ||
httpWriter = httptest.NewRecorder() | ||
suite.wc.Dispatch(httpWriter, httpRequest) | ||
assertResponseCode(suite.T(), http.StatusInternalServerError, httpWriter) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"username":"[email protected]", | ||
"password":"p@ssw0rd" | ||
} |
Oops, something went wrong.