@@ -42,6 +42,7 @@ const (
42
42
43
43
type cliOpts struct {
44
44
hostLabel string
45
+ nativeInteg bool
45
46
publishVtep bool
46
47
}
47
48
@@ -213,6 +214,7 @@ func getEndpointContainerContext(state core.StateDriver, epId string) (
213
214
return & epCtx , nil
214
215
}
215
216
epCtx .NewContName = epCfg .ContName
217
+ epCtx .NewAttachUUID = epCfg .AttachUUID
216
218
217
219
cfgNet := & drivers.OvsCfgNetworkState {StateDriver : state }
218
220
err = cfgNet .Read (epCfg .NetId )
@@ -230,6 +232,7 @@ func getEndpointContainerContext(state core.StateDriver, epId string) (
230
232
epCtx .CurrContName = operEp .ContName
231
233
epCtx .InterfaceId = operEp .PortName
232
234
epCtx .IpAddress = operEp .IpAddress
235
+ epCtx .CurrAttachUUID = operEp .AttachUUID
233
236
234
237
return & epCtx , err
235
238
}
@@ -264,21 +267,47 @@ func getContainerEpContextByContName(state core.StateDriver, contName string) (
264
267
return epCtxs [:idx ], nil
265
268
}
266
269
270
+ func contAttachPointAdded (epCtx * crtclient.ContainerEpContext ) bool {
271
+ if epCtx .CurrAttachUUID == "" && epCtx .NewAttachUUID != "" {
272
+ return true
273
+ }
274
+ if epCtx .CurrContName == "" && epCtx .NewContName != "" {
275
+ return true
276
+ }
277
+ return false
278
+ }
279
+
280
+ func contAttachPointDeleted (epCtx * crtclient.ContainerEpContext ) bool {
281
+ if epCtx .CurrAttachUUID != "" && epCtx .NewAttachUUID == "" {
282
+ return true
283
+ }
284
+ if epCtx .CurrContName != "" && epCtx .NewContName == "" {
285
+ return true
286
+ }
287
+ return false
288
+ }
289
+
267
290
func processEpEvent (netPlugin * plugin.NetPlugin , crt * crt.Crt ,
268
291
epId string , preValue string , opts cliOpts ) (err error ) {
269
292
293
+ deleteOp := false
270
294
homingHost := ""
271
295
vtepIp := ""
296
+
297
+ epCfg := & drivers.OvsCfgEndpointState {StateDriver : netPlugin .StateDriver }
298
+ err = epCfg .Read (epId )
272
299
if preValue == "" {
273
- epCfg := & drivers.OvsCfgEndpointState {StateDriver : netPlugin .StateDriver }
274
- err = epCfg .Read (epId )
275
300
if err != nil {
276
301
log .Printf ("Failed to read config for ep '%s' \n " , epId )
277
302
return
278
303
}
304
+
279
305
homingHost = epCfg .HomingHost
280
306
vtepIp = epCfg .VtepIp
281
307
} else {
308
+ if err != nil {
309
+ deleteOp = true
310
+ }
282
311
epOper := & drivers.OvsOperEndpointState {StateDriver : netPlugin .StateDriver }
283
312
err = epOper .Read (epId )
284
313
if err != nil {
@@ -305,7 +334,7 @@ func processEpEvent(netPlugin *plugin.NetPlugin, crt *crt.Crt,
305
334
// log.Printf("read endpoint context: %s \n", contEpContext)
306
335
307
336
operStr := ""
308
- if preValue != "" {
337
+ if deleteOp {
309
338
err = netPlugin .DeleteEndpoint (epId )
310
339
operStr = "delete"
311
340
} else {
@@ -320,8 +349,7 @@ func processEpEvent(netPlugin *plugin.NetPlugin, crt *crt.Crt,
320
349
log .Printf ("Endpoint operation %s succeeded" , operStr )
321
350
322
351
// attach or detach an endpoint to a container
323
- if preValue != "" ||
324
- (contEpContext .NewContName == "" && contEpContext .CurrContName != "" ) {
352
+ if deleteOp || contAttachPointDeleted (contEpContext ) {
325
353
err = crt .ContainerIf .DetachEndpoint (contEpContext )
326
354
if err != nil {
327
355
log .Printf ("Endpoint detach container '%s' from ep '%s' failed . " +
@@ -331,7 +359,7 @@ func processEpEvent(netPlugin *plugin.NetPlugin, crt *crt.Crt,
331
359
contEpContext .CurrContName , epId )
332
360
}
333
361
}
334
- if preValue == "" && contEpContext . NewContName != "" {
362
+ if ! deleteOp && contAttachPointAdded ( contEpContext ) {
335
363
// re-read post ep updated state
336
364
newContEpContext , err1 := getEndpointContainerContext (
337
365
netPlugin .StateDriver , epId )
@@ -392,27 +420,22 @@ func handleEtcdEvents(netPlugin *plugin.NetPlugin, crt *crt.Crt,
392
420
retErr <- nil
393
421
}
394
422
395
- func handleContainerStart (netPlugin * plugin.NetPlugin , crt * crt.Crt ,
396
- contId string ) error {
397
- var err error
398
- var epContexts []crtclient.ContainerEpContext
423
+ func attachContainer (stateDriver core.StateDriver , crt * crt.Crt , contName string ) error {
399
424
400
- contName , err := crt .GetContainerName (contId )
401
- if err != nil {
402
- log .Printf ("Could not find container name from container id %s \n " , contId )
403
- return err
404
- }
405
-
406
- epContexts , err = getContainerEpContextByContName (netPlugin .StateDriver ,
407
- contName )
425
+ epContexts , err := getContainerEpContextByContName (stateDriver , contName )
408
426
if err != nil {
409
427
log .Printf ("Error '%s' getting Ep context for container %s \n " ,
410
428
err , contName )
411
429
return err
412
430
}
413
431
414
432
for _ , epCtx := range epContexts {
415
- log .Printf ("## trying attach on epctx %v \n " , epCtx )
433
+ if epCtx .NewAttachUUID != "" || epCtx .InterfaceId == "" {
434
+ log .Printf ("## skipping attach on epctx %v \n " , epCtx )
435
+ continue
436
+ } else {
437
+ log .Printf ("## trying attach on epctx %v \n " , epCtx )
438
+ }
416
439
err = crt .AttachEndpoint (& epCtx )
417
440
if err != nil {
418
441
log .Printf ("Error '%s' attaching container to the network \n " , err )
@@ -423,6 +446,24 @@ func handleContainerStart(netPlugin *plugin.NetPlugin, crt *crt.Crt,
423
446
return nil
424
447
}
425
448
449
+ func handleContainerStart (netPlugin * plugin.NetPlugin , crt * crt.Crt ,
450
+ contId string ) error {
451
+ // var epContexts []crtclient.ContainerEpContext
452
+
453
+ contName , err := crt .GetContainerName (contId )
454
+ if err != nil {
455
+ log .Printf ("Could not find container name from container id %s \n " , contId )
456
+ return err
457
+ }
458
+
459
+ err = attachContainer (netPlugin .StateDriver , crt , contName )
460
+ if err != nil {
461
+ log .Printf ("error attaching container err \n " , err )
462
+ }
463
+
464
+ return err
465
+ }
466
+
426
467
func handleDockerEvents (event * dockerclient.Event , retErr chan error ,
427
468
args ... interface {}) {
428
469
var err error
@@ -474,11 +515,13 @@ func handleEvents(netPlugin *plugin.NetPlugin, crt *crt.Crt,
474
515
475
516
go handleEtcdEvents (netPlugin , crt , rsps , stop , recvErr , opts )
476
517
477
- // start docker client and handle docker events
478
- // wait on error chan for problems handling the docker events
479
- dockerCrt := crt .ContainerIf .(* docker.Docker )
480
- dockerCrt .Client .StartMonitorEvents (handleDockerEvents , recvErr ,
481
- netPlugin , crt )
518
+ if ! opts .nativeInteg {
519
+ // start docker client and handle docker events
520
+ // wait on error chan for problems handling the docker events
521
+ dockerCrt := crt .ContainerIf .(* docker.Docker )
522
+ dockerCrt .Client .StartMonitorEvents (handleDockerEvents , recvErr ,
523
+ netPlugin , crt )
524
+ }
482
525
483
526
// XXX: todo, restore any config that might have been created till this
484
527
// point
@@ -512,6 +555,10 @@ func main() {
512
555
"host-label" ,
513
556
defHostLabel ,
514
557
"label used to identify endpoints homed for this host, default is host name" )
558
+ flagSet .BoolVar (& opts .nativeInteg ,
559
+ "native-integration" ,
560
+ false ,
561
+ "do not listen to container runtime events, because the events are natively integrated into their call sequence and external integration is not required" )
515
562
flagSet .BoolVar (& opts .publishVtep ,
516
563
"publish-vtep" ,
517
564
false ,
0 commit comments