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