@@ -28,64 +28,80 @@ import (
28
28
"golang.org/x/net/context"
29
29
)
30
30
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*/
33
32
func handleDockerEvents (event * dockerclient.Event , ec chan error , args ... interface {}) {
34
33
35
34
log .Debugf ("Received Docker event: {%#v}\n " , * event )
36
35
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
+
39
56
defaultHeaders := map [string ]string {"User-Agent" : "engine-api-cli-1.0" }
40
57
cli , err := client .NewClient ("unix:///var/run/docker.sock" , "v1.21" , nil , defaultHeaders )
41
58
if err != nil {
42
- panic (err )
59
+ log .Errorf ("Client lookup failed :%s" , err )
60
+ return
43
61
}
44
62
45
- containerInfo , err := cli .ContainerInspect (context .Background (), event . ID )
63
+ containerInfo , err := cli .ContainerInspect (context .Background (), containerID )
46
64
47
65
if err != nil {
48
66
log .Errorf ("Container Inspect failed :%s" , err )
49
67
return
50
68
}
51
69
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
75
92
}
93
+ }
76
94
77
- var epUpdResp master.UpdateEndpointResponse
95
+ var epUpdResp master.UpdateEndpointResponse
78
96
79
- log .Infof ("Sending Endpoint update request to master: {%+v}" , endpointUpdReq )
97
+ log .Infof ("Sending Endpoint update request to master: {%+v}" , endpointUpdReq )
80
98
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 )
87
102
}
88
- case "die" :
103
+
104
+ } else if event .Type == "container" && event .Action == "die" {
89
105
endpointUpdReq .ContainerID = event .ID
90
106
endpointUpdReq .Event = "die"
91
107
var epUpdResp master.UpdateEndpointResponse
@@ -120,14 +136,21 @@ func getTenantFromContainerInspect(containerInfo *types.ContainerJSON) string {
120
136
}
121
137
122
138
/*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
+
124
142
var networkName string
125
143
var IPAddress string
126
144
var networkUUID string
145
+
146
+ networkName = ""
147
+ IPAddress = ""
148
+
127
149
if containerInfo != nil && containerInfo .NetworkSettings != nil {
128
150
for _ , endpoint := range containerInfo .NetworkSettings .Networks {
129
151
IPAddress = endpoint .IPAddress
130
152
networkUUID = endpoint .NetworkID
153
+
131
154
_ , network , serviceName , err := dockplugin .GetDockerNetworkName (networkUUID )
132
155
if err != nil {
133
156
log .Errorf ("Error getting docker networkname for network uuid : %s" , networkUUID )
@@ -138,8 +161,13 @@ func getEpNetworkInfoFromContainerInspect(containerInfo *types.ContainerJSON) (s
138
161
} else {
139
162
networkName = network
140
163
}
164
+
165
+ if networkID != "" && strings .EqualFold (networkID , networkUUID ) {
166
+ break
167
+ }
141
168
}
142
169
}
170
+
143
171
return networkName , IPAddress , nil
144
172
}
145
173
@@ -155,14 +183,25 @@ func getContainerFromContainerInspect(containerInfo *types.ContainerJSON) string
155
183
156
184
}
157
185
158
- func getEndpointFromContainerInspect (containerInfo * types.ContainerJSON ) string {
186
+ func getEndpointFromContainerInspect (containerInfo * types.ContainerJSON ,
187
+ networkName string ,
188
+ tenantName string ,
189
+ ) string {
159
190
160
191
endpointID := ""
192
+ qualifiedName := ""
193
+
194
+ if 0 == strings .Compare (tenantName , "default" ) {
195
+ qualifiedName = networkName
196
+ } else {
197
+ qualifiedName = networkName + "/" + tenantName
198
+ }
199
+
161
200
if containerInfo != nil && containerInfo .NetworkSettings != nil {
162
- for _ , endpoint := range containerInfo .NetworkSettings .Networks {
201
+ endpoint , ok := containerInfo .NetworkSettings .Networks [qualifiedName ]
202
+ if ok {
163
203
endpointID = endpoint .EndpointID
164
204
}
165
205
}
166
206
return endpointID
167
-
168
207
}
0 commit comments