Skip to content

Commit aa8ba21

Browse files
committed
Fixes issue contiv#541
Adjustments for last set of review comments
1 parent 43820b2 commit aa8ba21

File tree

7 files changed

+248
-64
lines changed

7 files changed

+248
-64
lines changed

netctl/netctl.go

100755100644
-18
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ 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)
69-
7068
pol, err := getClient(ctx).PolicyInspect(tenant, policy)
7169
errCheck(ctx, err)
7270

@@ -388,7 +386,6 @@ func inspectNetprofile(ctx *cli.Context) {
388386

389387
tenant := ctx.String("tenant")
390388
netprofile := ctx.Args()[0]
391-
fmt.Printf("Inspecting netprofile:%s for %s", netprofile, tenant)
392389

393390
profileList, err := getClient(ctx).NetprofileInspect(tenant, netprofile)
394391
errCheck(ctx, err)
@@ -471,8 +468,6 @@ func inspectNetwork(ctx *cli.Context) {
471468
tenant := ctx.String("tenant")
472469
network := ctx.Args()[0]
473470

474-
fmt.Printf("Inspeting network: %s tenant: %s\n", network, tenant)
475-
476471
net, err := getClient(ctx).NetworkInspect(tenant, network)
477472
errCheck(ctx, err)
478473

@@ -570,8 +565,6 @@ func inspectTenant(ctx *cli.Context) {
570565

571566
tenant := ctx.Args()[0]
572567

573-
fmt.Printf("Inspecting tenant: %s ", tenant)
574-
575568
ten, err := getClient(ctx).TenantInspect(tenant)
576569
errCheck(ctx, err)
577570

@@ -617,9 +610,6 @@ func inspectEndpoint(ctx *cli.Context) {
617610
}
618611

619612
epid := ctx.Args()[0]
620-
621-
fmt.Printf("Inspecting endpoint: %s\n", epid)
622-
623613
net, err := getClient(ctx).EndpointInspect(epid)
624614
errCheck(ctx, err)
625615

@@ -661,8 +651,6 @@ func inspectEndpointGroup(ctx *cli.Context) {
661651
tenant := ctx.String("tenant")
662652
endpointGroup := ctx.Args()[0]
663653

664-
fmt.Printf("Inspeting endpointGroup: %s tenant: %s\n", endpointGroup, tenant)
665-
666654
epg, err := getClient(ctx).EndpointGroupInspect(tenant, endpointGroup)
667655
errCheck(ctx, err)
668656

@@ -829,8 +817,6 @@ func inspectBgp(ctx *cli.Context) {
829817

830818
hostname := ctx.Args()[0]
831819

832-
fmt.Printf("netctl. Inspecting bgp: %s\n", hostname)
833-
834820
bgp, err := getClient(ctx).BgpInspect(hostname)
835821
errCheck(ctx, err)
836822

@@ -866,8 +852,6 @@ func inspectGlobal(ctx *cli.Context) {
866852
errExit(ctx, exitHelp, "More arguments than required", true)
867853
}
868854

869-
fmt.Printf("Inspecting global\n")
870-
871855
ginfo, err := getClient(ctx).GlobalInspect("global")
872856
errCheck(ctx, err)
873857

@@ -1262,8 +1246,6 @@ func inspectServiceLb(ctx *cli.Context) {
12621246
tenant := ctx.String("tenant")
12631247
service := ctx.Args()[0]
12641248

1265-
fmt.Printf("Inspecting service: %s tenant: %s\n", service, tenant)
1266-
12671249
net, err := getClient(ctx).ServiceLBInspect(tenant, service)
12681250
errCheck(ctx, err)
12691251

netmaster/master/api.go

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

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

netplugin/agent/docker_event.go

+83-42
Original file line numberDiff line numberDiff line change
@@ -28,64 +28,82 @@ 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.
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+
/* Currently we need to handle "container start", "container die" and "network connect" events only */
41+
42+
if (event.Type == "container" && event.Action == "start") ||
43+
(event.Type == "network" && event.Action == "connect") {
44+
45+
if event.Type == "container" {
46+
containerID = event.ID
47+
networkID = ""
48+
} else {
49+
containerID = event.Actor.Attributes["container"]
50+
networkID = event.Actor.ID
51+
}
52+
53+
if containerID == "" {
54+
log.Errorf("Container ID missing in docker event {%#v}\n", *event)
55+
return
56+
}
57+
3958
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
4059
cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.21", nil, defaultHeaders)
4160
if err != nil {
42-
panic(err)
61+
log.Errorf("Client lookup failed :%s", err)
62+
return
4363
}
4464

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

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

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.EPCommonName = containerInfo.Name
70-
endpointUpdReq.Labels = make(map[string]string)
71-
72-
for k, v := range labelMap {
73-
endpointUpdReq.Labels[k] = v
74-
}
72+
labelMap := getLabelsFromContainerInspect(&containerInfo)
73+
containerTenant := getTenantFromContainerInspect(&containerInfo)
74+
networkName, ipAddress, err := getEpNetworkInfoFromContainerInspect(&containerInfo, networkID)
75+
if err != nil {
76+
log.Errorf("Error getting container network info for %v.Err:%s", containerID, err)
77+
}
78+
endpoint := getEndpointFromContainerInspect(&containerInfo,
79+
networkName, containerTenant)
80+
81+
if ipAddress != "" {
82+
//Create provider info
83+
endpointUpdReq.IPAddress = ipAddress
84+
endpointUpdReq.ContainerID = containerID
85+
endpointUpdReq.Tenant = containerTenant
86+
endpointUpdReq.Network = networkName
87+
endpointUpdReq.Event = event.Action
88+
endpointUpdReq.EndpointID = endpoint
89+
endpointUpdReq.ContainerName = containerInfo.Name
90+
endpointUpdReq.Labels = make(map[string]string)
91+
92+
for k, v := range labelMap {
93+
endpointUpdReq.Labels[k] = v
7594
}
95+
}
7696

77-
var epUpdResp master.UpdateEndpointResponse
97+
var epUpdResp master.UpdateEndpointResponse
7898

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

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)
101+
err = cluster.MasterPostReq("/plugin/updateEndpoint", endpointUpdReq, &epUpdResp)
102+
if err != nil {
103+
log.Errorf("Event: 'start' , Http error posting endpoint update, Error:%s", err)
87104
}
88-
case "die":
105+
106+
} else if event.Type == "container" && event.Action == "die" {
89107
endpointUpdReq.ContainerID = event.ID
90108
endpointUpdReq.Event = "die"
91109
var epUpdResp master.UpdateEndpointResponse
@@ -120,14 +138,21 @@ func getTenantFromContainerInspect(containerInfo *types.ContainerJSON) string {
120138
}
121139

122140
/*getEpNetworkInfoFromContainerInspect inspects the network info from containerinfo returned by dockerclient*/
123-
func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON) (string, string, error) {
141+
func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON,
142+
networkID string) (string, string, error) {
143+
124144
var networkName string
125145
var IPAddress string
126146
var networkUUID string
147+
148+
networkName = ""
149+
IPAddress = ""
150+
127151
if containerInfo != nil && containerInfo.NetworkSettings != nil {
128152
for _, endpoint := range containerInfo.NetworkSettings.Networks {
129153
IPAddress = endpoint.IPAddress
130154
networkUUID = endpoint.NetworkID
155+
131156
_, network, serviceName, err := dockplugin.GetDockerNetworkName(networkUUID)
132157
if err != nil {
133158
log.Errorf("Error getting docker networkname for network uuid : %s", networkUUID)
@@ -138,8 +163,13 @@ func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON) (s
138163
} else {
139164
networkName = network
140165
}
166+
167+
if networkID != "" && strings.EqualFold(networkID, networkUUID) {
168+
break
169+
}
141170
}
142171
}
172+
143173
return networkName, IPAddress, nil
144174
}
145175

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

156186
}
157187

158-
func getEndpointFromContainerInspect(containerInfo *types.ContainerJSON) string {
188+
func getEndpointFromContainerInspect(containerInfo *types.ContainerJSON,
189+
networkName string,
190+
tenantName string,
191+
) string {
159192

160193
endpointID := ""
194+
qualifiedName := ""
195+
196+
if 0 == strings.Compare(tenantName, "default") {
197+
qualifiedName = networkName
198+
} else {
199+
qualifiedName = networkName + "/" + tenantName
200+
}
201+
161202
if containerInfo != nil && containerInfo.NetworkSettings != nil {
162-
for _, endpoint := range containerInfo.NetworkSettings.Networks {
203+
endpoint, ok := containerInfo.NetworkSettings.Networks[qualifiedName]
204+
if ok {
163205
endpointID = endpoint.EndpointID
164206
}
165207
}
166208
return endpointID
167-
168209
}

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.

0 commit comments

Comments
 (0)