Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #541 #578

Merged
merged 9 commits into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions netctl/netctl.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func inspectPolicy(ctx *cli.Context) {
tenant := ctx.String("tenant")
policy := ctx.Args()[0]

fmt.Printf("Inspeting policy: %s tenant: %s\n", policy, tenant)
fmt.Printf("Inspecting policy: %s tenant: %s\n", policy, tenant)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its time to make this a debug log. .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that but not sure if someone expects to scrape the CLI output instead of using the API. If yes, removing some lines from CLI output could impact their screen scraping logic … but maybe there aren’t any customers that do that OR we anyway do not support anyone to do CLI screen scraping since they should only be using API for automation. Also, this is not the only place … fmt.Printf() is used throughout this file in places where a debug log may have been appropriate. Should there be a separate issue opened to track which all Printf()s should be changed to logs ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible you can remove the printf's


pol, err := getClient(ctx).PolicyInspect(tenant, policy)
errCheck(ctx, err)
Expand Down Expand Up @@ -471,7 +471,7 @@ func inspectNetwork(ctx *cli.Context) {
tenant := ctx.String("tenant")
network := ctx.Args()[0]

fmt.Printf("Inspeting network: %s tenant: %s\n", network, tenant)
fmt.Printf("Inspecting network: %s tenant: %s\n", network, tenant)

net, err := getClient(ctx).NetworkInspect(tenant, network)
errCheck(ctx, err)
Expand Down Expand Up @@ -661,7 +661,7 @@ func inspectEndpointGroup(ctx *cli.Context) {
tenant := ctx.String("tenant")
endpointGroup := ctx.Args()[0]

fmt.Printf("Inspeting endpointGroup: %s tenant: %s\n", endpointGroup, tenant)
fmt.Printf("Inspecting endpointGroup: %s tenant: %s\n", endpointGroup, tenant)

epg, err := getClient(ctx).EndpointGroupInspect(tenant, endpointGroup)
errCheck(ctx, err)
Expand Down
2 changes: 1 addition & 1 deletion netmaster/master/api.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func UpdateEndpointHandler(w http.ResponseWriter, r *http.Request, vars map[stri
return nil, err
}

if epUpdReq.Event == "start" {
if epUpdReq.Event == "start" || epUpdReq.Event == "connect" {
//Received container start event from netplugin. Check if the Provider
//matches any service and perform service provider update if there is a matching
//service.
Expand Down
123 changes: 81 additions & 42 deletions netplugin/agent/docker_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,64 +28,80 @@ import (
"golang.org/x/net/context"
)

// Handles docker events monitored by dockerclient. Currently we only handle
// container start and die event*/
// Handles docker events monitored by dockerclient. Currently we only handle // container start and die event*/
func handleDockerEvents(event *dockerclient.Event, ec chan error, args ...interface{}) {

log.Debugf("Received Docker event: {%#v}\n", *event)
endpointUpdReq := &master.UpdateEndpointRequest{}
switch event.Status {
case "start":

var containerID string
var networkID string

if (event.Type == "container" && event.Action == "start") ||
(event.Type == "network" && event.Action == "connect") {

if event.Type == "container" {
containerID = event.ID
networkID = ""
} else {
containerID = event.Actor.Attributes["container"]
networkID = event.Actor.ID
}

if containerID == "" {
log.Errorf("Container ID missing in docker event {%#v}\n", *event)
return
}

defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.21", nil, defaultHeaders)
if err != nil {
panic(err)
log.Errorf("Client lookup failed :%s", err)
return
}

containerInfo, err := cli.ContainerInspect(context.Background(), event.ID)
containerInfo, err := cli.ContainerInspect(context.Background(), containerID)

if err != nil {
log.Errorf("Container Inspect failed :%s", err)
return
}

if event.ID != "" {
labelMap := getLabelsFromContainerInspect(&containerInfo)
containerTenant := getTenantFromContainerInspect(&containerInfo)
networkName, ipAddress, err := getEpNetworkInfoFromContainerInspect(&containerInfo)
if err != nil {
log.Errorf("Error getting container network info for %v.Err:%s", event.ID, err)
}
endpoint := getEndpointFromContainerInspect(&containerInfo)

if ipAddress != "" {
//Create provider info
endpointUpdReq.IPAddress = ipAddress
endpointUpdReq.ContainerID = event.ID
endpointUpdReq.Tenant = containerTenant
endpointUpdReq.Network = networkName
endpointUpdReq.Event = "start"
endpointUpdReq.EndpointID = endpoint
endpointUpdReq.ContainerName = containerInfo.Name
endpointUpdReq.Labels = make(map[string]string)

for k, v := range labelMap {
endpointUpdReq.Labels[k] = v
}
labelMap := getLabelsFromContainerInspect(&containerInfo)
containerTenant := getTenantFromContainerInspect(&containerInfo)
networkName, ipAddress, err := getEpNetworkInfoFromContainerInspect(&containerInfo, networkID)
if err != nil {
log.Errorf("Error getting container network info for %v.Err:%s", containerID, err)
}
endpoint := getEndpointFromContainerInspect(&containerInfo,
networkName, containerTenant)

if ipAddress != "" {
//Create provider info
endpointUpdReq.IPAddress = ipAddress
endpointUpdReq.ContainerID = containerID
endpointUpdReq.Tenant = containerTenant
endpointUpdReq.Network = networkName
endpointUpdReq.Event = event.Action
endpointUpdReq.EndpointID = endpoint
endpointUpdReq.ContainerName = containerInfo.Name
endpointUpdReq.Labels = make(map[string]string)

for k, v := range labelMap {
endpointUpdReq.Labels[k] = v
}
}

var epUpdResp master.UpdateEndpointResponse
var epUpdResp master.UpdateEndpointResponse

log.Infof("Sending Endpoint update request to master: {%+v}", endpointUpdReq)
log.Infof("Sending Endpoint update request to master: {%+v}", endpointUpdReq)

err = cluster.MasterPostReq("/plugin/updateEndpoint", endpointUpdReq, &epUpdResp)
if err != nil {
log.Errorf("Event: 'start' , Http error posting endpoint update, Error:%s", err)
}
} else {
log.Errorf("Unable to fetch container labels for container %s ", event.ID)
err = cluster.MasterPostReq("/plugin/updateEndpoint", endpointUpdReq, &epUpdResp)
if err != nil {
log.Errorf("Event: 'start' , Http error posting endpoint update, Error:%s", err)
}
case "die":

} else if event.Type == "container" && event.Action == "die" {
endpointUpdReq.ContainerID = event.ID
endpointUpdReq.Event = "die"
var epUpdResp master.UpdateEndpointResponse
Expand Down Expand Up @@ -120,14 +136,21 @@ func getTenantFromContainerInspect(containerInfo *types.ContainerJSON) string {
}

/*getEpNetworkInfoFromContainerInspect inspects the network info from containerinfo returned by dockerclient*/
func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON) (string, string, error) {
func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON,
networkID string) (string, string, error) {

var networkName string
var IPAddress string
var networkUUID string

networkName = ""
IPAddress = ""

if containerInfo != nil && containerInfo.NetworkSettings != nil {
for _, endpoint := range containerInfo.NetworkSettings.Networks {
IPAddress = endpoint.IPAddress
networkUUID = endpoint.NetworkID

_, network, serviceName, err := dockplugin.GetDockerNetworkName(networkUUID)
if err != nil {
log.Errorf("Error getting docker networkname for network uuid : %s", networkUUID)
Expand All @@ -138,8 +161,13 @@ func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON) (s
} else {
networkName = network
}

if networkID != "" && strings.EqualFold(networkID, networkUUID) {
break
}
}
}

return networkName, IPAddress, nil
}

Expand All @@ -155,14 +183,25 @@ func getContainerFromContainerInspect(containerInfo *types.ContainerJSON) string

}

func getEndpointFromContainerInspect(containerInfo *types.ContainerJSON) string {
func getEndpointFromContainerInspect(containerInfo *types.ContainerJSON,
networkName string,
tenantName string,
) string {

endpointID := ""
qualifiedName := ""

if 0 == strings.Compare(tenantName, "default") {
qualifiedName = networkName
} else {
qualifiedName = networkName + "/" + tenantName
}

if containerInfo != nil && containerInfo.NetworkSettings != nil {
for _, endpoint := range containerInfo.NetworkSettings.Networks {
endpoint, ok := containerInfo.NetworkSettings.Networks[qualifiedName]
if ok {
endpointID = endpoint.EndpointID
}
}
return endpointID

}
Empty file modified test/systemtests/docker_test.go
100755 → 100644
Empty file.
130 changes: 130 additions & 0 deletions test/systemtests/network_test.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func (s *systemtestSuite) TestNetworkAddDeleteVLAN(c *C) {
s.testNetworkAddDelete(c, "vlan")
}

func (s *systemtestSuite) TestNetworkAddDeleteVLANWithDns(c *C) {
s.testNetworkAddDeleteWithDns(c, "vlan")
}

func (s *systemtestSuite) TestNetworkAddDeleteVXLANWithDns(c *C) {
s.testNetworkAddDeleteWithDns(c, "vxlan")
}

func (s *systemtestSuite) testNetworkAddDelete(c *C, encap string) {

if s.fwdMode == "routing" && encap == "vlan" {
Expand Down Expand Up @@ -190,6 +198,58 @@ func (s *systemtestSuite) testNetworkAddDelete(c *C, encap string) {
}
}

func (s *systemtestSuite) testNetworkAddDeleteWithDns(c *C, encap string) {

var err error

for i := 0; i < s.basicInfo.Iterations; i++ {
var (
netNames = []string{}
)

for networkNum := 0; networkNum < 3; networkNum++ {
var v6subnet, v6gateway string
if networkNum%2 == 0 {
v6subnet = ""
v6gateway = ""
} else {
v6subnet = fmt.Sprintf("1001:%d::/120", networkNum)
v6gateway = fmt.Sprintf("1001:%d::254", networkNum)
}
network := &client.Network{
TenantName: "default",
NetworkName: fmt.Sprintf("net%d-%d", networkNum, i),
Subnet: fmt.Sprintf("10.1.%d.0/24", networkNum),
Gateway: fmt.Sprintf("10.1.%d.254", networkNum),
Ipv6Subnet: v6subnet,
Ipv6Gateway: v6gateway,
PktTag: 1001 + networkNum,
Encap: encap,
}

c.Assert(s.cli.NetworkPost(network), IsNil)
netNames = append(netNames, network.NetworkName)
}

// Wait 5 seconds for the call backs to come through

time.Sleep(5 * time.Second)

for _, netName := range netNames {

/*
Now that the networks are created, check that there is a dns endpoint on each network
*/
_, err = s.testNetworkInspectDNS("default", netName)
c.Assert(err, IsNil)
}

for _, netName := range netNames {
c.Assert(s.cli.NetworkDelete("default", netName), IsNil)
}
}
}

func (s *systemtestSuite) TestNetworkAddDeleteNoGatewayVXLAN(c *C) {
s.testNetworkAddDeleteNoGateway(c, "vxlan")
}
Expand Down Expand Up @@ -279,6 +339,10 @@ func (s *systemtestSuite) TestNetworkAddDeleteTenantVLAN(c *C) {
s.testNetworkAddDeleteTenant(c, "vlan", s.fwdMode)
}

func (s *systemtestSuite) TestNetworkAddDeleteTenantWithDns(c *C) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

s.testNetworkAddDeleteTenantWithDns(c, "vlan", s.fwdMode)
}

func (s *systemtestSuite) testNetworkAddDeleteTenant(c *C, encap, fwdmode string) {
mutex := sync.Mutex{}

Expand Down Expand Up @@ -371,6 +435,72 @@ func (s *systemtestSuite) testNetworkAddDeleteTenant(c *C, encap, fwdmode string

}

func (s *systemtestSuite) testNetworkAddDeleteTenantWithDns(c *C, encap, fwdmode string) {

var err error

for i := 0; i < s.basicInfo.Iterations; i++ {
var (
tenantNames = map[string][]string{}
netNames = []string{}
pktTag = 0
)

for tenantNum := 0; tenantNum < 3; tenantNum++ {
tenantName := fmt.Sprintf("tenant%d", tenantNum)
c.Assert(s.cli.TenantPost(&client.Tenant{TenantName: tenantName}), IsNil)
tenantNames[tenantName] = []string{}

for networkNum := 0; networkNum < 3; networkNum++ {
var v6subnet, v6gateway string
if networkNum%2 == 0 {
v6subnet = ""
v6gateway = ""
} else {
v6subnet = fmt.Sprintf("1001:%d:%d::/120", tenantNum, networkNum)
v6gateway = fmt.Sprintf("1001:%d:%d::254", tenantNum, networkNum)
}
network := &client.Network{
TenantName: tenantName,
NetworkName: fmt.Sprintf("net%d-%d", networkNum, i),
Subnet: fmt.Sprintf("10.%d.%d.0/24", tenantNum, networkNum),
Gateway: fmt.Sprintf("10.%d.%d.254", tenantNum, networkNum),
Ipv6Subnet: v6subnet,
Ipv6Gateway: v6gateway,
PktTag: pktTag + 1000,
Encap: encap,
}

logrus.Infof("Creating network %s on tenant %s", network.NetworkName, network.TenantName)

c.Assert(s.cli.NetworkPost(network), IsNil)
netNames = append(netNames, network.NetworkName)
tenantNames[tenantName] = append(tenantNames[tenantName], network.NetworkName)
pktTag++
}
}

// Wait 10 seconds for the call backs to come through

time.Sleep(10 * time.Second)

for tenant, networks := range tenantNames {
for _, network := range networks {
_, err = s.testNetworkInspectDNS(tenant, network)
c.Assert(err, IsNil)
}
}

for tenant, networks := range tenantNames {
for _, network := range networks {
c.Assert(s.cli.NetworkDelete(tenant, network), IsNil)
}
c.Assert(s.cli.TenantDelete(tenant), IsNil)
}
}

}

func (s *systemtestSuite) TestNetworkAddDeleteTenantFwdModeChangeVXLAN(c *C) {
fwdMode := s.fwdMode
for i := 0; i < s.basicInfo.Iterations; i++ {
Expand Down
Loading