Skip to content

Commit d9c113e

Browse files
committed
Fixes issue contiv#541
1 parent 6eca380 commit d9c113e

File tree

7 files changed

+248
-49
lines changed

7 files changed

+248
-49
lines changed

netctl/netctl.go

100755100644
+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func inspectPolicy(ctx *cli.Context) {
6565
tenant := ctx.String("tenant")
6666
policy := ctx.Args()[0]
6767

68-
fmt.Printf("Inspeting policy: %s tenant: %s\n", policy, tenant)
68+
fmt.Printf("Inspecting policy: %s tenant: %s\n", policy, tenant)
6969

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

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

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

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

666666
epg, err := getClient(ctx).EndpointGroupInspect(tenant, endpointGroup)
667667
errCheck(ctx, err)

netmaster/master/api.go

100755100644
+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func UpdateEndpointHandler(w http.ResponseWriter, r *http.Request, vars map[stri
346346
return nil, err
347347
}
348348

349-
if epUpdReq.Event == "start" {
349+
if epUpdReq.Event == "start" || epUpdReq.Event == "connect" {
350350
//Received container start event from netplugin. Check if the Provider
351351
//matches any service and perform service provider update if there is a matching
352352
//service.

netplugin/agent/docker_event.go

+81-42
Original file line numberDiff line numberDiff line change
@@ -28,64 +28,80 @@ import (
2828
"golang.org/x/net/context"
2929
)
3030

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

3534
log.Debugf("Received Docker event: {%#v}\n", *event)
3635
endpointUpdReq := &master.UpdateEndpointRequest{}
37-
switch event.Status {
38-
case "start":
36+
37+
var containerID string
38+
var networkID string
39+
40+
if (event.Type == "container" && event.Action == "start") ||
41+
(event.Type == "network" && event.Action == "connect") {
42+
43+
if event.Type == "container" {
44+
containerID = event.ID
45+
networkID = ""
46+
} else {
47+
containerID = event.Actor.Attributes["container"]
48+
networkID = event.Actor.ID
49+
}
50+
51+
if containerID == "" {
52+
log.Errorf("Container ID missing in docker event {%#v}\n", *event)
53+
return
54+
}
55+
3956
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
4057
cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.21", nil, defaultHeaders)
4158
if err != nil {
42-
panic(err)
59+
log.Errorf("Client lookup failed :%s", err)
60+
return
4361
}
4462

45-
containerInfo, err := cli.ContainerInspect(context.Background(), event.ID)
63+
containerInfo, err := cli.ContainerInspect(context.Background(), containerID)
4664

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

52-
if event.ID != "" {
53-
labelMap := getLabelsFromContainerInspect(&containerInfo)
54-
containerTenant := getTenantFromContainerInspect(&containerInfo)
55-
networkName, ipAddress, err := getEpNetworkInfoFromContainerInspect(&containerInfo)
56-
if err != nil {
57-
log.Errorf("Error getting container network info for %v.Err:%s", event.ID, err)
58-
}
59-
endpoint := getEndpointFromContainerInspect(&containerInfo)
60-
61-
if ipAddress != "" {
62-
//Create provider info
63-
endpointUpdReq.IPAddress = ipAddress
64-
endpointUpdReq.ContainerID = event.ID
65-
endpointUpdReq.Tenant = containerTenant
66-
endpointUpdReq.Network = networkName
67-
endpointUpdReq.Event = "start"
68-
endpointUpdReq.EndpointID = endpoint
69-
endpointUpdReq.ContainerName = containerInfo.Name
70-
endpointUpdReq.Labels = make(map[string]string)
71-
72-
for k, v := range labelMap {
73-
endpointUpdReq.Labels[k] = v
74-
}
70+
labelMap := getLabelsFromContainerInspect(&containerInfo)
71+
containerTenant := getTenantFromContainerInspect(&containerInfo)
72+
networkName, ipAddress, err := getEpNetworkInfoFromContainerInspect(&containerInfo, networkID)
73+
if err != nil {
74+
log.Errorf("Error getting container network info for %v.Err:%s", containerID, err)
75+
}
76+
endpoint := getEndpointFromContainerInspect(&containerInfo,
77+
networkName, containerTenant)
78+
79+
if ipAddress != "" {
80+
//Create provider info
81+
endpointUpdReq.IPAddress = ipAddress
82+
endpointUpdReq.ContainerID = containerID
83+
endpointUpdReq.Tenant = containerTenant
84+
endpointUpdReq.Network = networkName
85+
endpointUpdReq.Event = event.Action
86+
endpointUpdReq.EndpointID = endpoint
87+
endpointUpdReq.ContainerName = containerInfo.Name
88+
endpointUpdReq.Labels = make(map[string]string)
89+
90+
for k, v := range labelMap {
91+
endpointUpdReq.Labels[k] = v
7592
}
93+
}
7694

77-
var epUpdResp master.UpdateEndpointResponse
95+
var epUpdResp master.UpdateEndpointResponse
7896

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

81-
err = cluster.MasterPostReq("/plugin/updateEndpoint", endpointUpdReq, &epUpdResp)
82-
if err != nil {
83-
log.Errorf("Event: 'start' , Http error posting endpoint update, Error:%s", err)
84-
}
85-
} else {
86-
log.Errorf("Unable to fetch container labels for container %s ", event.ID)
99+
err = cluster.MasterPostReq("/plugin/updateEndpoint", endpointUpdReq, &epUpdResp)
100+
if err != nil {
101+
log.Errorf("Event: 'start' , Http error posting endpoint update, Error:%s", err)
87102
}
88-
case "die":
103+
104+
} else if event.Type == "container" && event.Action == "die" {
89105
endpointUpdReq.ContainerID = event.ID
90106
endpointUpdReq.Event = "die"
91107
var epUpdResp master.UpdateEndpointResponse
@@ -120,14 +136,21 @@ func getTenantFromContainerInspect(containerInfo *types.ContainerJSON) string {
120136
}
121137

122138
/*getEpNetworkInfoFromContainerInspect inspects the network info from containerinfo returned by dockerclient*/
123-
func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON) (string, string, error) {
139+
func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON,
140+
networkID string) (string, string, error) {
141+
124142
var networkName string
125143
var IPAddress string
126144
var networkUUID string
145+
146+
networkName = ""
147+
IPAddress = ""
148+
127149
if containerInfo != nil && containerInfo.NetworkSettings != nil {
128150
for _, endpoint := range containerInfo.NetworkSettings.Networks {
129151
IPAddress = endpoint.IPAddress
130152
networkUUID = endpoint.NetworkID
153+
131154
_, network, serviceName, err := dockplugin.GetDockerNetworkName(networkUUID)
132155
if err != nil {
133156
log.Errorf("Error getting docker networkname for network uuid : %s", networkUUID)
@@ -138,8 +161,13 @@ func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON) (s
138161
} else {
139162
networkName = network
140163
}
164+
165+
if networkID != "" && strings.EqualFold(networkID, networkUUID) {
166+
break
167+
}
141168
}
142169
}
170+
143171
return networkName, IPAddress, nil
144172
}
145173

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

156184
}
157185

158-
func getEndpointFromContainerInspect(containerInfo *types.ContainerJSON) string {
186+
func getEndpointFromContainerInspect(containerInfo *types.ContainerJSON,
187+
networkName string,
188+
tenantName string,
189+
) string {
159190

160191
endpointID := ""
192+
qualifiedName := ""
193+
194+
if 0 == strings.Compare(tenantName, "default") {
195+
qualifiedName = networkName
196+
} else {
197+
qualifiedName = networkName + "/" + tenantName
198+
}
199+
161200
if containerInfo != nil && containerInfo.NetworkSettings != nil {
162-
for _, endpoint := range containerInfo.NetworkSettings.Networks {
201+
endpoint, ok := containerInfo.NetworkSettings.Networks[qualifiedName]
202+
if ok {
163203
endpointID = endpoint.EndpointID
164204
}
165205
}
166206
return endpointID
167-
168207
}

test/systemtests/aci_util.go

100755100644
+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// EPSpec for aci-gw
1515
type EPSpec struct {
1616
Tenant string `json:"tenant,omitempty"`
17-
App string `json:"app,omitempty"`
17+
App string `json:"app-prof,omitempty"`
1818
Epg string `json:"epg,omitempty"`
1919
EpMac string `json:"epmac,omitempty"`
2020
}

test/systemtests/docker_test.go

100755100644
File mode changed.

test/systemtests/network_test.go

100755100644
+130
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ func (s *systemtestSuite) TestNetworkAddDeleteVLAN(c *C) {
8989
s.testNetworkAddDelete(c, "vlan")
9090
}
9191

92+
func (s *systemtestSuite) TestNetworkAddDeleteVLANWithDns(c *C) {
93+
s.testNetworkAddDeleteWithDns(c, "vlan")
94+
}
95+
96+
func (s *systemtestSuite) TestNetworkAddDeleteVXLANWithDns(c *C) {
97+
s.testNetworkAddDeleteWithDns(c, "vxlan")
98+
}
99+
92100
func (s *systemtestSuite) testNetworkAddDelete(c *C, encap string) {
93101

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

201+
func (s *systemtestSuite) testNetworkAddDeleteWithDns(c *C, encap string) {
202+
203+
var err error
204+
205+
for i := 0; i < s.basicInfo.Iterations; i++ {
206+
var (
207+
netNames = []string{}
208+
)
209+
210+
for networkNum := 0; networkNum < 3; networkNum++ {
211+
var v6subnet, v6gateway string
212+
if networkNum%2 == 0 {
213+
v6subnet = ""
214+
v6gateway = ""
215+
} else {
216+
v6subnet = fmt.Sprintf("1001:%d::/120", networkNum)
217+
v6gateway = fmt.Sprintf("1001:%d::254", networkNum)
218+
}
219+
network := &client.Network{
220+
TenantName: "default",
221+
NetworkName: fmt.Sprintf("net%d-%d", networkNum, i),
222+
Subnet: fmt.Sprintf("10.1.%d.0/24", networkNum),
223+
Gateway: fmt.Sprintf("10.1.%d.254", networkNum),
224+
Ipv6Subnet: v6subnet,
225+
Ipv6Gateway: v6gateway,
226+
PktTag: 1001 + networkNum,
227+
Encap: encap,
228+
}
229+
230+
c.Assert(s.cli.NetworkPost(network), IsNil)
231+
netNames = append(netNames, network.NetworkName)
232+
}
233+
234+
// Wait 5 seconds for the call backs to come through
235+
236+
time.Sleep(5 * time.Second)
237+
238+
for _, netName := range netNames {
239+
240+
/*
241+
Now that the networks are created, check that there is a dns endpoint on each network
242+
*/
243+
_, err = s.checkNetworkInspectDNS("default", netName)
244+
c.Assert(err, IsNil)
245+
}
246+
247+
for _, netName := range netNames {
248+
c.Assert(s.cli.NetworkDelete("default", netName), IsNil)
249+
}
250+
}
251+
}
252+
193253
func (s *systemtestSuite) TestNetworkAddDeleteNoGatewayVXLAN(c *C) {
194254
s.testNetworkAddDeleteNoGateway(c, "vxlan")
195255
}
@@ -279,6 +339,10 @@ func (s *systemtestSuite) TestNetworkAddDeleteTenantVLAN(c *C) {
279339
s.testNetworkAddDeleteTenant(c, "vlan", s.fwdMode)
280340
}
281341

342+
func (s *systemtestSuite) TestNetworkAddDeleteTenantWithDns(c *C) {
343+
s.testNetworkAddDeleteTenantWithDns(c, "vlan", s.fwdMode)
344+
}
345+
282346
func (s *systemtestSuite) testNetworkAddDeleteTenant(c *C, encap, fwdmode string) {
283347
mutex := sync.Mutex{}
284348

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

372436
}
373437

438+
func (s *systemtestSuite) testNetworkAddDeleteTenantWithDns(c *C, encap, fwdmode string) {
439+
440+
var err error
441+
442+
for i := 0; i < s.basicInfo.Iterations; i++ {
443+
var (
444+
tenantNames = map[string][]string{}
445+
netNames = []string{}
446+
pktTag = 0
447+
)
448+
449+
for tenantNum := 0; tenantNum < 3; tenantNum++ {
450+
tenantName := fmt.Sprintf("tenant%d", tenantNum)
451+
c.Assert(s.cli.TenantPost(&client.Tenant{TenantName: tenantName}), IsNil)
452+
tenantNames[tenantName] = []string{}
453+
454+
for networkNum := 0; networkNum < 3; networkNum++ {
455+
var v6subnet, v6gateway string
456+
if networkNum%2 == 0 {
457+
v6subnet = ""
458+
v6gateway = ""
459+
} else {
460+
v6subnet = fmt.Sprintf("1001:%d:%d::/120", tenantNum, networkNum)
461+
v6gateway = fmt.Sprintf("1001:%d:%d::254", tenantNum, networkNum)
462+
}
463+
network := &client.Network{
464+
TenantName: tenantName,
465+
NetworkName: fmt.Sprintf("net%d-%d", networkNum, i),
466+
Subnet: fmt.Sprintf("10.%d.%d.0/24", tenantNum, networkNum),
467+
Gateway: fmt.Sprintf("10.%d.%d.254", tenantNum, networkNum),
468+
Ipv6Subnet: v6subnet,
469+
Ipv6Gateway: v6gateway,
470+
PktTag: pktTag + 1000,
471+
Encap: encap,
472+
}
473+
474+
logrus.Infof("Creating network %s on tenant %s", network.NetworkName, network.TenantName)
475+
476+
c.Assert(s.cli.NetworkPost(network), IsNil)
477+
netNames = append(netNames, network.NetworkName)
478+
tenantNames[tenantName] = append(tenantNames[tenantName], network.NetworkName)
479+
pktTag++
480+
}
481+
}
482+
483+
// Wait 10 seconds for the call backs to come through
484+
485+
time.Sleep(10 * time.Second)
486+
487+
for tenant, networks := range tenantNames {
488+
for _, network := range networks {
489+
_, err = s.checkNetworkInspectDNS(tenant, network)
490+
c.Assert(err, IsNil)
491+
}
492+
}
493+
494+
for tenant, networks := range tenantNames {
495+
for _, network := range networks {
496+
c.Assert(s.cli.NetworkDelete(tenant, network), IsNil)
497+
}
498+
c.Assert(s.cli.TenantDelete(tenant), IsNil)
499+
}
500+
}
501+
502+
}
503+
374504
func (s *systemtestSuite) TestNetworkAddDeleteTenantFwdModeChangeVXLAN(c *C) {
375505
fwdMode := s.fwdMode
376506
for i := 0; i < s.basicInfo.Iterations; i++ {

0 commit comments

Comments
 (0)