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